先上效果图:
cdn.xitu.io/2018/8/20/165560462c7326fe?w=282&h=500&f=gif&s=549336">
public abstract class BaseTreeRVAdapter<T extends RecyclerView.ViewHolder, M extends Tree> extends BaseRVAdapterV2<T, M> {
@Override
public void onBindViewHolder(T holder, final int position) {
try {
getLevel(position, mDatas, new PosBean());
} catch (StopMsgException ex) {
int type = Integer.parseInt(ex.getMessage());
M tree = ex.getTree();
onBindViewHolder(type, tree, holder);
}
}
@Override
public int getItemViewType(int position) {
try {
getLevel(position, mDatas, new PosBean());
} catch (StopMsgException ex) {
return Integer.parseInt(ex.getMessage());
}
return 0;
}
@Override
public int getItemCount() {
return getTotal(mDatas);
}
protected abstract void onBindViewHolder(int type, M tree, T holder);
public List<M> getDatas() {
return mDatas;
}
public void setDatas(List<M> info) {
if (info != null) {
mDatas = info;
} else {
mDatas.clear();
}
notifyDataSetChanged();
}
}
注释1:主要是计算当前给的数据他的item个数,会根据是否展开的状态来统总个数
注释2:计算当前的postion对应的item层级,来显示对应的item布局
public class TreeListAdapter extends BaseTreeRVAdapter<RecyclerView.ViewHolder, T extends Tree> {
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view;
RecyclerView.ViewHolder vh;
switch (viewType) {
case 0:
......
break;
case 1:
......
break;
case 2:
......
break;
case 3:
case 4:
......
}
return vh;
}
@Override
protected void onBindViewHolder(int type, final DiscountItem itemCategoryListBean, RecyclerView.ViewHolder holder) {
switch (type) {
case 0:
......
break;
case 1:
......
break;
case 2:
......
break;
case 3:
case 4:
......
}
}
}
必须实现的几个方法:
int getLevel();//每个item的层级,比如一级就是0,二级就返回1,这里的0和1可以自己定义,后面就是根据这个来选择哪种样式的item
List<T> getChilds();//返回的子列表,比如当前类是一级的话,那么这个方法返回的是二级的列表数据
boolean isExpand();//是否展开,true展开,false不展开,一般就是点击下去修改这个值刷新adapter,就能达到展开还是折叠的目的了
step 1.
Add it in your root build.gradle at the end of repositories:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
Step 2. Add the dependency
dependencies {
implementation 'com.github.WelliJohn:MultiLevelRecyclerView:0.0.1'
}
多级列表示例,如果觉得有用,还请点个赞。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。