Android ExpandableListActivity 学习札记
Android ExpandableListActivity 学习笔记转载自http://blog.csdn.net/xiangyong2008/archive/2010/03/06/
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绑定数据:
?
public?class?ExpandableList3?extends?ExpandableListActivity?{??????private?static?final?String?NAME?=?"NAME";??????private?static?final?String?IS_EVEN?=?"IS_EVEN";????????????private?ExpandableListAdapter?mAdapter;????????????@Override??????public?void?onCreate(Bundle?savedInstanceState)?{??????????super.onCreate(savedInstanceState);????????????List<Map<String,?String>>?groupData?=?new?ArrayList<Map<String,?String>>();??????????List<List<Map<String,?String>>>?childData?=?new?ArrayList<List<Map<String,?String>>>();??????????for?(int?i?=?0;?i?<?20;?i++)?{??????????????Map<String,?String>?curGroupMap?=?new?HashMap<String,?String>();??????????????groupData.add(curGroupMap);??????????????curGroupMap.put(NAME,?"Group?"?+?i);??????????????curGroupMap.put(IS_EVEN,?(i?%?2?==?0)???"This?group?is?even"?:?"This?group?is?odd");????????????????????????????List<Map<String,?String>>?children?=?new?ArrayList<Map<String,?String>>();??????????????for?(int?j?=?0;?j?<?15;?j++)?{??????????????????Map<String,?String>?curChildMap?=?new?HashMap<String,?String>();??????????????????children.add(curChildMap);??????????????????curChildMap.put(NAME,?"Child?"?+?j);??????????????????curChildMap.put(IS_EVEN,?(j?%?2?==?0)???"This?child?is?even"?:?"This?child?is?odd");??????????????}??????????????childData.add(children);??????????}????????????????????//?Set?up?our?adapter??????????mAdapter?=?new?SimpleExpandableListAdapter(??????????????????this,??????????????????groupData,??//?存储父list的数据??????????????????android.R.layout.simple_expandable_list_item_2,?//父list的现实方式??????????????????new?String[]?{?NAME,IS_EVEN},????????????????????//?父list需要显示的数据??????????????new?int[]?{?android.R.id.text1,android.R.id.text2},?//?父list的数据绑定到的view??????????????????childData,??????????????????????????????????????//子list的数据??????????????????android.R.layout.simple_expandable_list_item_2,??????????????????new?String[]?{?NAME,?IS_EVEN?},??????????????????new?int[]?{?android.R.id.text1,?android.R.id.text2?}??????????????????);??????????setListAdapter(mAdapter);??????}????}???
?
?
注意: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中)文件的布局如下:
?
<?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:orientation="vertical">??????????<TextView?android:id="@+id/text1"???????????android:textSize="16sp"???????????android:textStyle="bold"???????????android:layout_width="fill_parent"???????????android:layout_height="wrap_content"/>??????????<TextView?android:id="@+id/text2"???????????android:textSize="16sp"???????????android:layout_width="fill_parent"???????????android:layout_height="wrap_content"/>???</LinearLayout>???
示例2—通过SimpleCussorTreeAdapter绑定数据:
view plaintext-decoration: none; color: #a0a0a0; background-image: none; background-color: inherit; margin-top: