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是官方提供的一個(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: 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'> <TextViewandroid: 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'> <TextViewandroid: 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'> <TextViewandroid: 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)度,嵌套所以返回只能1return 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è)置titlegroupHolder.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í)AdapterChapterExpandableLowAdapter 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è)置titlegroupHolder.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)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. IntelliJ IDEA創(chuàng)建web項(xiàng)目的方法2. Python 快速驗(yàn)證代理IP是否有效的方法實(shí)現(xiàn)3. 一文透徹詳解.NET框架類型系統(tǒng)設(shè)計(jì)要點(diǎn)4. 用PHP編寫每周簽到功能以提高用戶參與度5. php字符串截取的3個(gè)簡(jiǎn)單方法6. JS樹(shù)形結(jié)構(gòu)根據(jù)id獲取父級(jí)節(jié)點(diǎn)元素的示例代碼7. 網(wǎng)頁(yè)加載速度優(yōu)化技巧的方案詳解8. XML中顯示HTML的小技巧9. 多級(jí)聯(lián)動(dòng)下拉選擇框,動(dòng)態(tài)獲取下一級(jí)10. 博客日志摘要暨RSS技術(shù)
