package me.com.photopicker.adapter;

import android.app.Activity;
import android.net.Uri;
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 com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

import baidu.com.commontools.utils.MobileInfo;
import me.com.photopicker.R;
import me.com.photopicker.model.Photo;

public class ThumbnailAdapter extends RecyclerView.Adapter<ThumbnailAdapter.ThumbnailViewHolder> {

    private Activity mActivity;
    private List<Photo> mPhotoList;
    private List<String> mSelectList;

    public ThumbnailAdapter(Activity activity) {
        mActivity = activity;
        mSelectList = new ArrayList<>();
    }

    @Override
    public ThumbnailViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(mActivity);
        View view = inflater.inflate(R.layout.thumbnail_layout, parent, false);
        return new ThumbnailViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ThumbnailViewHolder holder, int position) {
        if (mPhotoList != null) {
            holder.setThumbnail(mPhotoList.get(position));
            holder.thumbnail.setOnClickListener(holder);
        }
    }

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

    public void setData(List<Photo> photoList) {
        if (photoList != null) {
            mPhotoList = photoList;
            notifyDataSetChanged();
        }
    }

    public List<String> getSelectList() {
        return mSelectList;
    }

    class ThumbnailViewHolder extends RecyclerView.ViewHolder
            implements View.OnClickListener {

        public ImageView thumbnail;
        public ImageView selectedTip;

        public ThumbnailViewHolder(View itemView) {
            super(itemView);
            thumbnail = (ImageView) itemView.findViewById(R.id.photo_thumbview);
            selectedTip = (ImageView) itemView.findViewById(R.id.photo_thumbview_selected);
            int width = MobileInfo.getScreenMetrics(mActivity).widthPixels / 3;
            thumbnail.setLayoutParams(new RelativeLayout.LayoutParams(width, width));
        }

        public void setThumbnail(Photo photo) {
            Uri uri = new Uri.Builder().scheme("file").path(photo.path).build();

            Glide.with(mActivity)
                    .load(uri)
                    .placeholder(R.mipmap.default_error)
                    .thumbnail(0.3f)
                    .error(R.mipmap.default_error)
                    .into(thumbnail);
        }

        @Override
        public void onClick(View v) {
        }
    }
}

@@ -126,24 +126,49 @@
 cyclerView;%0A
+import android.util.Log;%0A
 import andro
@@ -618,24 +618,83 @@
 wHolder%3E %7B%0A%0A
+    private static final String TAG = %22ThumbnailAdapter%22;%0A%0A
     private 
@@ -786,16 +786,48 @@
 ectList;
+%0A    private int%5B%5D mSelectTipId;
 %0A%0A    pu
@@ -936,24 +936,153 @@
 ayList%3C%3E();%0A
+        mSelectTipId = new int%5B%5D %7B%0A                R.mipmap.photo_selected,%0A                R.mipmap.photo_unselected%0A        %7D;%0A
     %7D%0A%0A    @
@@ -1170,24 +1170,67 @@
 viewType) %7B%0A
+        Log.d(TAG, %22onCreateViewHolder%22);%0A%0A
         Layo
@@ -1503,24 +1503,89 @@
 position) %7B%0A
+        Log.d(TAG, %22onBindViewHolder, position : %22 + position);%0A%0A
         if (
@@ -1666,65 +1666,18 @@
 ion)
-);%0A            holder.thumbnail.setOnClickListener(holder
+, position
 );%0A 
@@ -2234,16 +2234,46 @@
 ctedTip;
+%0A        private int position;
 %0A%0A      
@@ -2351,89 +2351,8 @@
 w);%0A
-            thumbnail = (ImageView) itemView.findViewById(R.id.photo_thumbview);%0A
     
@@ -2439,16 +2439,17 @@
 ected);%0A
+%0A
         
@@ -2520,16 +2520,97 @@
 ls / 3;%0A
+            thumbnail = (ImageView) itemView.findViewById(R.id.photo_thumbview);%0A
         
@@ -2699,58 +2699,159 @@
     
-%7D%0A%0A        public void setThumbnail(Photo photo) %7B
+    thumbnail.setOnClickListener(this);%0A        %7D%0A%0A        public void setThumbnail(Photo photo, int position) %7B%0A            this.position = position;%0A
 %0A   
@@ -3076,73 +3076,73 @@
    .
-thumbnail(0.3f)%0A                    .error(R.mipmap.default_error
+error(R.mipmap.default_error)%0A                    .thumbnail(0.3f
 )%0A  
@@ -3240,24 +3240,382 @@
 k(View v) %7B%0A
+            Photo photo = mPhotoList.get(position);%0A            if (mSelectList.contains(photo.path)) %7B%0A                selectedTip.setImageResource(mSelectTipId%5B1%5D);%0A                mSelectList.remove(photo.path);%0A            %7D else %7B%0A                selectedTip.setImageResource(mSelectTipId%5B0%5D);%0A                mSelectList.add(photo.path);%0A            %7D%0A
         %7D%0A  

change : handle thumbnail OnClick event.