您好,登錄后才能下訂單哦!
本文實(shí)例為大家分享了RecyclerView實(shí)現(xiàn)查看更多及收起的具體代碼,供大家參考,具體內(nèi)容如下
三個(gè)list:
realList 真實(shí)list
hideList 隱藏時(shí)的list
openList 展開(kāi)時(shí)的list
做法就是
判斷適配器條目小于4(可任意)時(shí),將適配器list設(shè)置為真實(shí)list
判斷適配器條目大于4(可任意)時(shí),將適配器hideList設(shè)置為真實(shí)list的前三個(gè)條目+查看更多;將適配器openList設(shè)置為真實(shí)list+收起
適配器代碼
public class LuckyCodeAdapter extends RecyclerView.Adapter<LuckyCodeAdapter.LuckyCodeViewHolder> { private Context context; private List<String> list; private boolean isHide;//隱藏 private boolean isOpen;//展開(kāi) 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; } //展開(kāi) public void setOpenList(List<String> openList) { this.list = openList; this.isOpen = true; this.isHide = false; notifyDataSetChanged(); } //不需要隱藏/展開(kāi) public void setRealList(List<String> realList) { this.list = realList; notifyDataSetChanged(); this.isHide = false; this.isOpen = false; } //清除數(shù)據(jù) public void clearData() { if (list != null) { this.list.clear(); notifyDataSetChanged(); } } class LuckyCodeViewHolder extends RecyclerView.ViewHolder { TextView txtLuckyCode;//幸運(yùn)號(hào)碼 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為真實(shí)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); }
設(shè)置監(jiān)聽(tīng)
luckyCodeAdapter.setHideOrShowCallBack(new LuckyCodeAdapter.HideOrShowCallBack() { @Override public void hide() { luckyCodeAdapter.setHideList(luckyCodeHideList); } @Override public void open() { luckyCodeAdapter.setOpenList(luckyCodeOpenList); } });
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。