参数
groupPosition 包含要取得子视图的分组位置.
childPosition 分组中子视图的位置.
返回
是否子视图可选择.
注意:
在XML布局文件中,如果ExpandableListView上一级视图的大小没有严格定义的话,则不能对ExpandableListView的Android:layout_height 属性使用wrap_content值。 (例如,如果上一级视图是ScrollView的话,则不应该指定wrap_content的值,因为它可以是任意的长度。不过,如果ExpandableListView的上一级视图有特定的大小的话,比如100像素,则可以使用wrap_content)
如果由于开发的时候粗心,对ExpandableListView指定wrap_content的值,则会报一个在SetContentView处的空指针错误。
根据描述,先看一个简单的例子:
定义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" >
- <ExpandableListView
- android:id = "@+id/myExpandableListView"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layout_weight="1"
- />
- </LinearLayout>
就是在LinearLayout中布置了一个ExpandableListView。
在JAVA中的代码如下:
- package com.blueeagle.www;
- import java.util.ArrayList;
- import java.util.List;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.Gravity;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.AbsListView;
- import android.widget.BaseExpandableListAdapter;
- import android.widget.ExpandableListView;
- import android.widget.TextView;
- public class ExpandableListViewActivity extends Activity {
- private List<String> GroupData;//定义组数据
- private List<List<String>> ChildrenData;//定义组中的子数据
- private void LoadListDate() {
- GroupData = new ArrayList<String>();
- GroupData.add("国家");
- GroupData.add("人物");
- GroupData.add("武器");
- ChildrenData = new ArrayList<List<String>>();
- List<String> Child1 = new ArrayList<String>();
- Child1.add("蜀国");
- Child1.add("魏国");
- Child1.add("吴国");
- ChildrenData.add(Child1);
- List<String> Child2 = new ArrayList<String>();
- Child2.add("关羽");
- Child2.add("张飞");
- Child2.add("典韦");
- Child2.add("吕布");
- Child2.add("曹操");
- Child2.add("甘宁");
- Child2.add("郭嘉");
- Child2.add("周瑜");
- ChildrenData.add(Child2);
- List<String> Child3 = new ArrayList<String>();
- Child3.add("青龙偃月刀");
- Child3.add("丈八蛇矛枪");
- Child3.add("青钢剑");
- Child3.add("麒麟弓");
- Child3.add("银月枪");
- ChildrenData.add(Child3);
- }
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- LoadListDate();
- ExpandableListView myExpandableListView = (ExpandableListView)findViewById(R.id.myExpandableListView);
- myExpandableListView.setAdapter(new ExpandableAdapter());
- }
- private class ExpandableAdapter extends BaseExpandableListAdapter {
- @Override
- public Object getChild(int groupPosition, int childPosition) {
- return ChildrenData.get(groupPosition).get(childPosition);
- }
- @Override
- public long getChildId(int groupPosition, int childPosition) {
- return 0;
- }
- @Override
- public View getChildView(int groupPosition, int childPosition,
- boolean isLastChild, View convertView, ViewGroup parent) {
- TextView myText = null;
- if (convertView != null) {
- myText = (TextView)convertView;
- myText.setText(ChildrenData.get(groupPosition).get(childPosition));
- } else {
- myText = createView(ChildrenData.get(groupPosition).get(childPosition));
- }
- return myText;
- }
- @Override
- public int getChildrenCount(int groupPosition) {
- return ChildrenData.get(groupPosition).size();
- }
- @Override
- public Object getGroup(int groupPosition) {
- return GroupData.get(groupPosition);
- }
- @Override
- public int getGroupCount() {
- return GroupData.size();
- }
- @Override
- public long getGroupId(int groupPosition) {
- return 0;
- }
- @Override
- public View getGroupView(int groupPosition, boolean isExpanded,
- View convertView, ViewGroup parent) {
- TextView myText = null;
- if (convertView != null) {
- myText = (TextView)convertView;
- myText.setText(GroupData.get(groupPosition));
- } else {
- myText = createView(GroupData.get(groupPosition));
- }
- return myText;
- }
- @Override
- public boolean hasStableIds() {
- return false;
- }
- @Override
- public boolean isChildSelectable(int groupPosition, int childPosition) {
- return false;
- }
- private TextView createView(String content) {
- AbsListView.LayoutParams layoutParams = new AbsListView.LayoutParams(
- ViewGroup.LayoutParams.FILL_PARENT, 80);
- TextView myText = new TextView(ExpandableListViewActivity.this);
- myText.setLayoutParams(layoutParams);
- myText.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
- myText.setPadding(80, 0, 0, 0);
- myText.setText(content);
- return myText;
- }
- }
- }
效果如下图所示:

这样就完成了一个最简单的ExpandableListView的控件。
在实际开发过程中,常常有不同的需求,比如每一个child需要不同的控件,每一个group或者child需要有图标,图标显示需要不一样,需要设置背景等各种各样能够让我们的程序变得美观的需求。那么下面就逐一讨论一下ExpandableListView如何实现这些需求。
比如,为ExpandableListView设置背景,并且默认展开第n组,n从0开始计数,则只需要添加如下代码:
- myExpandableListView.setBackgroundResource(R.drawable.background);
- myExpandableListView.expandGroup(0);

