吐槽
每次写个博客都要吐槽一下,这估计是我博客的习惯吧,,,,好了不多说了,上一篇讲了安卓的dialog的知识,但是产品给的需求还是弄不了,需要dialog和ExpandableListView结合起来弄个选择框,然后我就又去学了下ExpandableListView的知识,But发现我俩个结合在一起之后,程序一运行就崩了,,,哇哇哇简直是心累啊,马上期末考试了,这个项目bug还是没改完,项目都托了这么久了,主体还是没写好,难受。
什么叫ExpandableListView
A view that shows items in a vertically scrolling two-level list. This differs from the ListView by allowing two levels: groups which can individually be expanded to show its children. The items come from the ExpandableListAdapter associated with this view.
。。。。
以上是官方给的定义,but英语太渣,就是简单的来说,它是一种用于滚动的展示的两级列表视图。也就是ListView的二级选择加强版这种。
先看下效果图
沉迷王者荣耀无法自拔哈哈哈哈
这个就是优化过的ExpandableListView,很简单,二级选择,只需要准备三个东西:
1.一级选择的布局//也就是父布局
2.二级选择的布局//也就是子布局
3. ExtendableListViewAdapter的自定义//也就是适配器的书写
看我的效果图我做了如下的优化:
1.将展开的箭头弄到最右边,一级选择在最左边//因为你不改的话,它会自动把箭头放在最左边
2.只让它展开一个父布局,不然一起展开太长了,不好看
实现过程
第一步:设置调用ExpandableListView的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ExpandableListView
android:id="@+id/expend_list"
android:layout_width="match_parent"
android:layout_height="match_parent"></ExpandableListView>
</LinearLayout>
第二步:自己写个一级选择的布局,也就是父布局partent_item.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/label_group_normal"