本文實(shí)例為大家分享了RecyclerView實(shí)現(xiàn)查看更多及收起的具體代碼,供大家參考,具體內(nèi)容如下
棗強(qiáng)網(wǎng)站建設(shè)公司成都創(chuàng)新互聯(lián),棗強(qiáng)網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為棗強(qiáng)數(shù)千家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\外貿(mào)網(wǎng)站制作要多少錢,請(qǐng)找那個(gè)售后服務(wù)好的棗強(qiáng)做網(wǎng)站的公司定做!
三個(gè)list:
realList 真實(shí)list
hideList 隱藏時(shí)的list
openList 展開時(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{ private Context context; private List 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 newList) { this.list = newList; notifyDataSetChanged(); this.isHide = true; this.isOpen = false; } //展開 public void setOpenList(List openList) { this.list = openList; this.isOpen = true; this.isHide = false; notifyDataSetChanged(); } //不需要隱藏/展開 public void setRealList(List 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)聽
luckyCodeAdapter.setHideOrShowCallBack(new LuckyCodeAdapter.HideOrShowCallBack() { @Override public void hide() { luckyCodeAdapter.setHideList(luckyCodeHideList); } @Override public void open() { luckyCodeAdapter.setOpenList(luckyCodeOpenList); } });
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。