效果是酱紫的!!
main.xml
<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"
tools:context="${relativePackage}.${activityClass}" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<ExpandableListView
android:id="@id/android:list" //这句很重要,没有这句运行会报错
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
<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"
tools:context="${relativePackage}.${activityClass}" >
<TextView
android:id="@+id/group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="26sp"
/>
</LinearLayout>
child.xml
<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"
tools:context="${relativePackage}.${activityClass}" >
<TextView
android:id="@+id/child"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
/>
</LinearLayout>
mainActivity.java
package com.fzq.expandblelistview;
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;
//必须继承ExpandableListActivity
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> 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为第一个一级栏目提供二级栏目数据
List<Map<String, String>> child1=new ArrayList<Map<String,String>>();
Map<String, String>childdata1=new HashMap<String, String>();
childdata1.put("child", "childvalue11");
child1.add(childdata1);
Map<String, String>childdata2=new HashMap<String, String>();
childdata2.put("child", "childvalue12");
child1.add(childdata2);
//定一个List,该List为第二个一级栏目提供耳机栏目数据
List<Map<String, String>> child2=new ArrayList<Map<String,String>>();
Map<String, String> childdata = new HashMap<String, String>();
childdata.put("child", "childvalue21");
child2.add(childdata);
//定一个List,该对象用来存储所有的二级条目数据
List<List<Map<String, String>>> childs=new ArrayList<List<Map<String,String>>>();
childs.add(child1);
childs.add(child2);
/*
*
* context ->上下文对象
* groupData ->一级条目数据
* expandedGroupLayout, ->用来设置一级条目样式的布局文件
* collapsedGroupLayout, ->用来指定一级
* groupFrom, ->指定一级条目数据的key
* groupTo, -> 指定一级条目数据显控件的id
* childData, -> 指定二级条目数据
* childLayout, -> 二级条目样式的布局文件
* childFrom, -> 二级条目数据的key
* childTo -> 二级条目数据显示控件的id
*/
SimpleExpandableListAdapter selsAdapter=new SimpleExpandableListAdapter(MainActivity.this,
groups, R.layout.group, new String[]{"group"},new int[]{R.id.group} ,
childs, R.layout.child, new String[]{"child"}, new int[]{R.id.child});
setListAdapter(selsAdapter);
}
}