上传多张图片的流程与思想

这里写图片描述
如图效果。使用gridview显示选取的图片,在getItem中根据图片数判断返回的item数量如果未达到最大数量就加一用于显示带加号的图片,如果达到最大数则返回原始item数量!

class MyGridViewAdapter extends BaseAdapter {

    Context mContext;
    public MyGridViewAdapter(Context mContext) {
        this.mContext = mContext;
    }

    @Override
    public int getCount() {
        if (imgList.size() == 5) {
            return 5;
        } else {
            return imgList.size() + 1;
        }
    }

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

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

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        MyHolder holder = null;
        if (view == null) {
            view = LayoutInflater.from(mContext).inflate(R.layout.upload_img_item, null);
            holder = new MyHolder();
            holder.img = (ImageView) view.findViewById(R.id.iv_add);
            holder.imgDelete = (ImageView) view.findViewById(R.id.iv_delete);
            holder.progressBar = (ProgressBar) view.findViewById(R.id.progress);
            view.setTag(holder);
        } else {
            holder = (MyHolder) view.getTag();
        }
        if (imgList.size() == i) {//如果imgList.size为5,i最大只能到4
            holder.progressBar.setVisibility(View.GONE);
            holder.imgDelete.setVisibility(View.GONE);
            Glide.with(mContext).load(R.drawable.ic_salary2_11).dontAnimate().into(holder.img);
            if (i == 5) {//如果imgList.size为5,i最大只能到4,以防万一,多加一步判断
                holder.img.setVisibility(View.GONE);
            }
            //点击选取图片
            holder.img.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (ContextCompat.checkSelfPermission(mContext, Manifest.permission.READ_EXTERNAL_STORAGE)
                            != PackageManager.PERMISSION_GRANTED) {
                        //申请READ_EXTERNAL_STORAGE权限
                        ActivityCompat.requestPermissions(PromotiongRaphicActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                                READ_EXTERNAL_STORAGE_REQUEST_CODE);
                    }else {

                        Matisse.from(PromotiongRaphicActivity.this)
                                .choose(MimeType.allOf())
                                .countable(true)
                                .maxSelectable(5 - imgList.size())

                                .gridExpectedSize(getResources().getDimensionPixelSize(R.dimen.size_100dp))
                                .restrictOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED)
                                .thumbnailScale(1f)
                                .imageEngine(new GlideEngine())
                                .theme(R.style.Matisse_Zhihu)
                                .forResult(PICKIMAGE);
                    }
                }
            });
        } else {

             File imgFile = new File(CommonUtils.getRealFilePathFromUri(mContext,imgList.get(i)));
            Glide.with(mContext).load(imgFile).dontAnimate().into(holder.img);
            holder.imgDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                  //删除图片
                    imgList.remove(i);
                    notifyDataSetChanged();
                }
            });
            holder.img.setClickable(false);
        }
        return view;
    }
}

class MyHolder {
    ImageView img;
    ImageView imgDelete;
    ProgressBar progressBar;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值