<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 业务处理
}
// 存在第三级数据,事件分发机制继续想下传递
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;
}
}
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级安卓工程师,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年最新Android移动开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频
如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注Android)
最后
**要想成为高级安卓工程师,必须掌握许多基础的知识。**在工作中,这些原理可以极大的帮助我们理解技术,在面试中,更是可以帮助我们应对大厂面试官的刁难。
【Android核心高级技术PDF文档,BAT大厂面试真题解析】点击:Android架构视频+BAT面试专题PDF+学习笔记即可获取!
qS-1711289059441)]
[外链图片转存中…(img-tSw43f9K-1711289059441)]
【Android核心高级技术PDF文档,BAT大厂面试真题解析】点击:Android架构视频+BAT面试专题PDF+学习笔记即可获取!