您好,登錄后才能下訂單哦!
小編給大家分享一下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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。