ExpandableListView,是这种样式的
package com.example.expandabledemo;
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.view.Menu;
import android.widget.SimpleExpandableListAdapter;
//创建一个Activity,继承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>child1Data1=new HashMap<String,String>();
child1Data1.put("child1","child1Data1");
child1.add(child1Data1);
Map<String,String>child1Data2=new HashMap<String,String>();
child1Data2.put("child1","child1Data2");
child1.add(child1Data2);
List<Map<String,String>>child2=new ArrayList<Map<String,String>>();
Map<String,String>child2Data=new HashMap<String,String>();
group1.put("child"," child2Data");
child2.add(child2Data);
Map<String,String>child1Data=new HashMap<String,String>();
child2.add(child1Data);
//定义一个list,存储二级条目的数据
List<List<Map<String,String>>>childs=new ArrayList<List<Map<String,String>>>();
childs.add(child1);
childs.add(child2);
//生成一个SimpleExpandableListAdapter对象
//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});
//将SimpleExpandableListAdapter对象设置给当前的Activity
setListAdapter(sela);
}
//为各个ListView绑定一个监听器
@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;
}
}
<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" >
<TextView
android:id="@id/android:empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="No data" />
<ExpandableListView
android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="false"/>
</RelativeLayout>