本文实例为大家分享了RecyclerVIEw实现查看更多及收起的具体代码,供大家参考,具体内容如下
三个List:
realList 真实List
hIDeList 隐藏时的List
openList 展开时的List
做法就是
判断适配器条目小于4(可任意)时,将适配器List设置为真实List
判断适配器条目大于4(可任意)时,将适配器hIDeList设置为真实List的前三个条目+查看更多;将适配器openList设置为真实List+收起
适配器代码
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(); }}
//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); }
设置监听
luckyCodeAdapter.setHIDeOrShowCallBack(new LuckyCodeAdapter.HIDeOrShowCallBack() { @OverrIDe public voID hIDe() { luckyCodeAdapter.setHIDeList(luckyCodeHIDeList); } @OverrIDe public voID open() { luckyCodeAdapter.setopenList(luckyCodeOpenList); } });
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
总结以上是内存溢出为你收集整理的RecyclerView实现查看更多及收起全部内容,希望文章能够帮你解决RecyclerView实现查看更多及收起所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)