前段时间项目的新功能里有些页面需要三层嵌套列表实现,虽然在移动端这种很丑,但是需求就是需求。
本来想用各种 View 嵌套,然后发现系统有个 ExpandableListView。就直接拿来用了。
理论上来说,ExpandableListView 的二级嵌套和三级嵌套没有本质区别,如果把二级嵌套的子级换成一个新的 ExpandableListView,就可以实现三级嵌套。
有了思路,关于 ExpandableListView 的三层嵌套就直接上手实现
这里说下我的需求是有些数据是只有二级,有些数据是三级的。如果你的需求是只有三级,不需要考虑三级二级混合的情况,下面有说明怎么处理。
效果图
ExpandableListView
ExpandableListView 是官方提供的一个可展示折叠列表的控件。官方文档直链
它的基本用法如下
基本用法
ExpandableListView 的基本用法很简单,它本质上就是 ListView,所以用法也差不多,这里就不介绍了。
如果有需要的,可以参考菜鸟教程 ExpandableListView 基本用法
下面开始进入正题。
布局文件
先说下,因为是三级嵌套,所以需要四个布局文件,Activity 页面本身需要一个布局文件,然后就是三级嵌套的三个布局文件。
- Activity 布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ExpandableListView
android:id="@+id/expand_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:cacheColorHint="#00000000"
android:childIndicator="@color/white"
android:divider="@null"
android:fadeScrollbars="false"
android:groupIndicator="@null"
android:listSelector="#00000000"
android:scrollbars="none" />
</LinearLayout>
复制代码
我们可以通过 ExpandableListView 的默认属性来控制部分样式,这里贴上菜鸟教程的属性图片
- 一级菜单布局文件
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="44dp"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@drawable/chapter_gradient_group">
<TextView
android:id="@+id/adapter_title"
android:layout_width="0dp"
android:layout_height="match_parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginHorizontal="10dp"
android:paddingStart="20dp"
android:singleLine="true"
android:ellipsize="end"
android:text="@string/groupName"
android:textColor="@color/white"
android:textSize="16sp"
android:gravity="start|center_vertical" />
</androidx.constraintlayout.widget.ConstraintLayout>
复制代码
- 二级菜单布局文件
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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="match_parent"
android:background="@drawable/chapter_gradient_child">
<TextView
android:id="@+id/adapter_child_title"
android:layout_width="match_parent"
android:layout_height="40dp"
android:ellipsize="end"
android:gravity="start|center_vertical"
android:paddingStart="30dp"
android:paddingEnd="10dp"
android:singleLine="true"
android:text="@string/childName"
android:textColor="@color/white"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
复制代码
- 三级菜单布局文件
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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="match_parent"
android:background="@drawable/chapter_gradient_grandson">
<TextView
android:id="@+id/adapter_grandson_title"
android:layout_width="match_parent"
android:layout_height="40dp"
android:ellipsize="end"
android:gravity="start|center_vertical"
android:paddingStart="40dp"
android:paddingEnd="10dp"
android:singleLine="true"
android:text="@string/grandsonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
复制代码
Adapter
上面说过 ExpandableListView 继承自 ListView,所以我们需要 Adapter,三级嵌套,我们需要两个 Adapter。
这里有必要说一下,为什么是两个 Adapter,ExpandableListView 的 Adapter 继承自 BaseExpandableListAdapter。需要重写 getGroupView 和 getChildView。这两个方法中的 view 分别 inflate 父级菜单的布局和子级菜单的布局文件。
所以我们上面的三个级别的菜单布局文件通过两个 Adapter 来连接。分别是一级菜单的 Adapter 和三级菜单的 Adapter。
下面给出这两个 Adapter 的详细说明,需要注意的地方已经进行备注,请仔细看备注
- 一级菜单 Adapter
最值得注意的是该 Adapter 的 getChildView 方法和 getChildrenCount。因为有些数据不包含三级菜单,有些包含了三级菜单。另外,这个地方需要对下级嵌套的 ExpandableListView 进行处理。
/**
* 三级折叠菜单的一级Adapter
*
* @author StarryRivers
*/
public class ChapterExpandableAdapter extends BaseExpandableListAdapter {
...
@Override
public int getGroupCount() {
// 父菜单长度
return fatherChapterList.size();
}
@Override
public int getChildrenCount(int groupPosition) {
// 子菜单长度,嵌套所以返回只能1
return 1;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
GroupViewHolder groupHolder;
// 尽可能重用旧view处理
if (convertView == null) {
convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_expandable_group_view, parent, false);
groupHolder = new GroupViewHolder();
groupHolder.groupTitle = convertView.findViewById(R.id.adapter_title);
convertView.setTag(groupHolder);
} else {
groupHolder = (GroupViewHolder) convertView.getTag();
}
// 设置title
groupHolder.groupTitle.setText(fatherChapterList.get(groupPosition).getName());
return convertView;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = new CustomExpandableListView(context);
}
CustomExpandableListView expandableListView = (CustomExpandableListView) convertView;
// 加载子级Adapter
ChapterExpandableLowAdapter lowAdapter = new ChapterExpandableLowAdapter(context);
lowAdapter.setTotalList(fatherChapterList.get(groupPosition).getSec());
expandableListView.setAdapter(lowAdapter);
if (fatherChapterList.get(groupPosition).getSec().get(childPosition).getThird().size() == 0) {
expandableListView.setGroupIndicator(null);
}
// 本身的父级,相当于三级目录的子级监听
expandableListView.setOnGroupClickListener((parent12, v, groupPosition12, id) -> {
// 如果第三层size为0,意味着没有三级菜单
if (fatherChapterList != null && fatherChapterList.size() > 0 && fatherChapterList.get(groupPosition).getSec().get(groupPosition12).getThird().size() == 0) {
// TODO 业务处理
}
// 存在第三级数据,事件分发机制继续想下传递
return false;
});
expandableListView.setOnChildClickListener((parent1, v, groupPosition1, childPosition1, id) -> {
// 三级菜单的业务处理
return true;
});
return expandableListView;
}
/**
* 子列表是否可选,如果为false,则子项不能触发点击事件,默认为false
*
* @param groupPosition groupPosition
* @param childPosition childPosition
* @return result
*/
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
/**
* 父级菜单的ViewHolder
*/
static class GroupViewHolder {
TextView groupTitle;
}
}
复制代码
- 三级菜单 Adapter
三级菜单的 Adapter 就和普通的二级嵌套时的 Adapter 相同,没什么特别注意的地方,所以只列出了 getGroupView 和 getChildView 方法代码
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
ChapterExpandableLowAdapter.GroupViewHolder groupHolder;
// 尽可能重用旧view处理
if (convertView == null) {
convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_expandable_child_view, parent, false);
groupHolder = new ChapterExpandableLowAdapter.GroupViewHolder();
groupHolder.groupTitle = convertView.findViewById(R.id.adapter_child_title);
convertView.setTag(groupHolder);
} else {
groupHolder = (ChapterExpandableLowAdapter.GroupViewHolder) convertView.getTag();
}
// 设置title
groupHolder.groupTitle.setText(childChapterList.get(groupPosition).getName());
return convertView;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
ChapterExpandableLowAdapter.ChildViewHolder childHolder;
if (convertView == null) {
convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_expandable_grandson_view, parent, false);
childHolder = new ChapterExpandableLowAdapter.ChildViewHolder();
childHolder.childTitle = convertView.findViewById(R.id.adapter_grandson_title);
convertView.setTag(childHolder);
} else {
childHolder = (ChapterExpandableLowAdapter.ChildViewHolder) convertView.getTag();
}
if (childChapterList.get(groupPosition).getThird() != null && childChapterList.get(groupPosition).getThird().size() > 0) {
childHolder.childTitle.setText(childChapterList.get(groupPosition).getThird().get(childPosition).getName());
}
return convertView;
}
复制代码
使用
当我们完成了上面的步骤之后,最后就是在 Activity 中的使用了。使用方法超级简单
给 ExpandableListView 设置 Adapter 就可以了
@BindView(R.id.chapter_elv)
ExpandableListView chapterExpandable;
private ChapterExpandableAdapter chapterExpandableAdapter;
...
chapterExpandableAdapter = new ChapterExpandableAdapter(this);
chapterExpandable.setAdapter(chapterExpandableAdapter);
复制代码
写在最后
哦,忘记了,因为是三级嵌套,所以 ExpandableListView 需要重写一下,重新绘制高度。不然会出现页面展示不全或者不完整的问题。
完整代码在 Git,感兴趣可以看下,配合 Star 食用更香!