Android ExpandableListActivity 学习笔记

转载自http://blog.csdn.net/xiangyong2008/archive/2010/03/06/5351969.aspx

ExpandableListActivity:

   An activity that displays an expandable list of items by binding to a data source implementing the ExpandableListAdapter, and exposes event handlers when the user selects an item.

  即,可扩展的list,单击某个item后,又可显示一个子list。它的数据通过绑定到ExpandableListAdapter或者ExpandableListAdapter的子类上。

示例1—通过SimpelExpandableListAdapter绑定数据:

 

  1. public class ExpandableList3 extends ExpandableListActivity {  
  2.     private static final String NAME = "NAME";  
  3.     private static final String IS_EVEN = "IS_EVEN";  
  4.       
  5.     private ExpandableListAdapter mAdapter;  
  6.       
  7.     @Override  
  8.     public void onCreate(Bundle savedInstanceState) {  
  9.         super.onCreate(savedInstanceState);  
  10.   
  11.         List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();  
  12.         List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>();  
  13.         for (int i = 0; i < 20; i++) {  
  14.             Map<String, String> curGroupMap = new HashMap<String, String>();  
  15.             groupData.add(curGroupMap);  
  16.             curGroupMap.put(NAME, "Group " + i);  
  17.             curGroupMap.put(IS_EVEN, (i % 2 == 0) ? "This group is even" : "This group is odd");  
  18.               
  19.             List<Map<String, String>> children = new ArrayList<Map<String, String>>();  
  20.             for (int j = 0; j < 15; j++) {  
  21.                 Map<String, String> curChildMap = new HashMap<String, String>();  
  22.                 children.add(curChildMap);  
  23.                 curChildMap.put(NAME, "Child " + j);  
  24.                 curChildMap.put(IS_EVEN, (j % 2 == 0) ? "This child is even" : "This child is odd");  
  25.             }  
  26.             childData.add(children);  
  27.         }  
  28.           
  29.         // Set up our adapter  
  30.         mAdapter = new SimpleExpandableListAdapter(  
  31.                 this,  
  32.                 groupData,  // 存储父list的数据  
  33.                 android.R.layout.simple_expandable_list_item_2, //父list的现实方式  
  34.                 new String[] { NAME,IS_EVEN},                    // 父list需要显示的数据  
  35.             new int[] { android.R.id.text1,android.R.id.text2}, // 父list的数据绑定到的view  
  36.                 childData,                                      //子list的数据  
  37.                 android.R.layout.simple_expandable_list_item_2,  
  38.                 new String[] { NAME, IS_EVEN },  
  39.                 new int[] { android.R.id.text1, android.R.id.text2 }  
  40.                 );  
  41.         setListAdapter(mAdapter);  
  42.     }  
  43.   
  44. }  

 

 

 

注意:android.R.layout.simple_expandable_list_item_1:表示只显示一个TextView的数据,即R.id.text1

          android.R.layout.simple_expandable_list_item_2:表示显示二个TextView的数据,即R.id.text1,R.id,text2

         android.R.layout.simple_expandable_list_item_2.xml(在R.layout中)文件的布局如下:

 

  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:orientation="vertical">  
  6.    
  7.      <TextView android:id="@+id/text1"  
  8.          android:textSize="16sp"  
  9.          android:textStyle="bold"  
  10.          android:layout_width="fill_parent"  
  11.          android:layout_height="wrap_content"/>  
  12.    
  13.      <TextView android:id="@+id/text2"  
  14.          android:textSize="16sp"  
  15.          android:layout_width="fill_parent"  
  16.          android:layout_height="wrap_content"/>  
  17.  </LinearLayout>  

 

示例2—通过SimpleCussorTreeAdapter绑定数据:

你可能感兴趣的:(android,xml,.net,J#,UP)