转载自http://leiwuluan.iteye.com/blog/1508356
先看一效果图、
列表中要有 图片和文字:
所以我们要实现一个自定义的 适配器。
介绍一个类:BaseExpandableListAdapter
一看就知道是 适配器的一个基类了。
所以我们自定义的适配器要 继承它。
除了 完成这个 适配器,还要有两个自定义模板,分别 组和子列表的,单元模板。如下图:
模板布局xml 要放在 layouts 下面。
main_tree_group.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="45dp"
- android:background="@color/white"
- android:gravity="center"
- android:orientation="vertical" >
- <TextView
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/main_tree_title_id"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_marginLeft="20dip"
- android:background="@color/white"
- android:text="NoData"
- android:textColor="@color/black"
- android:textSize="20dp"
- android:textStyle="bold" />
- </LinearLayout>
子列表模板文件
main_tree_child.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:background="@color/white"
- android:gravity="center_vertical"
- android:orientation="horizontal"
- android:paddingBottom="5dp"
- android:paddingLeft="8dp"
- android:paddingTop="8dp" >
- <ImageView
- android:id="@+id/mainChildIcoId"
- android:layout_width="50dp"
- android:layout_height="50dp"
- android:src="@drawable/person_icon" />
- <LinearLayout
- android:layout_width="wrap_content"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <TextView
- android:id="@+id/mainChildText1"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_marginLeft="10dp"
- android:layout_marginTop="5dp"
- android:background="@color/white"
- android:gravity="center_vertical"
- android:text="CNoData"
- android:textColor="@color/black"
- android:textSize="16dp" />
- <TextView
- android:id="@+id/mainChildText2"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_marginLeft="10dp"
- android:background="@color/white"
- android:gravity="center_vertical"
- android:text="13693668970"
- android:textColor="@color/black"
- android:textSize="12dp" />
- </LinearLayout>
- </LinearLayout>
然后写两个对 模板文件的 bean
main_tree_group.xml 对应 bean
- //父单元
- class ExpandableGroupHolder {
- TextView title;
- }
//父单元
class ExpandableGroupHolder {
TextView title;
}
main_tree_child.xml
- //单元类
- class ExpandableListHolder {
- TextView nickName;
- TextView phone;
- ImageView ioc;
- }
//单元类
class ExpandableListHolder {
TextView nickName;
TextView phone;
ImageView ioc;
}
现在来实现最重要的关结。 适配器
MainListExpandableListAdapter.java
我这里把 上面两个模板对应java bean 写成 自定义适配器的内部类。