您好,登錄后才能下訂單哦!
本文實例為大家分享了RecyclerView實現查看更多及收起的具體代碼,供大家參考,具體內容如下
三個list:
realList 真實list
hideList 隱藏時的list
openList 展開時的list
做法就是
判斷適配器條目小于4(可任意)時,將適配器list設置為真實list
判斷適配器條目大于4(可任意)時,將適配器hideList設置為真實list的前三個條目+查看更多;將適配器openList設置為真實list+收起
適配器代碼
public class LuckyCodeAdapter extends RecyclerView.Adapter<LuckyCodeAdapter.LuckyCodeViewHolder> { private Context context; private List<String> list; private boolean isHide;//隱藏 private boolean isOpen;//展開 public LuckyCodeAdapter(Context context) { this.context = context; } @Override public LuckyCodeViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View v = LayoutInflater.from(context).inflate(R.layout.item_tosanpup_lucky_code, parent, false); return new LuckyCodeViewHolder(v); } @Override public void onBindViewHolder(LuckyCodeViewHolder holder, final int position) { holder.txtLuckyCode.setText(list.get(position)); if (hideOrShowCallBack != null) { holder.txtLuckyCode.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (position == list.size() - 1) { if (isOpen) { hideOrShowCallBack.hide(); return; } if (isHide) { hideOrShowCallBack.open(); return; } } } }); } } @Override public int getItemCount() { return list == null ? 0 : list.size(); } //隱藏 public void setHideList(List<String> newList) { this.list = newList; notifyDataSetChanged(); this.isHide = true; this.isOpen = false; } //展開 public void setOpenList(List<String> openList) { this.list = openList; this.isOpen = true; this.isHide = false; notifyDataSetChanged(); } //不需要隱藏/展開 public void setRealList(List<String> realList) { this.list = realList; notifyDataSetChanged(); this.isHide = false; this.isOpen = false; } //清除數據 public void clearData() { if (list != null) { this.list.clear(); notifyDataSetChanged(); } } class LuckyCodeViewHolder extends RecyclerView.ViewHolder { TextView txtLuckyCode;//幸運號碼 public LuckyCodeViewHolder(View itemView) { super(itemView); txtLuckyCode = (TextView) itemView; } } private HideOrShowCallBack hideOrShowCallBack; public void setHideOrShowCallBack(HideOrShowCallBack hideOrShowCallBack) { this.hideOrShowCallBack = hideOrShowCallBack; } public interface HideOrShowCallBack { void hide(); void open(); } }
//luckyList為真實list,判斷是否需要隱藏 if (luckyList.size() > 4) { luckyCodeHideList = new ArrayList<>(); luckyCodeOpenList = new ArrayList<>(); for (int i = 0; i < luckyList.size(); i++) { luckyCodeOpenList.add(luckyList.get(i)); } luckyCodeOpenList.add("收起"); for (int i = 0; i < 3; i++) { luckyCodeHideList.add(luckyList.get(i)); } luckyCodeHideList.add("查看更多"); luckyCodeAdapter.setHideList(luckyCodeHideList); } else { luckyCodeAdapter.setRealList(luckyList); }
設置監聽
luckyCodeAdapter.setHideOrShowCallBack(new LuckyCodeAdapter.HideOrShowCallBack() { @Override public void hide() { luckyCodeAdapter.setHideList(luckyCodeHideList); } @Override public void open() { luckyCodeAdapter.setOpenList(luckyCodeOpenList); } });
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。