Android Open Source - Joetz-Android-V2 Camp Adapter






From Project

Back to project page Joetz-Android-V2.

License

The source code is released under:

GNU General Public License

If you think the Android project Joetz-Android-V2 listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.example.jens.myapplication.camps;
//from   www . j a  va2  s .c o m
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.jens.myapplication.R;
import com.example.jens.myapplication.apimanager.ApiConnection;
import com.example.jens.myapplication.domain.Camp;
import com.example.jens.myapplication.domain.PeriodCategory;
import com.example.jens.myapplication.util.DateTimeStringConverter;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by Jens on 8/11/2014.
 */
public class CampAdapter extends RecyclerView.Adapter<CampAdapter.ViewHolder>  {
    private static final int ROWS_TO_CACHE_IMAGES = 2;

    private List<Camp> camps;
    private int rowLayout;
    private Context mContext;
    private GridLayoutManager layoutManager;

    //Pre-loading next/previous camp's thumbnail image
    private Map<Integer, BitmapDrawable> imagesMap;
    private int minInMap = 0;
    private int maxInMap = 0;

    private DisplayImageOptions imageLoaderOptions;

    public CampAdapter(List<Camp> camps, int rowLayout, Context context, GridLayoutManager layoutManager){
        this.camps = camps;
        this.rowLayout = rowLayout;
        this.mContext = context;
        this.layoutManager = layoutManager;
        this.imagesMap = new HashMap<Integer, BitmapDrawable>();

        imageLoaderOptions = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.stub)
               // .showImageForEmptyUri(R.drawable.ic_empty)
               // .showImageOnFail(R.drawable.ic_error)
                //.cacheInMemory(true)
                .cacheOnDisk(true)
                .considerExifParams(true)
                .build();

      //  imageLoader=new ImageLoader(context);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(viewGroup.getContext()).inflate(rowLayout, viewGroup, false);
        ViewHolder holder = new ViewHolder(v);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int position) {
        final Camp camp = camps.get(position);

        viewHolder.campName.setText(camp.getTitle());
        viewHolder.location.setText(camp.getLocation());
        viewHolder.age.setText(camp.getMinimumAge() + " - " + camp.getMaximumAge() + " " + "j");
        viewHolder.price.setText("\u20ac " + camp.getPrice().toString());
        viewHolder.campDate.setText(DateTimeStringConverter.getSimpleDate(camp.getStartDate())
                + " - " + DateTimeStringConverter.getSimpleDate(camp.getEndDate()));

        if(camp.getPeriodCategory() == PeriodCategory.OTHER){
            viewHolder.periodImageContainer.setVisibility(View.GONE);
        }else{
            viewHolder.periodImageContainer.setVisibility(View.VISIBLE);
        }

        viewHolder.periodeImage.setImageDrawable(mContext.getResources().getDrawable(camp.getPeriodCategory().getResourceIdImage()));


//        ImageLoader.getInstance().displayImage("http://theferkel.files.wordpress.com/2013/05/300688_10150339844429260_820764259_8021172_208942660_n.jpg",viewHolder.campImage, imageLoaderOptions);
        loadImage(camp.getThumbnailId(), viewHolder.campImage, position);
    }

    /**
     * Load an image (will check in the imagesMap, and otherwise go fetch it from the server)
     * @param imageId ID of the image
     * @param imageView View for the image to be displayed in
     * @param position position in the list
     */
    private void loadImage(int imageId, ImageView imageView, int position){
        BitmapDrawable drawable = imagesMap.get(position);
        if(drawable == null){
            if(imageId > 0){
                retrieveImage(imageId, imageView, position);
            }else{
                imageView.setImageDrawable(mContext.getResources().getDrawable(R.drawable.stub));
            }
        }else{
            imageView.setImageDrawable(drawable);
        }

        //Load next images
        int firstViewVisible = layoutManager.findFirstVisibleItemPosition();
        int lastViewVisible = layoutManager.findLastVisibleItemPosition();

        //In front
        for(int i=firstViewVisible-1; i >= firstViewVisible - ROWS_TO_CACHE_IMAGES && i >= 0; i--){
            int thumbId = camps.get(i).getThumbnailId();
            if(thumbId > 0){
                retrieveImage(thumbId, null, i);
            }
        }

        //In back
        for(int i=lastViewVisible+1; i <= lastViewVisible + ROWS_TO_CACHE_IMAGES && i < camps.size(); i++){
            int thumbId = camps.get(i).getThumbnailId();
            if(thumbId > 0){
                retrieveImage(thumbId, null, i);
            }
        }

        //Remove images from map that are no longer in range
        //In front
        for(int i=minInMap; i < firstViewVisible - ROWS_TO_CACHE_IMAGES; i++){
            imagesMap.remove(i);
        }
        //In back
        for(int i=maxInMap; i > lastViewVisible+ROWS_TO_CACHE_IMAGES; i--){
            imagesMap.remove(i);
        }

        minInMap = firstViewVisible - ROWS_TO_CACHE_IMAGES;
        maxInMap = lastViewVisible + ROWS_TO_CACHE_IMAGES;
    }

    /**
     * retrieve an image from the server
     * @param imageId ID of the image
     * @param imageView View for the image
     * @param position Position in de list
     */
    private void retrieveImage(int imageId, ImageView imageView, final int position){
        if(imagesMap.containsKey(position)){
            return;
        }
        ImageLoadingListener listener = new ImageLoadingListener() {
            @Override
            public void onLoadingStarted(String imageUri, View view) {

            }

            @Override
            public void onLoadingFailed(String imageUri, View view, FailReason failReason) {

            }

            @Override
            public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                if(position < minInMap || position > maxInMap){
                    return;
                }
                if(!imagesMap.containsKey(position)){
                    BitmapDrawable d;
                    if(view == null){
                        d = new BitmapDrawable(mContext.getResources(), loadedImage);
                    }else{
                        d = (BitmapDrawable) ((ImageView) view).getDrawable();
                    }

                    imagesMap.put(position, d);
                }
            }

            @Override
            public void onLoadingCancelled(String imageUri, View view) {

            }
        };

        String url = ApiConnection.URL + "api/images/" +imageId;

        if(imageView != null){
            ImageLoader.getInstance().displayImage(url,
                    imageView, imageLoaderOptions, listener);
        }else{
            ImageLoader.getInstance().loadImage(url, imageLoaderOptions, listener);
        }
    }

    public void clearImagesMap(){
        imagesMap.clear();
    }

    @Override
    public int getItemCount() {
        return camps == null ? 0 : camps.size();
    }

    public Camp getItem(int position){
       return camps.get(position);
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public View container;

        public TextView campName;
        public TextView location;
        public TextView campDate;
        public TextView price;
        public TextView age;
        public TextView campPeriod;
        public ImageView campImage;
        public ImageView periodeImage;
        public RelativeLayout periodImageContainer;

        public ViewHolder(View itemView) {
            super(itemView);
            container = itemView.findViewById(R.id.campContainer);
            campName = (TextView) itemView.findViewById(R.id.campName);
            location =  (TextView) itemView.findViewById(R.id.lbl_dest);
            campDate = (TextView) itemView.findViewById(R.id.lbl_date);
            price = (TextView) itemView.findViewById(R.id.lbl_price);
            age= (TextView) itemView.findViewById(R.id.lbl_age);
            campPeriod = (TextView) itemView.findViewById(R.id.lbl_period);
            campImage= (ImageView) itemView.findViewById(R.id.campImage);
            periodeImage=(ImageView) itemView.findViewById(R.id.periodeImage);
            periodImageContainer = (RelativeLayout) itemView.findViewById(R.id.periodImageContainer);
        }
    }


}




Java Source Code List

com.example.jens.myapplication.ApplicationTest.java
com.example.jens.myapplication.account.FragmentAccount.java
com.example.jens.myapplication.account.LoginDialogActivity.java
com.example.jens.myapplication.account.LoginFragment.java
com.example.jens.myapplication.account.PersonsFragment.java
com.example.jens.myapplication.account.ProfileFragment.java
com.example.jens.myapplication.account.RegisterDialogActivity.java
com.example.jens.myapplication.account.RegisterFragment.java
com.example.jens.myapplication.account.TabsPagerAdapter.java
com.example.jens.myapplication.activities.ActivityDetailActivity.java
com.example.jens.myapplication.activities.ActivityDetailFragment.java
com.example.jens.myapplication.activities.FragmentListActivities.java
com.example.jens.myapplication.activities.JoetzActivitiesAdapter.java
com.example.jens.myapplication.activities.JoetzActivity.java
com.example.jens.myapplication.activities.StartActivity.java
com.example.jens.myapplication.apimanager.ApiConnection.java
com.example.jens.myapplication.apimanager.CancellableTask.java
com.example.jens.myapplication.apimanager.JSONParsingTask.java
com.example.jens.myapplication.apimanager.PostRequestTask.java
com.example.jens.myapplication.apimanager.RequestParams.java
com.example.jens.myapplication.apimanager.SimpleRequestTask.java
com.example.jens.myapplication.apimanager.filter.PersonFilter.java
com.example.jens.myapplication.apimanager.manager.ActivityManager.java
com.example.jens.myapplication.apimanager.manager.AuthToken.java
com.example.jens.myapplication.apimanager.manager.BookingManager.java
com.example.jens.myapplication.apimanager.manager.CampManager.java
com.example.jens.myapplication.apimanager.manager.ImageManager.java
com.example.jens.myapplication.apimanager.manager.LoginManager.java
com.example.jens.myapplication.apimanager.manager.ProfileManager.java
com.example.jens.myapplication.apimanager.manager.RegisterManager.java
com.example.jens.myapplication.booking.AddPersonActivity.java
com.example.jens.myapplication.booking.BookingActivity.java
com.example.jens.myapplication.booking.BookingBondFragment.java
com.example.jens.myapplication.booking.BookingContactFragment.java
com.example.jens.myapplication.booking.BookingExtraFragment.java
com.example.jens.myapplication.booking.BookingParticipantsFragment.java
com.example.jens.myapplication.bookingHistory.BookingHistoryDetailActivity.java
com.example.jens.myapplication.bookingHistory.BookingHistoryDetailFragment.java
com.example.jens.myapplication.bookingHistory.BookingHistoryOverviewFragment.java
com.example.jens.myapplication.bookingHistory.BookingsAdapter.java
com.example.jens.myapplication.calendar.FragmentCalendar.java
com.example.jens.myapplication.calendar.KalenderAdapter.java
com.example.jens.myapplication.calendar.KalenderItem.java
com.example.jens.myapplication.camps.CampAdapter.java
com.example.jens.myapplication.camps.CampDetailActivity.java
com.example.jens.myapplication.camps.CampFilterActivity.java
com.example.jens.myapplication.camps.FilterFragment.java
com.example.jens.myapplication.camps.FragmentListCamps.java
com.example.jens.myapplication.camps.ImageActivity.java
com.example.jens.myapplication.camps.ImagePagerFragment.java
com.example.jens.myapplication.camps.InfoFragment.java
com.example.jens.myapplication.camps.PhotoFragment.java
com.example.jens.myapplication.camps.RecyclerItemClickListener.java
com.example.jens.myapplication.camps.SquareImageView.java
com.example.jens.myapplication.camps.TabsPagerAdapter.java
com.example.jens.myapplication.domain.BookingStatus.java
com.example.jens.myapplication.domain.Booking.java
com.example.jens.myapplication.domain.Camp.java
com.example.jens.myapplication.domain.PeriodCategory.java
com.example.jens.myapplication.domain.Person.java
com.example.jens.myapplication.domain.UserRole.java
com.example.jens.myapplication.domain.User.java
com.example.jens.myapplication.domain.binding.PersonValidatorBinding.java
com.example.jens.myapplication.domain.binding.UserValidatorBinding.java
com.example.jens.myapplication.domain.binding.ValidatorBinding.java
com.example.jens.myapplication.domain.converter.ErrorConverter.java
com.example.jens.myapplication.domain.converter.MyStringParser.java
com.example.jens.myapplication.domain.converter.UnixDateTimeConverter.java
com.example.jens.myapplication.domain.validator.PersonValidator.java
com.example.jens.myapplication.domain.validator.UserValidator.java
com.example.jens.myapplication.domain.validator.Validator.java
com.example.jens.myapplication.home.AanbevolenAdapter.java
com.example.jens.myapplication.home.HomeFragment.java
com.example.jens.myapplication.home.ImageSlideAdapter.java
com.example.jens.myapplication.home.Reclame.java
com.example.jens.myapplication.lib.RangeSeekBar.java
com.example.jens.myapplication.main.MainActivity.java
com.example.jens.myapplication.main.NavDrawerAdapter.java
com.example.jens.myapplication.main.NavDrawerItem.java
com.example.jens.myapplication.main.NavMenuItem.java
com.example.jens.myapplication.main.NavMenuSection.java
com.example.jens.myapplication.sam.JoetzApplication.java
com.example.jens.myapplication.sam.MyFragment.java
com.example.jens.myapplication.sam.MySaveableFragment.java
com.example.jens.myapplication.util.AESEncryption.java
com.example.jens.myapplication.util.ActivityUtils.java
com.example.jens.myapplication.util.BindingUtils.java
com.example.jens.myapplication.util.DateTimeStringConverter.java
com.example.jens.myapplication.util.JsonUtils.java
com.example.jens.myapplication.util.MyMonths.java
com.example.jens.myapplication.util.StringUtils.java
com.example.jens.myapplication.view.MyLinearLayout.java