笔记摘要:
本次主要介绍ExpandableListActivity和SimpleExpandableListAdapter的简单使用方法,其中的难点在于为SimpleExpandableListAdapter
提供数据,而较于Spinner只能提供一个下拉菜单供用户选择,ExpandableListActivity可以提供更多的目录和选项,当然实现起来就比较复杂
ExpandableListActivity
继承关系:
java.lang.Object | |||||
↳ | android.content.Context | ||||
↳ | android.content.ContextWrapper | ||||
↳ | android.view.ContextThemeWrapper | ||||
↳ | android.app.Activity | ||||
↳ | android.app.ExpandableListActivity |
所以Activity需要继承ExpandableListActivity,获取到其特定方法或对象,比如setListAdapter(SimpleExpandableListAdapter)
另外要实现一个ExpandableListActivity,一共需要3个布局文件
第一个用来控制整个列表的样式,需要一个ExpandableListActivity来显示
第二个用来控制一级条目的样式
第三个用来控制二级条目的样式
SimpleExpandableListAdapter
用于为View提供要显示的数据
这里的难点也正是是为适配器提供数据,由于需要满足SimpleExpandableListAdapter的参数要求,
所以这里使用嵌套集合来存储数据以便表示多级条目的数据,我们需要定义一下集合:
1、为所有的一级条目提供数据的集合:List<Map<String,String>>groups
2、为每一个一级条目下的二级条目提供数据的集合:List<Map<String,String>>child
3、为所有二级条目提供数据的集合:List<List<Map<String,String>>childs
效果图:
代码体现
main.xml为整个Activity布局,需要一个ExpandableListView
<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" >
<ExpandableListView
android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="false"
></ExpandableListView>
<TextView
android:id="@id/android:empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="No Data"
/>
</RelativeLayout>
groups.xml :为一级条目提供布局
<TextView android:id="@+id/groupTo"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingTop="10dp"
android:paddingLeft="60dp"
android:paddingBottom="10dp"
android:textSize="25dp"
android:text="No Data"
/>
child.xml:为二级条目提供布局
<TextView android:id="@+id/childTo"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="50dp"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:textSize="20dp"
android:text="No Data"
/>
ExpandableActivity.java代码:主要实现部分
package com.example.expandablelistview;
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;
public class ExpandableActivity extends ExpandableListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//定义一个List,该List为一级条目提供数据
List<Map<String,String>> groups = new ArrayList<Map<String,String>>();
Map<String,String> group1 = new HashMap<String, String>();
Map<String,String> group2 = new HashMap<String, String>();
group1.put("group","group1");
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>();
Map<String,String> child1Data2 = new HashMap<String, String>();
child1Data1.put("child", "child1Data1");
child1Data2.put("child", "child1Data2");
child1.add(child1Data1);
child1.add(child1Data2);
//定义一个List,该List对象为第二个一级条目提供二级条目的数据
List<Map<String,String>> child2 = new ArrayList<Map<String,String>>();
Map<String,String> child2Data1 = new HashMap<String, String>();
Map<String,String> child2Data2 = new HashMap<String, String>();
child2Data1.put("child", "child2Data1");
child2Data2.put("child", "child2Data2");
child2.add(child2Data1);
child2.add(child2Data2);
//定义一个LIst,用来存储所有的二级条目的数据
List<List<Map<String,String>>> childs = new ArrayList<List<Map<String,String>>>();
childs.add(child1);
childs.add(child2);
/* 1.context
* 2.一级条目的数据
* 3.设置一级条目的布局文件
* 4.指定一级条目数据的key
* 4.指定以及条目数据显示控件的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});
setListAdapter(sela);
}
}
以上学习来自marsAndroid的视频和论坛:http://www.mars-droid.com/bbs/forum.php