1.在图形界面创建二级列表控件,此控件名为ExpandableList,在Comosite子选单下。
<ExpandableListView
android:id="@+id/expandableListView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" >
</ExpandableListView></span>
2.在布局文件中声明group样式。
<TextView
android:id="@+id/tv_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="35dp"
android:textSize="20sp"
android:text="TextView" /></span>
3.在布局文件中声明child样式。
<TextView
android:id="@+id/tv_child"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="35dp"
android:textSize="20sp"
android:text="TextView" /></span>
4.创建一个链表groups,以维护map类型的数据。然后创建map类型的数据作为组名,键值均为group,加入到map的链表中。
List<Map<String,String>> groups = new ArrayList<Map<String,String>>();
Map<String,String> group1 = new HashMap<String,String>();
group1.put("group","蜀国");
Map<String,String> group2 = new HashMap<String,String>();
group2.put("group","魏国");
groups.add(group1);
groups.add(group2);</span>
5.创建一个二维链表,维护map类型的数据,二维链表的第一维对应着第几组,第二维对应着相应组的数据集。然后将数据传进去。
List<List<Map<String,String>>> childs = new ArrayList<List<Map<String,String>>>();
List<Map<String,String>> child1 = new ArrayList<Map<String,String>>();
Map<String,String> child1data1 = new HashMap<String, String>();
child1data1.put("child", "张翼德");
Map<String,String> child1data2 = new HashMap<String,String>();
child1data2.put("child", "关云长");
child1.add(child1data1);
child1.add(child1data2);
childs.add(child1);
List<Map<String,String>> child2 = new ArrayList<Map<String,String>>();
Map<String,String> child2data1 =new HashMap<String, String>();
Map<String,String> child2data2 =new HashMap<String,String>();
child2data1.put("child", "司马懿");
child2data2.put("child","夏侯渊");
child2.add(child2data1);
child2.add(child2data2);
childs.add(child2);</span>
6.建立一个SimpleExpandableListAdapter适配器,并对数据进行处理。适配器参数
arg1:当前对象
arg2:组数据
arg3:组布局文件
arg4:组中map的key数组
arg5:组布局文件中textview的id数组
arg6:子项数据
arg7:子项布局文件
arg8:子项布局文件中map的key数组
arg9:子项布局文件中textview的id数组
SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter(
this,
groups,
R.layout.group,
new String[]{"group"},
new int[]{R.id.tv_group},
childs,
R.layout.child,
new String[]{"child"},
new int[]{R.id.tv_child});</span>
7.建立ExpandableListView对象,并将适配器的值传进去。
ExpandableListView elv = (ExpandableListView) findViewByID(R.id.expandableListView1);
elv.setAdapter(adapter);</span>