其他分享
首页 > 其他分享> > Android ExpandableListView折叠菜单的三层嵌套实现,kotlin语言优点特性

Android ExpandableListView折叠菜单的三层嵌套实现,kotlin语言优点特性

作者:互联网

<?xml version="1.0" encoding="utf-8"?>



我们可以通过 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">

</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">

</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">

</androidx.constraintlayout.widget.ConstraintLayout>

Adapter

上面说过 ExpandableListView 继承自 ListVie

《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》

【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享

w,所以我们需要 Adapter,三级嵌套,我们需要两个 Adapter。

这里有必要说一下,为什么是两个 Adapter,ExpandableListView 的 Adapter 继承自 BaseExpandableListAdapter。需要重写 getGroupView 和 getChildView。这两个方法中的 view 分别 inflate 父级菜单的布局和子级菜单的布局文件。

所以我们上面的三个级别的菜单布局文件通过两个 Adapter 来连接。分别是一级菜单的 Adapter 和三级菜单的 Adapter。

下面给出这两个 Adapter 的详细说明,需要注意的地方已经进行备注,请仔细看备注

/**

@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;
}

/**

/**

/**

标签:菜单,return,kotlin,Adapter,groupPosition,ExpandableListView,convertView,Android,an
来源: https://blog.csdn.net/m0_64604042/article/details/122188993