GridViewAdapte图片上传

package com.example.yifuhua.apicture.module.personal.view.activity;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.example.yifuhua.apicture.R;

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

import butterknife.ButterKnife;
import butterknife.InjectView;

/**图片评价的适配器
 * Created by yifuhua on 16/8/10.
 */
public class RateGridViewAdapter extends BaseAdapter {
    private List imgPaths = new ArrayList<>();//图片地址集合
    private Context context;
    private OnItemClickListen mOnItemClickListen;
    public RateGridViewAdapter(Context context , List imgPaths) {
        this.context = context;
        this.imgPaths = imgPaths;

    }

    public void setListData(List imgPaths){
        this.imgPaths = imgPaths;
    }

    @Override
    public int getCount() {
        if(imgPaths.size() == 3){
            return 3;
        }
        return imgPaths.size()+1 ;
    }

    @Override
    public Object getItem(int position) {
        return  null;
    }

    @Override
    public long getItemId(int position) {
        return  0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_grid_view, parent, false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        if (position == imgPaths.size()) {
            //显示加
            viewHolder.img.setImageResource(R.mipmap.ico_add_pic);
            viewHolder.ivSelector.setImageResource(0);
            viewHolder.img.setOnClickListener(v -> {
                if(mOnItemClickListen != null){
                    mOnItemClickListen.OnItemClickAdd();
                }
            });
        } else {
            viewHolder.img.setOnClickListener(null);
//            ImageHelper.initImage("file:///" + imgPaths.get(position), viewHolder.img);
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize = 4;
            Bitmap bitmap = BitmapFactory.decodeFile(imgPaths.get(position),options);
            viewHolder.img.setImageBitmap(bitmap);
            viewHolder.ivSelector.setImageResource(R.mipmap.ico_pic_cancle_40px);
            viewHolder.ivSelector.setOnClickListener(v -> {
                mOnItemClickListen.OnItemClickDel(position);
                Log.d("RateGridViewAdapter", "position:" + position);

            });
        }
        return convertView;
    }


    class ViewHolder {
        @InjectView(R.id.img)
        ImageView img;
        @InjectView(R.id.iv_selector)
        ImageView ivSelector;
        @InjectView(R.id.mask)
        View mask;

        ViewHolder(View view) {
            ButterKnife.inject(this, view);
        }
    }

    public interface OnItemClickListen{
        void OnItemClickAdd();
        void OnItemClickDel(int position);

    }

    public void setOnItemClickListen(OnItemClickListen onItemClickListen){
        this.mOnItemClickListen = onItemClickListen;
    }
}


你可能感兴趣的:(代码库)