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="fill_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="false"
></ExpandableListView>
</LinearLayout>
<!-- 注:ExtandableListView 的id是调用系统的是andorid:list
它属性:drawSelectorOnTop的意思是选中后是否用橘黄色覆盖它原来的字体
-->
group.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" >
<TextView
android:id="@+id/groupTo"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="60px"
android:paddingTop="10px"
android:paddingBottom="10px"
android:textSize="26sp"
android:text="No data" />
</LinearLayout>
<!-- 注:这是一级科目,既是可以点的那些控件 -->
child.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" >
<TextView
android:id="@+id/childTo"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="50px"
android:paddingTop="5px"
android:paddingBottom="5px"
android:textSize="20sp"
android:text="No data" />
</LinearLayout>
<!-- 注:这是二级科目,既是可以点的那些控件后出现的那些list -->
Acitivity
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.ExpandableListActivity;
import android.os.Bundle;
import android.widget.SimpleExpandableListAdapter;
//继承的是ExpandaableListActivity
public class ExpandableListAdapterActivity extends ExpandableListActivity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//准备数据为Adapter
//一级数据为grops
List<Map<String,String>> groups=new ArrayList<Map<String,String>>();
Map<String,String> group1=new HashMap<String,String>();
group1.put("group", "group1"); //一级数据的key为group
groups.add(group1);
Map<String,String> group2=new HashMap<String,String>();
group2.put("group", "group2");
groups.add(group2);
//二级数据的key为child
List<Map<String,String>> childs=new ArrayList<Map<String,String>>();
Map<String,String> child1data1=new HashMap<String,String>();
child1data1.put("child", "child1data1"); //childs 为一级科目groups的group1的子目录
Map<String,String> child1data2=new HashMap<String,String>();
child1data2.put("child", "child1data2");
childs.add(child1data1);
childs.add(child1data2);
List<Map<String,String>> childs1=new ArrayList<Map<String,String>>();
Map<String,String> child2data=new HashMap<String,String>();
child2data.put("child", "child2data");//childs1 为一级科目groups的group2的子目录
childs1.add(child2data);
//childss为二级数据
List<List<Map<String,String>>> childss=new ArrayList<List<Map<String,String>>>();
childss.add(childs);
childss.add(childs1);
//SimpleExpandableListAdapter的九大参数
//1.context
//2.一级条目数据 groups
//3.一级条目布局R.layout.group
//4.一级条目数据的key
//5.一级条目数据设置控件的id
//6.二级条目数据childss
//7.二级条目的布局
//8.二级条目数据的KEY
//9.二级条目数据设置控件的id
SimpleExpandableListAdapter sela=new SimpleExpandableListAdapter
(this, groups, R.layout.group, new String[]{"group"}, new int[]{R.id.groupTo},childss,R.layout .child , new String[]{"child"},
new int[]{R.id.childTo });
//设置Adapterer
this.setListAdapter(sela);
}
}