有时候,使用ListView并不能满足应用程序所需要的功能。有些应用程序需要多组ListView,这时候我们就要使用一种新的控件ExpandableListView——可以扩展的ListView。
下面介绍一个例子方便我们对其的理解
在OnCreate里面加载数据
// 定义一个List,该List对象为一级条目提供数据
List<Map<String, String>> groups = new ArrayList<Map<String, String>>();
Map<String, String> group1 = new HashMap<String, String>();
Map<String, String> group2 = new HashMap<String, String>();
group1.put("groupto", "group1");
groups.add(group1);
group2.put("groupto", "group2");
groups.add(group2);
// 定义一个List,该对象为第一个一级条目提供二级条目的数据
List<Map<String, String>> child1 = new ArrayList<Map<String, String>>();
Map<String, String> child1Date = new HashMap<String, String>();
child1Date.put("child1", "child1Date");
child1.add(child1Date);
Map<String, String> child2Data = new HashMap<String, String>();
child2Data.put("child1", "child2Data");
child1.add(child2Data);
List<Map<String, String>> child2 = new ArrayList<Map<String, String>>();
Map<String, String> child2Data1 = new HashMap<String, String>();
child2Data1.put("child1", "child2Data1");
child2.add(child2Data1);
Map<String, String> child2Data2 = new HashMap<String, String>();
child2Data2.put("child1", "child2Data2");
child2.add(child2Data2);
// 定义一个List,这个List对象那个用来存放所有的二级条目
List<List<Map<String, String>>> childs = new ArrayList<List<Map<String, String>>>();
childs.add(child1);
childs.add(child2);
// 生成一个SimpleExpandableListAdapter对象
// 1.第一个参数意思是上下文
// 2.第二个参数用来设置一条目的数据
// 3.第三个参数用来设置一级条目样式的布局文件
// 4.第四个参数用来指定一级条目的key
// 5.第五个参数用来指定一级条目数据显示控件的id
// 6.第六个参数用来指定二级条目的数据
// 7.第七个参数用来设置二级条目样式的布局文件
// 8.第八个参数用来指定二级条目数据的key
// 9.第九个参数用来指定二级条目显示控件的id
SimpleExpandableListAdapter sela = new SimpleExpandableListAdapter(
this, groups, R.layout.group, new String[] { "groupto" },
new int[] { R.id.groupto }, childs, R.layout.child,
new String[] { "child1" }, new int[] { R.id.childto });
setListAdapter(sela);
下面介绍一下它的布局:主要的布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<ExpandableListView
android:id="@id/android:list"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@id/android:empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="No data" />
</LinearLayout>
其次就是其它数据加载的布局如:
group
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/groupto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="26sp"
android:text="No data"/>
</LinearLayout>
还有就是:
child
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/childto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="26sp"
android:text="No data"/>
</LinearLayout>
下面见效果图