ExpandableListView
ExpandableListViewActivity
package org.wp.activity;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ExpandableListView;
import android.widget.SimpleExpandableListAdapter;
public class ExpandableListViewActivity extends Activity {
private ExpandableListView expandableListView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
expandableListView = (ExpandableListView) this
.findViewById(R.id.expandableListViewId);
// 一级条目的数据
List<Map<String, String>> groups = new ArrayList<Map<String, String>>();
Map<String, String> group1 = new HashMap<String, String>();
group1.put("week", "星期一");
Map<String, String> group2 = new HashMap<String, String>();
group2.put("week", "星期二");
groups.add(group1);
groups.add(group2);
// 二级条目的数据
List<List<Map<String, String>>> childs = new ArrayList<List<Map<String, String>>>();
// 第一个一级条目所包含的二级条目数据
List<Map<String, String>> group1Child = new ArrayList<Map<String, String>>();
Map<String, String> group1ChindData1 = new HashMap<String, String>();
group1ChindData1.put("course", "英语");
Map<String, String> group1ChindData2 = new HashMap<String, String>();
group1ChindData2.put("course", "数学");
Map<String, String> group1ChindData3 = new HashMap<String, String>();
group1ChindData3.put("course", "语文");
group1Child.add(group1ChindData1);
group1Child.add(group1ChindData2);
group1Child.add(group1ChindData3);
// 第二个一级条目所包含的二级条目数据
List<Map<String, String>> group2Child = new ArrayList<Map<String, String>>();
Map<String, String> group2ChildData1 = new HashMap<String, String>();
group2ChildData1.put("course", "语文");
Map<String, String> group2ChildData2 = new HashMap<String, String>();
group2ChildData2.put("course", "化学");
Map<String, String> group2ChildData3 = new HashMap<String, String>();
group2ChildData3.put("course", "物理");
group2Child.add(group2ChildData1);
group2Child.add(group2ChildData2);
group2Child.add(group2ChildData3);
childs.add(group1Child);
childs.add(group2Child);
SimpleExpandableListAdapter sela = new SimpleExpandableListAdapter(
this, groups, R.layout.group, new String[] { "week" },
new int[] { R.id.groupView }, childs, R.layout.child,
new String[] { "course" }, new int[] { R.id.childView });
expandableListView.setAdapter(sela);
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <!-- android:drawSelectorOnTop setDrawSelectorOnTop(boolean) When set to true, the selector will be drawn over the selected item. --> <ExpandableListView android:id="@+id/expandableListViewId" android:layout_width="fill_parent" android:layout_height="fill_parent" android:drawSelectorOnTop="false" /> <!-- @id/android:empty 当ExpandableListView里面没有条目时在这个布局里面 这个TextView将显示 --> <TextView android:id="@id/android:empty" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="no data" /> </LinearLayout>
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="fill_parent" > <TextView android:id="@+id/groupView" android:layout_width="fill_parent" android:layout_height="fill_parent" android:paddingLeft="60px" android:paddingTop="10px" android:paddingBottom="10px" android:textSize="26sp" android:text="no data" /> </LinearLayout>
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="fill_parent" > <TextView android:id="@+id/childView" android:layout_width="fill_parent" android:layout_height="fill_parent" android:paddingLeft="50px" android:paddingTop="5px" android:paddingBottom="5px" android:textSize="20sp" android:text="no data" /> </LinearLayout>