Android仿微信图片上传带加号且超过最大数隐藏功能

本文介绍了一个用于图片选择器的功能性适配器的实现细节。该适配器支持不同类型的图片展示,包括普通图片展示和添加图片按钮。文中详细解释了如何使用RecyclerView来显示图片列表,并提供了自定义ViewHolder来区分图片类型的方法。此外,还介绍了如何通过监听器实现图片删除和点击事件。
摘要由CSDN通过智能技术生成
private static int TYPE_ADD = 0;//添加图片
private static int TYPE_COMMON = 1;//普通图片展示

private Context context;
private LayoutInflater mLayoutInflater;
//data
private int mMaxAlbum;//最大选择图片的数量
private List<String> mStringList=new ArrayList<>();//图片url集合
IdeletePicCallBack ideletePicCallBack;

public interface IdeletePicCallBack {
    void getPicPathCount(View view, int position, List<String> mStringList);
}

public void setOnClickDeletePic(IdeletePicCallBack ideletePicCallBack) {
    this.ideletePicCallBack = ideletePicCallBack;
}


public PhotoAdapters(Context context, List<String> mStringList, int maxAlbum) {
    this.context = context;
    this.mStringList = mStringList;
    this.mMaxAlbum = maxAlbum;
    this.mLayoutInflater = LayoutInflater.from(context);
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    if (viewType == TYPE_ADD) {
        return new ItemViewHolderAdd(mLayoutInflater.inflate(R.layout.rephoto_item, parent, false));
    } else {
        return new ItemViewHolderCommon(mLayoutInflater.inflate(R.layout.choosepic_item, parent, false));
    }

}

ItemViewHolderAdd itemViewHolderAdd;//加号的ViewHolder
ItemViewHolderCommon itemViewHolderCommon;//

@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
    View itemView = null;
    if (holder instanceof ItemViewHolderAdd) {
        itemViewHolderAdd = (ItemViewHolderAdd) holder;
        if (position >= mMaxAlbum) {
            itemViewHolderAdd.itemView.setVisibility(View.GONE);
        } else {
            itemViewHolderAdd.tvNum.setImageResource(R.mipmap.add_pic);
            itemViewHolderAdd.itemView.setVisibility(View.VISIBLE);
            itemView = ((ItemViewHolderAdd) holder).itemView;
        }
    } else if (holder instanceof ItemViewHolderCommon) {
        itemViewHolderCommon = (ItemViewHolderCommon) holder;
        String url = mStringList.get(position);
        if (url.contains("/")) {
            Glide.with(context).load(url).into(((ItemViewHolderCommon) holder).ivCommon);
        } else {
            ImageLoader.getInstance(context).setImages(((ItemViewHolderCommon) holder).ivCommon, ImageLoader.getImgUrl_180(url));
        }
        itemView = ((ItemViewHolderCommon) holder).itemView;
        if (ideletePicCallBack != null && null != itemView) {
            itemViewHolderCommon.ivDeletePic.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mStringList.remove(position);
                    notifyDataSetChanged();
                    int position = holder.getLayoutPosition();
                    ideletePicCallBack.getPicPathCount(holder.itemView, position, mStringList);
                }
            });
        }
    }

    if (mOnItemClickListener != null && null != itemView) {
        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = holder.getLayoutPosition();
                mOnItemClickListener.onItemClick(holder.itemView, position);
            }
        });
    }
}

private OnItemClickListener mOnItemClickListener;

public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
    mOnItemClickListener = onItemClickListener;
}

public interface OnItemClickListener {
    void onItemClick(View view, int position);
}

@Override
public int getItemCount() {
    return mStringList.size() + 1;//加一代表最后一个添加图片按钮
}

@Override
public int getItemViewType(int position) {
    return position == mStringList.size() ? TYPE_ADD : TYPE_COMMON;
}

public static class ItemViewHolderAdd extends RecyclerView.ViewHolder {
    private ImageView tvNum;

    public ItemViewHolderAdd(View itemView) {
        super(itemView);
        tvNum = (ImageView) itemView.findViewById(R.id.choose_pic);
    }
}

public static class ItemViewHolderCommon extends RecyclerView.ViewHolder {
    private ImageView ivCommon;
    private ImageView ivDeletePic;

    public ItemViewHolderCommon(View itemView) {
        super(itemView);
        ivCommon = (ImageView) itemView.findViewById(R.id.image);
        ivDeletePic = (ImageView) itemView.findViewById(R.id.delete);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值