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<String> imgPaths = new ArrayList<>();//图片地址集合
    private Context context;
    private OnItemClickListen mOnItemClickListen;
    public RateGridViewAdapter(Context context , List<String> imgPaths) {
        this.context = context;
        this.imgPaths = imgPaths;

    }

    public void setListData(List<String> 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;
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值