先上效果圖:
cdn.xitu.io/2018/8/20/165560462c7326fe?w=282&h=500&f=gif&s=549336">
成都創(chuàng)新互聯(lián)公司咨詢熱線:028-86922220,為您提供成都網(wǎng)站建設(shè)網(wǎng)頁設(shè)計及定制高端網(wǎng)站建設(shè)服務(wù),成都創(chuàng)新互聯(lián)公司網(wǎng)頁制作領(lǐng)域十年,包括成都展覽展示等多個領(lǐng)域擁有多年設(shè)計經(jīng)驗,選擇成都創(chuàng)新互聯(lián)公司,為企業(yè)錦上添花!
public abstract class BaseTreeRVAdapter extends BaseRVAdapterV2 {
@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 getDatas() {
return mDatas;
}
public void setDatas(List info) {
if (info != null) {
mDatas = info;
} else {
mDatas.clear();
}
notifyDataSetChanged();
}
}
注釋1:主要是計算當前給的數(shù)據(jù)他的item個數(shù),會根據(jù)是否展開的狀態(tài)來統(tǒng)總個數(shù)
注釋2:計算當前的postion對應(yīng)的item層級,來顯示對應(yīng)的item布局
public class TreeListAdapter extends BaseTreeRVAdapter {
@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:
......
}
}
}
必須實現(xiàn)的幾個方法:
int getLevel();//每個item的層級,比如一級就是0,二級就返回1,這里的0和1可以自己定義,后面就是根據(jù)這個來選擇哪種樣式的item
List getChilds();//返回的子列表,比如當前類是一級的話,那么這個方法返回的是二級的列表數(shù)據(jù)
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'
}
多級列表示例,如果覺得有用,還請點個贊。