1、在Android开发中,有时候希望对列表项可以分组管理并实现收缩功能,例如QQ在使用时,有“我的好友”、“家人”、“同学”等分组,单击其中一项会展开,再单击一次又缩回去。要实现这种功能,就得使用到我们今天的主角ExpandableListView组件了。
2、该组件层次结构关系如下:
java.lang.Object
android.view.View
android.view.ViewGroup
android widget.AdapterView<T extends android.widget.Adapter>
android.widget.AbsListView
android.widget.ListView
android.widget.ExpandableListView
3、每一个可扩展项旁边都有一个提示符(箭头等)用来说明该列表项目前的状态,可以使用方法:setChildIndicator(Drawable)和setGroupIndicator(Drawable)(或相应的XML文件的属性)去设置这些提示符的样式。注意:在XML布局文件中,一般不对ExpandableListView的android:layout_height属性使用wrap_content,否则可能会报错。
与ListView一样ExpandableListView也需要一个适配器做桥梁来提供数据,ExpandableListView是一个垂直滚动显示两级列表项,它可以有两层,每层都能够独立的展开并显示其子项。BaseExpandableListAdapter是一个用在ExpandableListView组件的适配器。
4、创建布局文件
<?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">
<TextView
android:textSize="24sp"
android:gravity="center"
android:text="花名册"
2、该组件层次结构关系如下:
java.lang.Object
android.view.View
android.view.ViewGroup
android widget.AdapterView<T extends android.widget.Adapter>
android.widget.AbsListView
android.widget.ListView
android.widget.ExpandableListView
3、每一个可扩展项旁边都有一个提示符(箭头等)用来说明该列表项目前的状态,可以使用方法:setChildIndicator(Drawable)和setGroupIndicator(Drawable)(或相应的XML文件的属性)去设置这些提示符的样式。注意:在XML布局文件中,一般不对ExpandableListView的android:layout_height属性使用wrap_content,否则可能会报错。
与ListView一样ExpandableListView也需要一个适配器做桥梁来提供数据,ExpandableListView是一个垂直滚动显示两级列表项,它可以有两层,每层都能够独立的展开并显示其子项。BaseExpandableListAdapter是一个用在ExpandableListView组件的适配器。
4、创建布局文件
<?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">
<TextView
android:textSize="24sp"
android:gravity="center"
android:text="花名册"