android ExpandableListView可扩展列表

来源:互联网 发布:golang 程序假死 编辑:程序博客网 时间:2024/05/10 22:06

转载自http://leiwuluan.iteye.com/blog/1508356

先看一效果图、

列表中要有 图片和文字:


 

 

 

 

所以我们要实现一个自定义的   适配器。

 

介绍一个类:BaseExpandableListAdapter   

一看就知道是 适配器的一个基类了。

 

 

所以我们自定义的适配器要 继承它。

 

除了 完成这个 适配器,还要有两个自定义模板,分别   组和子列表的,单元模板。如下图:


 

 

 

模板布局xml  要放在 layouts  下面。

 

main_tree_group.xml

Xml代码 复制代码 收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="45dp"  
  5.     android:background="@color/white"  
  6.     android:gravity="center"  
  7.     android:orientation="vertical" >  
  8.   
  9.     <TextView  
  10.         xmlns:android="http://schemas.android.com/apk/res/android"  
  11.         android:id="@+id/main_tree_title_id"  
  12.         android:layout_width="fill_parent"  
  13.         android:layout_height="wrap_content"  
  14.         android:layout_marginLeft="20dip"  
  15.         android:background="@color/white"  
  16.         android:text="NoData"  
  17.         android:textColor="@color/black"  
  18.         android:textSize="20dp"  
  19.         android:textStyle="bold" />  
  20.   
  21. </LinearLayout>  

 

 

子列表模板文件

main_tree_child.xml

Xml代码 复制代码 收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="wrap_content"  
  5.     android:background="@color/white"  
  6.     android:gravity="center_vertical"  
  7.     android:orientation="horizontal"  
  8.     android:paddingBottom="5dp"  
  9.     android:paddingLeft="8dp"  
  10.     android:paddingTop="8dp" >  
  11.   
  12.     <ImageView  
  13.         android:id="@+id/mainChildIcoId"  
  14.         android:layout_width="50dp"  
  15.         android:layout_height="50dp"  
  16.         android:src="@drawable/person_icon" />  
  17.   
  18.     <LinearLayout  
  19.         android:layout_width="wrap_content"  
  20.         android:layout_height="fill_parent"  
  21.         android:orientation="vertical" >  
  22.   
  23.         <TextView  
  24.             android:id="@+id/mainChildText1"  
  25.             android:layout_width="fill_parent"  
  26.             android:layout_height="wrap_content"  
  27.             android:layout_marginLeft="10dp"  
  28.             android:layout_marginTop="5dp"  
  29.             android:background="@color/white"  
  30.             android:gravity="center_vertical"  
  31.             android:text="CNoData"  
  32.             android:textColor="@color/black"  
  33.             android:textSize="16dp" />  
  34.   
  35.         <TextView  
  36.             android:id="@+id/mainChildText2"  
  37.             android:layout_width="fill_parent"  
  38.             android:layout_height="wrap_content"  
  39.             android:layout_marginLeft="10dp"  
  40.             android:background="@color/white"  
  41.             android:gravity="center_vertical"  
  42.             android:text="13693668970"  
  43.             android:textColor="@color/black"  
  44.             android:textSize="12dp" />  
  45.     </LinearLayout>  
  46.   
  47. </LinearLayout>  

 

 

 

 

然后写两个对  模板文件的    bean

 

main_tree_group.xml 对应 bean

Java代码 复制代码 收藏代码
  1. //父单元  
  2. class ExpandableGroupHolder {  
  3.     TextView title;  
  4. }   
 

main_tree_child.xml

 

Java代码 复制代码 收藏代码
  1. //单元类  
  2. class ExpandableListHolder {  
  3.     TextView nickName;  
  4.     TextView phone;  
  5.     ImageView ioc;  
  6. }   
 

 

现在来实现最重要的关结。  适配器  

MainListExpandableListAdapter.java

 

我这里把 上面两个模板对应java bean 写成 自定义适配器的内部类。

0 0
原创粉丝点击