效果图:
1 引入库
implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.46'
2 对象bean
一级列表:
package com.lucas.yanfriends.model.YanNew.select;
import com.chad.library.adapter.base.entity.AbstractExpandableItem;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import java.io.Serializable;
import static com.lucas.yanfriends.ui.selectSchoolMajor.adapter.MajorSmallAdapter.ITEM_FIRST_LEVEL;
public class MajorMainBean extends AbstractExpandableItem<MajorSmallBean> implements Serializable , MultiItemEntity {
private String major_main_name;
public MajorMainBean(String major_main_name) {
this.major_main_name = major_main_name;
}
public String getMajor_main_name() {
return major_main_name;
}
public void setMajor_main_name(String major_main_name) {
this.major_main_name = major_main_name;
}
@Override
public int getItemType() {
return ITEM_FIRST_LEVEL;
}
@Override
public int getLevel() {
return 0;
}
}
二级列表:
package com.lucas.yanfriends.model.YanNew.select;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import java.io.Serializable;
import static com.lucas.yanfriends.ui.selectSchoolMajor.adapter.MajorSmallAdapter.ITEM_FIRST_LEVEL;
import static com.lucas.yanfriends.ui.selectSchoolMajor.adapter.MajorSmallAdapter.ITEM_SECOND_LEVEL;
public class MajorSmallBean implements Serializable , MultiItemEntity {
private int major_id;
private String major_name;
private int major_bbsid;
private int major_pid;
private String major_pname;
public int getMajor_id() {
return major_id;
}
public void setMajor_id(int major_id) {
this.major_id = major_id;
}
public String getMajor_name() {
return major_name;
}
public void setMajor_name(String major_name) {
this.major_name = major_name;
}
public int getMajor_bbsid() {
return major_bbsid;
}
public void setMajor_bbsid(int major_bbsid) {
this.major_bbsid = major_bbsid;
}
public int getMajor_pid() {
return major_pid;
}
public void setMajor_pid(int major_pid) {
this.major_pid = major_pid;
}
public String getMajor_pname() {
return major_pname;
}
public void setMajor_pname(String major_pname) {
this.major_pname = major_pname;
}
@Override
public int getItemType() {
return ITEM_SECOND_LEVEL;
}
}
3 一/二级列表的布局
一级列表的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="@dimen/dp_45">
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="@dimen/sp_14"
android:textStyle="bold"
android:text="dfkjfds"
android:textColor="@color/black"
android:layout_alignParentLeft="true"
android:layout_marginLeft="@dimen/dp_15"
android:layout_centerVertical="true"/>
<ImageView
android:id="@+id/item_iright"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="@mipmap/blackback"
android:rotation="180"
android:padding="4dp"
android:alpha="0.5"
android:layout_marginRight="15dp"
/>
<View
android:layout_width="match_parent"
android:layout_height="@dimen/dp_1"
android:layout_alignParentBottom="true"
android:layout_marginLeft="@dimen/dp_15"
android:background="@color/gray_backgroud_parent"
/>
</RelativeLayout>
</LinearLayout>
二级列表的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="@dimen/dp_38">
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="@dimen/sp_13"
android:textStyle="bold"
android:text="dfkjfds"
android:textColor="@color/gray"
android:layout_alignParentLeft="true"
android:layout_marginLeft="@dimen/dp_25"
android:layout_centerVertical="true"/>
<View
android:layout_width="match_parent"
android:layout_height="@dimen/dp_1"
android:layout_alignParentBottom="true"
android:layout_marginLeft="@dimen/dp_25"
android:background="@color/gray_backgroud_parent"
/>
</RelativeLayout>
</LinearLayout>
4 Adapter
package com.lucas.yanfriends.ui.selectSchoolMajor.adapter;
import android.support.annotation.Nullable;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.lucas.yanfriends.R;
import com.lucas.yanfriends.model.YanNew.select.MajorMainBean;
import com.lucas.yanfriends.model.YanNew.select.MajorSmallBean;
import java.util.List;
public class MajorSmallAdapter extends BaseMultiItemQuickAdapter<MultiItemEntity, BaseViewHolder> {
public static final int ITEM_FIRST_LEVEL = 1;
public static final int ITEM_SECOND_LEVEL = 2;
public MajorSmallAdapter(@Nullable List<MultiItemEntity> data) {
super(data);
addItemType(ITEM_FIRST_LEVEL, R.layout.list_main_major_item);
addItemType(ITEM_SECOND_LEVEL, R.layout.list_small_major_item);
}
@Override
protected void convert(BaseViewHolder helper, MultiItemEntity item) {
switch (helper.getItemViewType()) {
case ITEM_FIRST_LEVEL:
final MajorMainBean item_temp = (MajorMainBean) item;
helper.setText(R.id.tv_name, item_temp.getMajor_main_name());
helper.itemView.setOnClickListener(v -> {
int pos = helper.getAdapterPosition();
if (item_temp.isExpanded()) {
collapse(pos);
} else {
expand(pos);
}
});
break;
case ITEM_SECOND_LEVEL:
final MajorSmallBean item_temp2 = (MajorSmallBean) item;
helper.setText(R.id.tv_name, item_temp2.getMajor_name());
break;
}
}
}
5 activty中的设置
5.1 适配器
//--------------------------列表适配器--------------------------
//Adapter适配器初始化
recycle.setLayoutManager(new LinearLayoutManager(mContext));
//设置列表默认动画效果
recycle.setItemAnimator(new DefaultItemAnimator());
//绑定适配器
majorSmallAdapter = new MajorSmallAdapter(list_MajorBean);
recycle.setAdapter(majorSmallAdapter);
//列表点击事件
majorSmallAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
@Override
public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
}
});
//--------------------------适配器--------------------------
5.2 数据填充
for (MultiItemEntity multiItemEntity : list_MajorBean_beifen) {
MajorSmallBean majorSamllBean = ((MajorSmallBean) multiItemEntity);
if (temp_class != majorSamllBean.getMajor_pid()) {
MajorMainBean majorMainBean = new MajorMainBean(majorSamllBean.getMajor_name());
temp_class = majorSamllBean.getMajor_pid();
for (MultiItemEntity multiItemEntity2 : list_MajorBean_beifen) {
MajorSmallBean majorSamllBean2 = ((MajorSmallBean) multiItemEntity2);
if (majorSamllBean2.getMajor_pid() == temp_class) {
majorMainBean.addSubItem(majorSamllBean2);
}
}
list_MajorBean.add(majorMainBean);
}
}
关于数据的构造,简化下来如下:
List<MultiItemEntity> 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);
}