Android仿微信图片上传带加号且超过最大数隐藏功能
1、仿照微信空间上传图片,显示图片数量以及超过最大,上传按钮隐藏功能
2、上效果图
3、上代码,主要是Adapter类
/** * Created by zhangyinlei on 2018/3/2 0002. */ public class AlbumSelectedShowAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { 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;//图片url集合 public AlbumSelectedShowAdapter(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.item_album_selected_add, parent, false)); } else { return new ItemViewHolderCommon(mLayoutInflater.inflate(R.layout.item_album_selected_common, parent, false)); } } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { View itemView = null; if (holder instanceof ItemViewHolderAdd) { ItemViewHolderAdd itemViewHolderAdd = (ItemViewHolderAdd) holder; if (position >= mMaxAlbum) { itemViewHolderAdd.itemView.setVisibility(View.GONE); } else { itemViewHolderAdd.tvNum.setText(position + "/" + mMaxAlbum); itemViewHolderAdd.itemView.setVisibility(View.VISIBLE); itemView = ((ItemViewHolderAdd) holder).itemView; } } else if (holder instanceof ItemViewHolderCommon) { String url = mStringList.get(position); Glide.with(context).load(url).apply(RequestOptions.centerCropTransform()).transition(withCrossFade()).into(((ItemViewHolderCommon) holder).ivCommon); itemView = ((ItemViewHolderCommon) holder).itemView; } 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); } }); } } @Override public int getItemViewType(int position) { return position == mStringList.size() ? TYPE_ADD : TYPE_COMMON; } @Override public int getItemCount() { return mStringList.size() + 1;//加一代表最后一个添加图片按钮 } public static class ItemViewHolderAdd extends RecyclerView.ViewHolder { private TextView tvNum; public ItemViewHolderAdd(View itemView) { super(itemView); tvNum = itemView.findViewById(R.id.tv_album_selected_num); } } public static class ItemViewHolderCommon extends RecyclerView.ViewHolder { private ImageView ivCommon; public ItemViewHolderCommon(View itemView) { super(itemView); ivCommon = itemView.findViewById(R.id.iv_album_selected); } } private OnItemClickListener mOnItemClickListener; public void setOnItemClickListener(OnItemClickListener onItemClickListener) { mOnItemClickListener = onItemClickListener; } public interface OnItemClickListener { void onItemClick(View view, int position); } }
在Activity中使用,recycleView
mStringList = new ArrayList<>(); mAlbumSelectedShowAdapter = new AlbumSelectedShowAdapter(this, mStringList, mMaxAlbum); mRvAlbumSelected.setLayoutManager(new GridLayoutManager(this, 4)); mRvAlbumSelected.setHasFixedSize(true); mRvAlbumSelected.setAdapter(mAlbumSelectedShowAdapter); mAlbumSelectedShowAdapter.setOnItemClickListener(new AlbumSelectedShowAdapter.OnItemClickListener() { @Override public void onItemClick(View view, int position) { if (mStringList.size() == position) { //选择相册功能 AlbumUtils.choosePicture(SuggestionActivity.this, false, mMaxAlbum - mStringList.size(), true); } else { //图片展示界面 } } }); }
总结
以上所述是小编给大家介绍的Android仿微信图片上传带加号且超过最大数隐藏功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对我们网站的支持!
您可能感兴趣的文章:
- Android中使用GridView实现仿微信图片上传功能(附源代码)
赞 (0)