Android使用ExpandableListView實(shí)現(xiàn)三層嵌套折疊菜單
前段時(shí)間項(xiàng)目的新功能里有些頁(yè)面需要三層嵌套列表實(shí)現(xiàn),雖然在移動(dòng)端這種很丑,但是需求就是需求。
本來(lái)想用各種View嵌套,然后發(fā)現(xiàn)系統(tǒng)有個(gè)ExpandableListView。就直接拿來(lái)用了。
理論上來(lái)說(shuō),ExpandableListView的二級(jí)嵌套和三級(jí)嵌套沒(méi)有本質(zhì)區(qū)別,如果把二級(jí)嵌套的子級(jí)換成一個(gè)新的ExpandableListView,就可以實(shí)現(xiàn)三級(jí)嵌套。
有了思路,關(guān)于ExpandableListView的三層嵌套就直接上手實(shí)現(xiàn)
這里說(shuō)下我的需求是有些數(shù)據(jù)是只有二級(jí),有些數(shù)據(jù)是三級(jí)的。如果你的需求是只有三級(jí),不需要考慮三級(jí)二級(jí)混合的情況,下面有說(shuō)明怎么處理。
效果圖
ExpandableListView
ExpandableListView是官方提供的一個(gè)可展示折疊列表的控件。
它的基本用法如下
基本用法
ExpandableListView的基本用法很簡(jiǎn)單,它本質(zhì)上就是ListView,所以用法也差不多,這里就不介紹了。
下面開(kāi)始進(jìn)入正題。
布局文件
先說(shuō)下,因?yàn)槭侨?jí)嵌套,所以需要四個(gè)布局文件,Activity頁(yè)面本身需要一個(gè)布局文件,然后就是三級(jí)嵌套的三個(gè)布局文件。
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>
我們可以通過(guò)ExpandableListView的默認(rèn)屬性來(lái)控制部分樣式,這里貼上菜鳥(niǎo)教程的屬性圖片
一級(jí)菜單布局文件
<?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>
二級(jí)菜單布局文件
<?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>
三級(jí)菜單布局文件
<?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
上面說(shuō)過(guò)ExpandableListView繼承自ListView,所以我們需要Adapter,三級(jí)嵌套,我們需要兩個(gè)Adapter。
這里有必要說(shuō)一下,為什么是兩個(gè)Adapter,ExpandableListView的Adapter繼承自BaseExpandableListAdapter。需要重寫getGroupView和getChildView。這兩個(gè)方法中的view分別inflate父級(jí)菜單的布局和子級(jí)菜單的布局文件。
所以我們上面的三個(gè)級(jí)別的菜單布局文件通過(guò)兩個(gè)Adapter來(lái)連接。分別是一級(jí)菜單的Adapter和三級(jí)菜單的Adapter。
下面給出這兩個(gè)Adapter的詳細(xì)說(shuō)明,需要注意的地方已經(jīng)進(jìn)行備注,請(qǐng)仔細(xì)看備注
一級(jí)菜單Adapter
最值得注意的是該Adapter的getChildView方法和getChildrenCount。因?yàn)橛行?shù)據(jù)不包含三級(jí)菜單,有些包含了三級(jí)菜單。另外,這個(gè)地方需要對(duì)下級(jí)嵌套的ExpandableListView進(jìn)行處理。
/** * 三級(jí)折疊菜單的一級(jí)Adapter * * @author StarryRivers */ public class ChapterExpandableAdapter extends BaseExpandableListAdapter { ... @Override public int getGroupCount() { // 父菜單長(zhǎng)度 return fatherChapterList.size(); } @Override public int getChildrenCount(int groupPosition) { // 子菜單長(zhǎng)度,嵌套所以返回只能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(); } // 設(shè)置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; // 加載子級(jí)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); } // 本身的父級(jí),相當(dāng)于三級(jí)目錄的子級(jí)監(jiān)聽(tīng) expandableListView.setOnGroupClickListener((parent12, v, groupPosition12, id) -> { // 如果第三層size為0,意味著沒(méi)有三級(jí)菜單 if (fatherChapterList != null && fatherChapterList.size() > 0 && fatherChapterList.get(groupPosition).getSec().get(groupPosition12).getThird().size() == 0) { // TODO 業(yè)務(wù)處理 } // 存在第三級(jí)數(shù)據(jù),事件分發(fā)機(jī)制繼續(xù)想下傳遞 return false; }); expandableListView.setOnChildClickListener((parent1, v, groupPosition1, childPosition1, id) -> { // 三級(jí)菜單的業(yè)務(wù)處理 return true; }); return expandableListView; } /** * 子列表是否可選,如果為false,則子項(xiàng)不能觸發(fā)點(diǎn)擊事件,默認(rèn)為false * * @param groupPosition groupPosition * @param childPosition childPosition * @return result */ @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } /** * 父級(jí)菜單的ViewHolder */ static class GroupViewHolder { TextView groupTitle; } }
三級(jí)菜單Adapter
三級(jí)菜單的Adapter就和普通的二級(jí)嵌套時(shí)的Adapter相同,沒(méi)什么特別注意的地方,所以只列出了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(); } // 設(shè)置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; }
使用
當(dāng)我們完成了上面的步驟之后,最后就是在Activity中的使用了。使用方法超級(jí)簡(jiǎn)單
給ExpandableListView設(shè)置Adapter就可以了
@BindView(R.id.chapter_elv) ExpandableListView chapterExpandable; private ChapterExpandableAdapter chapterExpandableAdapter; ... chapterExpandableAdapter = new ChapterExpandableAdapter(this); chapterExpandable.setAdapter(chapterExpandableAdapter);
寫在最后
因?yàn)槭侨?jí)嵌套,所以ExpandableListView需要重寫一下,重新繪制高度。不然會(huì)出現(xiàn)頁(yè)面展示不全或者不完整的問(wèn)題。
以上就是Android使用ExpandableListView實(shí)現(xiàn)三層嵌套折疊菜單的詳細(xì)內(nèi)容,更多關(guān)于Android ExpandableListView三層嵌套折疊菜單的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Android中實(shí)現(xiàn)長(zhǎng)按照片彈出右鍵菜單功能的實(shí)例代碼
這篇文章主要介紹了Android中實(shí)現(xiàn)長(zhǎng)按照片彈出右鍵菜單功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-01-01Android?webview攔截H5的接口請(qǐng)求并返回處理好的數(shù)據(jù)代碼示例
這篇文章主要給大家介紹了關(guān)于Android?webview攔截H5的接口請(qǐng)求并返回處理好的數(shù)據(jù)的相關(guān)資料,通過(guò)WebView的shouldInterceptRequest方法,Android可以攔截并處理WebView中的H5網(wǎng)絡(luò)請(qǐng)求,需要的朋友可以參考下2024-10-10Android 中RecyclerView頂部刷新實(shí)現(xiàn)詳解
這篇文章主要介紹了Android 中RecyclerView頂部刷新實(shí)現(xiàn)詳解的相關(guān)資料,希望通過(guò)本文能幫助到大家,需要的朋友可以參考下2017-10-10Android開(kāi)發(fā)實(shí)現(xiàn)的標(biāo)準(zhǔn)體重計(jì)算器功能示例
這篇文章主要介紹了Android開(kāi)發(fā)實(shí)現(xiàn)的標(biāo)準(zhǔn)體重計(jì)算器功能,結(jié)合實(shí)例形式分析了Android體重計(jì)算器的界面布局與功能實(shí)現(xiàn)相關(guān)操作技巧,需要的朋友可以參考下2017-12-12Android類加載ClassLoader雙親委托機(jī)制詳解
這篇文章主要為大家介紹了Android類加載ClassLoader雙親委托機(jī)制詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07一款A(yù)ndroid APK的結(jié)構(gòu)構(gòu)成解析
本篇文章介紹了我在學(xué)習(xí)過(guò)程中對(duì)于Android 程序的理解總結(jié),刨析了apk的組成與產(chǎn)生過(guò)程,通讀本篇對(duì)大家的學(xué)習(xí)或工作具有一定的價(jià)值,需要的朋友可以參考下2021-10-10Android ListView滑動(dòng)改變標(biāo)題欄背景漸變效果
這篇文章主要為大家詳細(xì)介紹了Android ListView滑動(dòng)改變標(biāo)題欄背景漸變效果,透明轉(zhuǎn)變成不透明,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07