您好,登錄后才能下訂單哦!
先上效果圖:
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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。