ExpandableListView是一个垂直滚动显示两级列表项的视图,与ListView不同的是,它可以有两层:每一层都能够被独立的展开并显示其子项。
好友QQ列表,可以展开,可以收起,在android中,以往用的比较多的是listview,虽然可以实现列表的展示,但在某些情况下,我们还是希望用到可以分组并实现收缩的列表,那就要用到android的ExpandableListView,今天研究了一下这个的用法,也参考了很多资料动手写了一个小demo,实现了基本的功能,下面直接上效果图以及源代码~!
本文效果:
一、实现原理
1、首先必须在布局文件中定义一个ExpandableListView
2、其次创建一级条目对应的布局文件group
3、创建二级条目对应的布局文件child
4、加载ExpandableListView组件的Activity必须继承自ExpandableListActivity
二、布局与代码1、首先在主布局中activity_main.xml
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
2、其次在drawable文件夹定义布局一级列表groups.xml
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
android:id="@+id/textGroup"
android:layout_width=