?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"
ExpandableListView
android:id="@+id/ex_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="false"
/
/LinearLayout
2、一级显示的界面描述文件group.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"
TextView
android:id="@+id/txt_group"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="60px"
android:paddingTop="10px"
android:paddingBottom="10px"
android:textSize="26sp"
android:text="No data"
/
/LinearLayout
3、二级显示的界面描述文件child.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"
TextView
android:id="@+id/txt_child"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="50px"
android:paddingTop="5px"
android:paddingBottom="5px"
android:textSize="20sp"
android:text="No data"
/
/LinearLayout
4、编写活动程序Main.java
package com.yazhou.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 Main extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 获得二级列表的控件
ExpandableListView expandable_listview = (ExpandableListView) findViewById(R.id.ex_list);
// 为一级显示添加数据
List<MAP> list_group = new ArrayList<MAP>();
for (int i = 0; i < 2; i++) {
Map map_group = new HashMap();
map_group.put("group", "group" + i);
list_group.add(map_group);
}
// 为一级显示的条目一添加二级数据
List<MAP> list_child1 = new ArrayList<MAP>();
for (int i = 0; i < 2; i++) {
Map map_child1 = new HashMap();
map_child1.put("child", "child" + i);
list_child1.add(map_child1);
}
// 为一级显示的条目二添加二级数据
List<MAP> list_child2 = new ArrayList<MAP>();
Map map_child2 = new HashMap();
map_child2.put("child", "child2");
list_child2.add(map_child2);
// 把二级数据添加到集合
List<LIST<MAP>> list_childs = new ArrayList<LIST<MAP>>();
list_childs.add(list_child1);
list_childs.add(list_child2);
// 把一级数据和二级数据绑定到SimpleExpandableListAdapter对象
SimpleExpandableListAdapter expandable_listadapter = new SimpleExpandableListAdapter(
Main.this, list_group, R.layout.group,
new String[] { "group" }, new int[] { R.id.txt_group },
list_childs, R.layout.child, new String[] { "child" },
new int[] { R.id.txt_child });
//将SimpleExpandableListAdapter对象设置到二级列表控件
expandable_listview.setAdapter(expandable_listadapter);
}
}
LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
ExpandableListView
android:id="@+id/ex_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="false"
/
/LinearLayout
2、一级显示的界面描述文件group.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"
TextView
android:id="@+id/txt_group"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="60px"
android:paddingTop="10px"
android:paddingBottom="10px"
android:textSize="26sp"
android:text="No data"
/
/LinearLayout
3、二级显示的界面描述文件child.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"
TextView
android:id="@+id/txt_child"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="50px"
android:paddingTop="5px"
android:paddingBottom="5px"
android:textSize="20sp"
android:text="No data"
/
/LinearLayout
4、编写活动程序Main.java
package com.yazhou.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 Main extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 获得二级列表的控件
ExpandableListView expandable_listview = (ExpandableListView) findViewById(R.id.ex_list);
// 为一级显示添加数据
List<MAP> list_group = new ArrayList<MAP>();
for (int i = 0; i < 2; i++) {
Map map_group = new HashMap();
map_group.put("group", "group" + i);
list_group.add(map_group);
}
// 为一级显示的条目一添加二级数据
List<MAP> list_child1 = new ArrayList<MAP>();
for (int i = 0; i < 2; i++) {
Map map_child1 = new HashMap();
map_child1.put("child", "child" + i);
list_child1.add(map_child1);
}
// 为一级显示的条目二添加二级数据
List<MAP> list_child2 = new ArrayList<MAP>();
Map map_child2 = new HashMap();
map_child2.put("child", "child2");
list_child2.add(map_child2);
// 把二级数据添加到集合
List<LIST<MAP>> list_childs = new ArrayList<LIST<MAP>>();
list_childs.add(list_child1);
list_childs.add(list_child2);
// 把一级数据和二级数据绑定到SimpleExpandableListAdapter对象
SimpleExpandableListAdapter expandable_listadapter = new SimpleExpandableListAdapter(
Main.this, list_group, R.layout.group,
new String[] { "group" }, new int[] { R.id.txt_group },
list_childs, R.layout.child, new String[] { "child" },
new int[] { R.id.txt_child });
//将SimpleExpandableListAdapter对象设置到二级列表控件
expandable_listview.setAdapter(expandable_listadapter);
}
}