最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2018年01月02日 08:40:59   作者:尚尚一枚  
這篇文章主要為大家詳細(xì)介紹了RecyclerView實(shí)現(xiàn)查看更多及收起,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(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í)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

南陵县| 永靖县| 万州区| 荃湾区| 英山县| 上蔡县| 绥滨县| 新田县| 深泽县| 文化| 乌拉特中旗| 娄底市| 永寿县| 玛纳斯县| 丰镇市| 徐闻县| 望奎县| 望奎县| 宁乡县| 乐都县| 行唐县| 高雄县| 朝阳区| 花垣县| 墨竹工卡县| 剑川县| 元氏县| 四平市| 湖北省| 桐梓县| 沙河市| 科技| 肃宁县| 呼伦贝尔市| 郎溪县| 晋宁县| 丰城市| 吉安县| 开江县| 岑溪市| 杭州市|