国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > Android > 正文

RecyclerView實(shí)現(xiàn)查看更多及收起

2019-12-12 01:21:29
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

本文實(shí)例為大家分享了RecyclerView實(shí)現(xiàn)查看更多及收起的具體代碼,供大家參考,具體內(nèi)容如下

這里寫(xiě)圖片描述

三個(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í)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 宁蒗| 万年县| 英德市| 桐城市| 印江| 文安县| 韶关市| 大田县| 从化市| 德清县| 重庆市| 姜堰市| 志丹县| 甘洛县| 平远县| 郎溪县| 灵武市| 双牌县| 灵寿县| 弥渡县| 酒泉市| 手机| 油尖旺区| 洛宁县| 双城市| 监利县| 洪湖市| 玉环县| 白山市| 榆树市| 陇西县| 阳春市| 锡林郭勒盟| 宁强县| 晋江市| 洛隆县| 盐津县| 涪陵区| 米易县| 利津县| 永城市|