有了思路,关于 ExpandableListView 的三层嵌套就直接上手实现
这里说下我的需求是有些数据是只有二级,有些数据是三级的。如果你的需求是只有三级,不需要考虑三级二级混合的情况,下面有说明怎么处理。
效果图
ExpandableListView
ExpandableListView 是官方提供的一个可展示折叠列表的控件。官方文档直链
它的基本用法如下
基本用法
ExpandableListView 的基本用法很简单,它本质上就是 ListView,所以用法也差不多,这里就不介绍了。
如果有需要的,可以参考菜鸟教程 ExpandableListView 基本用法
下面开始进入正题。
布局文件
先说下,因为是三级嵌套,所以需要四个布局文件,Activity 页面本身需要一个布局文件,然后就是三级嵌套的三个布局文件。
- Activity 布局文件
我们可以通过 ExpandableListView 的默认属性来控制部分样式,这里贴上菜鸟教程的属性图片
- 一级菜单布局文件
<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”>
</androidx.constraintlayout.widget.ConstraintLayout>
- 二级菜单布局文件
<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”>
</androidx.constraintlayout.widget.ConstraintLayout>
- 三级菜单布局文件
<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”>
</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 业务处理
文末
好了,今天的分享就到这里,如果你对在面试中遇到的问题,或者刚毕业及工作几年迷茫不知道该如何准备面试并突破现状提升自己,对于自己的未来还不够了解不知道给如何规划,可以来看看同行们都是如何突破现状,怎么学习的,来吸收他们的面试以及工作经验完善自己的之后的面试计划及职业规划。
这里放上一部分我工作以来以及参与过的大大小小的面试收集总结出来的一套进阶学习的视频及面试专题资料包,主要还是希望大家在如今大环境不好的情况下面试能够顺利一点,希望可以帮助到大家
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》,点击传送门,即可获取!
过的大大小小的面试收集总结出来的一套进阶学习的视频及面试专题资料包,主要还是希望大家在如今大环境不好的情况下面试能够顺利一点,希望可以帮助到大家
[外链图片转存中…(img-s5X6XLFa-1714543339425)]
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》,点击传送门,即可获取!