前段时间项目的新功能里有些页面须要三层嵌套列表实现,虽然在移动端这种很丑,可是需求就是需求。
原本想用各类 View 嵌套,而后发现系统有个 ExpandableListView。就直接拿来用了。html
理论上来讲,ExpandableListView 的二级嵌套和三级嵌套没有本质区别,若是把二级嵌套的子级换成一个新的 ExpandableListView,就能够实现三级嵌套。java
有了思路,关于 ExpandableListView 的三层嵌套就直接上手实现android
这里说下个人需求是有些数据是只有二级,有些数据是三级的。若是你的需求是只有三级,不须要考虑三级二级混合的状况,下面有说明怎么处理。git
效果图
github
ExpandableListView 是官方提供的一个可展现折叠列表的控件。官方文档直链markdown
它的基本用法以下app
ExpandableListView 的基本用法很简单,它本质上就是 ListView,因此用法也差很少,这里就不介绍了。
ide
若是有须要的,能够参考菜鸟教程 ExpandableListView 基本用法
下面开始进入正题。oop
先说下,由于是三级嵌套,因此须要四个布局文件,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>
复制代码
上面说过 ExpandableListView 继承自 ListView,因此咱们须要 Adapter,三级嵌套,咱们须要两个 Adapter。
这里有必要说一下,为何是两个 Adapter,ExpandableListView 的 Adapter 继承自 BaseExpandableListAdapter。须要重写 getGroupView 和 getChildView。这两个方法中的 view 分别 inflate 父级菜单的布局和子级菜单的布局文件。
因此咱们上面的三个级别的菜单布局文件经过两个 Adapter 来链接。分别是一级菜单的 Adapter 和三级菜单的 Adapter。
下面给出这两个 Adapter 的详细说明,须要注意的地方已经进行备注,请仔细看备注
/**
* 三级折叠菜单的一级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;
}
}
复制代码
@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 食用更香!