一.简单示例
src
public class AndroidUIActivity extends ExpandableListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 准备顶层列表数据
List<Map<String, String>> topList = new ArrayList<Map<String, String>>();
Map<String, String> topMap1 = new HashMap<String, String>();
Map<String, String> topMap2 = new HashMap<String, String>();
Map<String, String> topMap3 = new HashMap<String, String>();
topMap1.put("month", "三月测评项");
topMap2.put("month", "四月测评项");
topMap3.put("month", "五月测评项");
topList.add(topMap1);
topList.add(topMap2);
topList.add(topMap3);
// 准备二层列表数据
List<List<Map<String, String>>> nestList = new ArrayList<List<Map<String, String>>>();
// 准备二层列表第一个子列表数据
List<Map<String, String>> nestList1 = new ArrayList<Map<String, String>>();
Map<String, String> nestMap1 = new HashMap<String, String>();
Map<String, String> nestMap2 = new HashMap<String, String>();
Map<String, String> nestMap3 = new HashMap<String, String>();
nestMap1.put("test", "看手");
nestMap2.put("test", "吃手");
nestMap3.put("test", "玩手");
nestList1.add(nestMap1);
nestList1.add(nestMap2);
nestList1.add(nestMap3);
// 准备二层列表第二个子列表数据
List<Map<String, String>> nestList2 = new ArrayList<Map<String, String>>();
Map<String, String> nestMap4 = new HashMap<String, String>();
Map<String, String> nestMap5 = new HashMap<String, String>();
nestMap4.put("test", "翻身");
nestMap5.put("test", "辨别声音来源方位");
nestList2.add(nestMap4);
nestList2.add(nestMap5);
// 准备二层列表第三个子列表数据
List<Map<String, String>> nestList3 = new ArrayList<Map<String, String>>();
Map<String, String> nestMap6 = new HashMap<String, String>();
Map<String, String> nestMap7 = new HashMap<String, String>();
nestMap6.put("test", "你好");
nestMap7.put("test", "你坏");
nestList3.add(nestMap6);
nestList3.add(nestMap7);
// 把子列表数据放入
nestList.add(nestList1);
nestList.add(nestList2);
nestList.add(nestList3);
// 准备数据匹配器
SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter(
this, // 1.上下文
topList, // 2.顶层数据列表
android.R.layout.simple_expandable_list_item_1, // 3.一层显示样式
new String[] { "month" }, // 4.顶层map的键
new int[] { android.R.id.text1 }, // 5.顶层数据显示的View ID
nestList, // 6.二层数据列表
android.R.layout.simple_list_item_1, // 7.二层显示样式
new String[] { "test"}, // 8.二层map的键
new int[] { android.R.id.text1 } // 9.二层数据显示的View ID
);
// 设置数据匹配器
this.setListAdapter(adapter);
}
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
Toast.makeText(this,
"嵌套列表被点击,顶层列表定位" + groupPosition + "二层列表定位" + childPosition,
Toast.LENGTH_SHORT).show();
return super.onChildClick(parent, v, groupPosition, childPosition, id);
}
@Override
public void onGroupCollapse(int groupPosition) {
Toast.makeText(this, "顶层列表收缩,列表定位" + groupPosition, Toast.LENGTH_SHORT)
.show();
super.onGroupCollapse(groupPosition);
}
@Override
public void onGroupExpand(int groupPosition) {
Toast.makeText(this, "顶层列表展开,列表定位" + groupPosition, Toast.LENGTH_SHORT)
.show();
super.onGroupExpand(groupPosition);
}
}
main.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"
android:orientation="vertical" >
<ExpandableListView
android:id="@id/android:list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ExpandableListView>
</LinearLayout>
二. 运行结果
启动
展开列表三
点击列表三子列表1