前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >RecyclerView实现查看更多及收起

RecyclerView实现查看更多及收起

作者头像
砸漏
发布2020-10-29 23:12:13
1.4K0
发布2020-10-29 23:12:13
举报
文章被收录于专栏:恩蓝脚本

本文实例为大家分享了RecyclerView实现查看更多及收起的具体代码,供大家参考,具体内容如下

三个list:

realList 真实list hideList 隐藏时的list openList 展开时的list

做法就是

判断适配器条目小于4(可任意)时,将适配器list设置为真实list 判断适配器条目大于4(可任意)时,将适配器hideList设置为真实list的前三个条目+查看更多;将适配器openList设置为真实list+收起

适配器代码

代码语言:javascript
复制
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();
 }
}
代码语言:javascript
复制
//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);
  }

设置监听

代码语言:javascript
复制
luckyCodeAdapter.setHideOrShowCallBack(new LuckyCodeAdapter.HideOrShowCallBack() {
   @Override
   public void hide() {
    luckyCodeAdapter.setHideList(luckyCodeHideList);
   }

   @Override
   public void open() {
    luckyCodeAdapter.setOpenList(luckyCodeOpenList);
   }
  });

以上就是本文的全部内容,希望对大家的学习有所帮助。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-09-11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档