1、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=".Activity01" >
<ExpandableListView
android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="false"/>
<TextView
android:id="@id/android:empty"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="NO data!"/>
<!-- android:drawSelectorOnTop="false" 是选项高亮部分不覆盖文字 -->
</RelativeLayout>
2、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:paddingLeft="60px"
android:textSize="26sp"
android:text="NO DATA!"/>
</LinearLayout>
3、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:paddingLeft="60px"
android:textSize="20sp"
android:text="NO DATA!"/>
</LinearLayout>
4、Activity01.java代码:
package mars.expandablelistactivity;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.os.Bundle;
import android.widget.SimpleExpandableListAdapter;
import android.app.ExpandableListActivity;
public class Activity01 extends ExpandableListActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//定义一个List,为一级条目提供数据
List<Map<String,String>> groups=new ArrayList<Map<String,String>>();
Map<String,String> group1=new HashMap<String,String>();
group1.put("group", "group1");
Map<String,String> group2=new HashMap<String,String>();
group2.put("group", "group2");
groups.add(group1);
groups.add(group2);
//定义一个List,为第一个条目增加二级条目的数据
List<Map<String,String>> child1=new ArrayList<Map<String,String>>();
Map<String,String> childDate1=new HashMap<String,String>();
childDate1.put("child", "childDate1");
Map<String,String> childDate2=new HashMap<String,String>();
childDate2.put("child", "childDate2");
child1.add(childDate1);
child1.add(childDate2);
//定义一个List,为第二个条目增加二级条目的数据
List<Map<String,String>> child2=new ArrayList<Map<String,String>>();
Map<String,String> childDate3=new HashMap<String,String>();
childDate3.put("child", "childDate1");
child2.add(childDate3);
//定义一个List,存储所有二级条目的数据
List<List<Map<String,String>>> childs=new ArrayList<List<Map<String,String>>>();
childs.add(child1);
childs.add(child2);
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);
}
}
5、运行效果: