小编给大家分享一下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如何实现仿微信图片上传带加号且超过最大数隐藏功能”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。