一个RecyclerView实现多级折叠列表(TreeRecyc
转载请标明出处:http://www.jianshu.com/p/e96912dd0d06
本文出自:Jlanglang
前言
不得不吐槽一下产品.
尼玛为啥要搞这样的功能....
搞个两级的不就好了嘛...自带控件,多好.
三级,四级,听说还有六级的....
这样丧心病狂的设计,后台也不好给数据吧.
感谢
这里得感谢Hongyang大神的博客
传送门
效果及实现代码:
以下实现都只用一个RecycerView,没有嵌套.
(一) 三级列表的效果:

具体使用:

因为有3种类型,所以写3种Item.
public class ProvinceItemParent extends TreeItemGroup<CityBean> {
@Override
public List<TreeItem> initChildsList(CityBean data) {
return ItemHelperFactory.createTreeItemList(data.getCitys(), CountyItemParent.class, this);
}
@Override
public int initLayoutId() {
return R.layout.itme_one;
}
@Override
public void onBindViewHolder(ViewHolder holder) {
holder.setText(R.id.tv_content, data.getProvinceName());
}
}
Item如果简单使用则只需复写这3个方法,使用ItemHelperFactory来创建子集,都封装好了.由于将item拆分了.所以onBindViewHolder()也不会有很多代码.
---------------------------------------分割------------------------------------------
商城购物车多选的实现:


//扩展封装的可选中子item的ParentItem.
public class ShopGroupItem extends TreeSelectItemGroup<StoreBean>{
@Override
public SelectFlag selectFlag() {
return SelectFlag.MULTIPLE_CHOICE;//选中方式为多选
}
@Override
public boolean onInterceptClick(TreeItem child) {
//如果是headItem则不调用super的实现
return child != mHeadItem && super.onInterceptClick(child);
}
@Override
public void onClick() {
//不拦截点击事件,则会走到这里.默认实现的点击事件,点击父item,则全选或者全部不选.
if (!isChildCheck()) {
getSelectItems().clear();
getSelectItems().addAll(getChilds());
getSelectItems().remove(mHeadItem);
} else {
getSelectItems().clear();
}
int size = getChilds().size();
for (int i = 0; i < size; i++) {
Object data = getChilds().get(i).getData();
if (data != mHeadItem) {
((ShopListBean) data).setCheck(isChildCheck());
}
}
getItemManager().notifyDataChanged();
}
}
---------------------------------------分割------------------------------------------
多type列表的实现:




---------------------------------------分割------------------------------------------
新闻类,多type混合列表:



------------------------------------分割线--------------------------------------

分包有点糟糕..- -
------------------------------------分割线--------------------------------------
说说为什么写这货吧:
公司产品提出三级这个需求后,我就找啊找.
找的第一个,发现实现其实是ExpandListview嵌套.
找的第二个,ExpandRecyclview,然后就用呗,发现三级展开很卡,看源码,
发现是RecyclerView套RecyclerView
就没有不嵌套的么.....
然后找到hongyang的那个博客,写个试试吧.
说说思路:
1.TreeAdapter只需要关心List<Item> datas 的内容
2.把每个item看成独立的个体,布局样式,每行所占比,bindViewHolder都放到item里面实现,这样不同type相对拆分开,而不是全写在adapter里面.便于维护修改.
3.ItemParent关心自己,上一级和下一级的数据,不会去关心上上级,下下级,item则关心上级和自己,因为没有下一级.
4.展开的实现,父item把子数据集拿出来,然后添加到adapter的List<> datas,变成与自己同级,因为每次展开只会展开一级数据。
5.折叠,遍历(递归或者不递归)子数据,拿数据集可以理解因为一个文件夹下所有的文件,包括子文件夹下的所有),然后从Adapter的List<> datas中删除这些数据。
更新及详解:
下面附上github地址,里面有Demo,
传送门:TreeRecyclerView
欢迎Star,哈哈
您的喜欢与回复是我最大的动力-_-