去网上找了下资料,然后自己整理了下二级、三级菜单的实现,直接上代码。
Recyclerview记得要导包:implementation 'com.android.support:recyclerview-v7:28.0.0'
一、首先在app下的build.gradle中加入:
//适配器 implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.6.1'
二、在project下的build.gradle中加入:
maven { url 'https://jitpack.io' } 如图:
三、建立三个bean类并实现MultiItemEntity
public class ExpandItem extends AbstractExpandableItemimplements MultiItemEntity { private String title; public ExpandItem(String title) { this.title = title; } @Override public int getLevel() { return 0; } @Override public int getItemType() { return QuickExpandableAdapter.TYPE_LEVEL_0; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } }
public class Expand1Item extends AbstractExpandableItemimplements MultiItemEntity { private String title; public Expand1Item(String title){ this.title = title; } @Override public int getLevel() { return 1; } @Override public int getItemType() { return QuickExpandableAdapter.TYPE_LEVEL_1; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } }
public class Expand2Item implements MultiItemEntity { private String title; public Expand2Item(String title) { this.title = title; } @Override public int getItemType() { return QuickExpandableAdapter.TYPE_LEVEL_2; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } }
四、建立QuickExpandableAdapter
public class QuickExpandableAdapter extends BaseMultiItemQuickAdapter{ public static final int TYPE_LEVEL_0 = 0; public static final int TYPE_LEVEL_1 = 1; public static final int TYPE_LEVEL_2 = 2; /** * * Same as QuickAdapter#QuickAdapter(Context,int) but with * * some initialization data. * * * * @param data A new list is created out of this one to avoid mutable list * */ public QuickExpandableAdapter(List data) { super(data); addItemType(TYPE_LEVEL_0, R.layout.view_expand_0); addItemType(TYPE_LEVEL_1, R.layout.view_expand_1); addItemType(TYPE_LEVEL_2, R.layout.view_expand_2); } @Override protected void convert(final BaseViewHolder helper, MultiItemEntity item) { switch (helper.getItemViewType()) { case TYPE_LEVEL_0: final ExpandItem item0 = (ExpandItem) item; helper.setText(R.id.expand_list_item0_tv, item0.getTitle()); helper.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int pos = helper.getAdapterPosition(); if (item0.isExpanded()) { collapse(pos); Toast.makeText(mContext, "收起:" + item0.getTitle(), Toast.LENGTH_SHORT).show(); } else { expand(pos); Toast.makeText(mContext, "展开:" + item0.getTitle(), Toast.LENGTH_SHORT).show(); } } }); break; case TYPE_LEVEL_1: final Expand1Item item1 = (Expand1Item) item; helper.setText(R.id.expand_list_item1_tv, item1.getTitle()); helper.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int pos = helper.getAdapterPosition(); if (item1.isExpanded()) { collapse(pos, false); Toast.makeText(mContext, "收起:" + item1.getTitle(), Toast.LENGTH_SHORT).show(); } else { expand(pos, false); Toast.makeText(mContext, "展开:" + item1.getTitle(), Toast.LENGTH_SHORT).show(); } } }); break; case TYPE_LEVEL_2: final Expand2Item item2 = (Expand2Item) item; helper.setText(R.id.expand_list_item2_tv, item2.getTitle()); helper.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(mContext, "点击:" + item2.getTitle(), Toast.LENGTH_SHORT).show(); } }); break; } } }
五、主界面代码
public class ListActivity extends AppCompatActivity { private QuickExpandableAdapter expandableAdapter; private RecyclerView recyclerView; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_list); recyclerView = findViewById(R.id.recyclerView); LinearLayoutManager layoutManager = new LinearLayoutManager(this); //设置布局管理器 recyclerView.setLayoutManager(layoutManager); //设置为垂直布局,这也是默认的 layoutManager.setOrientation(OrientationHelper.VERTICAL); //设置增加或删除条目的动画 recyclerView.setItemAnimator(new DefaultItemAnimator()); expandableAdapter = new QuickExpandableAdapter(getExpandListData(10)); expandableAdapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_RIGHT); recyclerView.setAdapter(expandableAdapter); expandableAdapter.expandAll(0, true); } private ListgetExpandListData(int count) { int lvCount = count; int lv1Count = 3; int lv2Count = 5; List data = new ArrayList<>(); for (int i = 0; i < lvCount; i++) { ExpandItem item0 = new ExpandItem("一级列表标题" + i); for (int j = 0; j < lv1Count; j++) { Expand1Item item1 = new Expand1Item("二级列表标题" + j); for (int k = 0; k < lv2Count; k++) { item1.addSubItem(new Expand2Item("三级列表标题" + k)); } item0.addSubItem(item1); } data.add(item0); } return data; } }
六、布局文件
好了,到这里代码已经全部写完了,因为很简单,所以就写的粗糙了些,拿上能直接用。
这里有个问题:
1.是和我做的项目有关联,如果你的recycleView外面嵌套了NestedScrollView的话,数据量一大就会导致你的列表好卡,我表示深受其害,这是因为你嵌套了NestedScrollView后,会把recyclerview所有的条目都加载出来,相当于吧recycleView的复用机制给禁掉。想想多可怕~~~
2.用ScrollView虽然可以复用,但是又会与recyclerview有滑动冲突,所以最好是不要嵌套,要不然又要解决冲突。
因为我之前解决NestedScrollView和Recyclerview的冲突就是禁掉了Recyclerview的滑动事件,只用外层NestedScrollView的滑动事件,所以最后坑了自己一把。。。。。。。。