仿微信图片上传,带加号,且超过最大数隐藏
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; ViewGroup.LayoutParams layoutParams = holder.itemView.getLayoutParams(); layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT; layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT; if (holder instanceof ItemViewHolderAdd) { ItemViewHolderAdd itemViewHolderAdd = (ItemViewHolderAdd) holder; if (position >= mMaxAlbum) { layoutParams.width = 0; layoutParams.height = 0; 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 { //图片展示界面 } } }); }