如图实现ListView的嵌套
第一步:在布局文件当中声明ExpandableListActivity控件:
main.xml
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ExpandableListView
android:id="@+id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="false" >
</ExpandableListView>
<TextView
android:id="@+id/android:empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="No data" />
</RelativeLayout>
第二步:在布局文件当中声明group的样式:
group.xml
<?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="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="10px"
android:paddingLeft="60px"
android:paddingTop="10px"
android:text="No data"
android:textSize="26sp" />
</LinearLayout>
第三步:在布局文件当中声明子项的样式:
child.xml
<?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="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="50px"
android:paddingLeft="50px"
android:paddingTop="50px"
android:text="No data"
android:textSize="20sp" />
</LinearLayout>
第四步:创建一个Actuvity,继承ExpandableListActivity:
public class MainActivity extends ExpandableListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
第五步:在MainActivity.java里写入监听器,写入ListView
package com.example.acti_02_03_expandable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.os.Bundle;
import android.os.Handler;
import android.R.string;
import android.app.Activity;
import android.app.ExpandableListActivity;
import android.view.Menu;
import android.widget.SimpleAdapter;
import android.widget.SimpleExpandableListAdapter;
public class MainActivity extends ExpandableListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 定义一个List,该List对象为一级条目提供数据
List<Map<String, String>> groups = new ArrayList<Map<String, String>>();
Map<String, String> groups1 = new HashMap<String, String>();
groups1.put("group", "group1");
Map<String, String> groups2 = new HashMap<String, String>();
groups2.put("group", "group2");
groups.add(groups1);
groups.add(groups2);
// 定义一个List,该List对象为第一个一级条目提供二级条目的数据
List<Map<String, String>> chlidList1 = new ArrayList<Map<String, String>>();
Map<String, String> chlid1Data1 = new HashMap<String, String>();
chlid1Data1.put("child", "child1Data1");
Map<String, String> chlid1Data2 = new HashMap<String, String>();
chlid1Data2.put("child", "child1Data2");
chlidList1.add(chlid1Data1);
chlidList1.add(chlid1Data2);
List<Map<String, String>> chlidList2 = new ArrayList<Map<String, String>>();
Map<String, String> chlid2Data1 = new HashMap<String, String>();
chlid2Data1.put("child", "child2Data1");
Map<String, String> chlid2Data2 = new HashMap<String, String>();
chlid2Data2.put("child", "child1Data2");
chlidList2.add(chlid2Data1);
chlidList2.add(chlid2Data2);
List<List<Map<String, String>>> childs = new ArrayList<List<Map<String, String>>>();
childs.add(chlidList1);
childs.add(chlidList2);
// SimpleExpandableListAdapter(context, groupData, expandedGroupLayout,
// collapsedGroupLayout, groupFrom, groupTo, childData, childLayout,
// childFrom, childTo)
//1.context
//2.一级条目的数据
//3.用来设置一级条目样式的布局文件
//4.指定一级条目的key
//5.指定一级条目数据显示控件的id
//6.指定二级条目的数据
//7.用来设置二级条目样式的布局文件
//8.指定二级条目数据的key
//9.指定二级条目数据显示控件的id
SimpleExpandableListAdapter sela = 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(sela);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}