@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_expandable_list);
//定义一个List 该List 为为一级条目提供数据
List<Map<String,String>>groups =new ArrayList<Map<String,String>>();
Map<String,String> group1=new HashMap<String,String>();
group1.put("group", "group1");
Map<String,String> group2= new HashMap<String,String>();
group2.put("group", "group2");
groups.add(group1);
groups.add(group2);
//定义一个List,该List对象为第一个一级条目的子条目
List<Map<String,String>>child1=new ArrayList<Map<String,String>>();
Map<String,String> child1Data1=new HashMap<String,String>();
child1Data1.put("child", "child1Data1");
Map<String,String> child1Data2=new HashMap<String,String>();
child1Data2.put("child", "child1Data2");
child1.add(child1Data1);
child1.add(child1Data2);
//定义一个List,该List对象为第二个一级条目的子条目
List<Map<String,String>>child2=new ArrayList<Map<String,String>>();
Map<String,String> child2Data1=new HashMap<String,String>();
child2Data1.put("child", "child2Data1");
Map<String,String> child2Data2=new HashMap<String,String>();
child2Data2.put("child", "child2Data2");
child2.add(child2Data1);
child2.add(child2Data2);
List<List<Map<String,String>>> childs =new ArrayList<List<Map<String,String>>>();
childs.add(child1);
childs.add(child2);
SimpleExpandableListAdapter asla =new SimpleExpandableListAdapter
(this,
groups,
R.layout.group,
new String[] {"group"},
new int[]{R.id.groupTo},
childs,
R.layout.child,
new String[]{"child"},
new int[] {R.id.childTo});
setListAdapter(asla);
}
layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ExpandableListView
android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="false"
/>
<TextView
android:id="@id/android:empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="No data" />
</LinearLayout>
grouplayout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:id="@+id/groupTo"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="60px"
android:paddingTop="10px"
android:textSize="26sp"
android:text="No data" />
</LinearLayout>
childLayout:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:id="@+id/childTo"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="50px"
android:paddingTop="5px"
android:textSize="20sp"
android:text="No data" />
</LinearLayout>