您的位置:首页 > 移动开发 > Android开发

Android基础教程之---ExpandableListView基本用法

2012-04-02 14:55 405 查看
什么是ExpandableListView,如下图所示:



展开后的效果:



创建ExpandableListView的步骤

1.首先在main.xml布局文件中声明ExpandableListView控件:

<?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="fill_parent"
android:drawSelectorOnTop="false"/>
<TextView
android:id="@id/android:empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="No Data" />

</LinearLayout>


2.然后在布局文件夹中创建group.xml文件声明group的样式:

<?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>


3.在布局文件夹中创建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>


4.创建一个Activity,继承ExpandableListView:

public class MainActivity extends ExpandableListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}


5.为group创建数据,也就是一级菜单项。一级菜单有两个菜单项,分别为group1,group2

//定义一个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);


为child创建数据,也就是二级菜单项。这个二级菜单项属于一级菜单项group1

//定义一个List,该List对象为第一个一级菜单项提供二级菜单项数据
List<Map<String, String>> child1 = new ArrayList<Map<String, String>>();
Map<String, String> child1Data1 = new HashMap<String, String>();
child1Data1.put("child", "child1Data1");
child1.add(child1Data1);
Map<String, String> child1Data2 = new HashMap<String, String>();
child1Data2.put("child", "child1Data2");
child1.add(child1Data2);


创建属于group2的二级菜单项

//定义一个List,该List对象为第二个一级菜单项提供二级菜单项数据
List<Map<String, String>> child2 = new ArrayList<Map<String, String>>();
Map<String, String> child2Data1 = new HashMap<String, String>();
child2Data1.put("child", "child2Data1");
child2.add(child2Data1);
Map<String, String> child2Data2 = new HashMap<String, String>();
child2Data2.put("child", "child2Data2");
child2.add(child2Data2);


再定义一个list,用来存储所有二级菜单项的数据

List<List<Map<String, String>>> childs = new ArrayList<List<Map<String,String>>>();
childs.add(child1);
childs.add(child2);


6.生成一个SimpleExpandableListAdapter对象

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});


参数注释:

this, 上下文对象

groups, 一级条目的数据

R.layout.group, 用来设置一级条目样式的布局文件

new String[] {"group"}, 指定一级条目数据的key

new int[] {R.id.groupTo}, 指定一级条目数据显示控件的id

childs, 指定二级条目的数据

R.layout.child, 用来设置二级条目样式的布局文件

new String[] {"child"}, 指定二级条目数据的key

new int[] {R.id.childTo} 指定二级条目数据显示控件的id

程序结构图:



源码下载地址

摘自:mars。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: