Android学习笔记(十八)——ExpandableListActivity与SimpleExpandableListAdapter

ExpandableListActivity用于显示组列表,显示效果如下图:


组列表和列表在原理上是相似的;

实现组列表的主要步骤有:

1、先建一个继承于ExpandableListActivity的Activity

2、有三个xml布局文件,main.xml中有一个ExpandableListView,代码如下:

[html]  view plain copy
  1. main.xml  
  2. <?xml version="1.0" encoding="UTF-8"?>  
  3.  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  4.          android:orientation="vertical"  
  5.          android:layout_width="fill_parent"  
  6.          android:layout_height="fill_parent"  
  7.          >  
  8.    
  9.      <ExpandableListView android:id="@id/android:list"  
  10.                android:layout_width="fill_parent"   
  11.                android:layout_height="fill_parent"  
  12.                android:drawSelectorOnTop="false"/>  
  13.    
  14.      <TextView android:id="@id/android:empty"  
  15.                android:layout_width="fill_parent"  
  16.                android:layout_height="fill_parent"  
  17.                android:text="No data"/>  
  18.  </LinearLayout>  
  19.    
用于布局一级列表的布局文件group.xml:

[html]  view plain copy
  1. group.xml  
  2. <?xml version="1.0" encoding="UTF-8"?>  
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:orientation="vertical" android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"   
  6.     >  
  7.   
  8.   
  9.     <TextView android:id="@+id/groupTo"   
  10.         android:layout_width="fill_parent"  
  11.         android:layout_height="fill_parent"   
  12.         android:paddingLeft="60px"   
  13.         android:paddingTop="10px"  
  14.         android:paddingBottom="10px"   
  15.         android:textSize="26sp"   
  16.         android:text="No data" />  
  17. </LinearLayout>  
  18.    
用于布局二级列表的布局文件,也即一级列表展开的数据:

[html]  view plain copy
  1. child.xml  
  2. <?xml version="1.0" encoding="UTF-8"?>  
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:orientation="vertical" android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent">  
  6.   
  7.   
  8.     <TextView android:id="@+id/childTo"   
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="fill_parent"   
  11.         android:paddingLeft="50px"   
  12.         android:paddingTop="5px"  
  13.         android:paddingBottom="5px"   
  14.         android:textSize="20sp"   
  15.         android:text="No data" />  
  16. </LinearLayout>  
  17.    

3、最后一步编写Activity中的内容,主要填充列表中的数据,一级列表中数据用一个List表示,里面是Map结构

二级列表中数据也是一个list,里面还存放list数据

[java]  view plain copy
  1. package mars.expandable;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.HashMap;  
  5. import java.util.List;  
  6. import java.util.Map;  
  7.   
  8. import android.app.ExpandableListActivity;  
  9. import android.os.Bundle;  
  10. import android.widget.SimpleExpandableListAdapter;  
  11.   
  12. /* 
  13.  * 创建一个Activity,继承ExpandableListAcitivty 
  14.  */  
  15. public class MainActivity extends ExpandableListActivity {  
  16.     /** Called when the activity is first created. */  
  17.     @Override  
  18.     public void onCreate(Bundle savedInstanceState) {  
  19.         super.onCreate(savedInstanceState);  
  20.         setContentView(R.layout.main);  
  21.         //定义一个List,该List对象为一级条目提供数据  
  22.         List<Map<String, String>> groups = new ArrayList<Map<String, String>>();  
  23.         Map<String, String> group1 = new HashMap<String, String>();  
  24.         group1.put("group""group1");  
  25.         Map<String, String> group2 = new HashMap<String, String>();  
  26.         group2.put("group""group2");  
  27.         groups.add(group1);  
  28.         groups.add(group2);  
  29.           
  30.         //定义一个List,该List对象为第一个一级条目提供二级条目的数据  
  31.         List<Map<String, String>> child1 = new ArrayList<Map<String, String>>();  
  32.         Map<String, String> child1Data1 = new HashMap<String, String>();  
  33.         child1Data1.put("child""child1Data1");  
  34.         child1.add(child1Data1);  
  35.         Map<String,String> child1Data2 = new HashMap<String,String>();  
  36.         child1Data2.put("child""child1Data2");  
  37.         child1.add(child1Data2);  
  38.           
  39.         //定义一个List,该List对象为第二个一级条目提供二级条目的数据  
  40.         List<Map<String, String>> child2 = new ArrayList<Map<String, String>>();  
  41.         Map<String, String> child2Data = new HashMap<String, String>();  
  42.         child2Data.put("child""child2Data");  
  43.         child2.add(child2Data);  
  44.           
  45.         //定义一个List,该List对象用来存储所有的二级条目的数据  
  46.         List<List<Map<String, String>>> childs = new ArrayList<List<Map<String, String>>>();  
  47.         childs.add(child1);  
  48.         childs.add(child2);  
  49.   
  50.         //生成一个SimpleExpandableListAdapter对象  
  51.         //1.context  
  52.         //2.一级条目的数据  
  53.         //3.用来设置一级条目样式的布局文件  
  54.         //4.指定一级条目数据的key  
  55.         //5.指定一级条目数据显示控件的id  
  56.         //6.指定二级条目的数据  
  57.         //7.用来设置二级条目样式的布局文件  
  58.         //8.指定二级条目数据的key  
  59.         //9.指定二级条目数据显示控件的id  
  60.         SimpleExpandableListAdapter sela = new SimpleExpandableListAdapter(  
  61.                 this, groups, R.layout.group, new String[] { "group" },  
  62.                 new int[] { R.id.groupTo }, childs, R.layout.child,  
  63.                 new String[] { "child" }, new int[] { R.id.childTo });  
  64.         //将SimpleExpandableListAdapter对象设置给当前的ExpandableListActivity  
  65.         setListAdapter(sela);  
  66.     }  
  67. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值