二、 设置简单参数
获取ExpandableListView对象
设置参数setGroupIndicator(null);
加载adapter,注意参数的对应
设置setAdapter(mExpandableListAdapter);
添加点击事件,点击打开或者关闭分组
setOnGroupClickListener
三、创建一个类,继承SimpleExpandableListAdapter重写六个方法
重写6个方法
getGroup:返回对应位置的组的参数
getGroupCount:返回分了多少组
getGroupView:显示组
getChild:返回每组的成员
getChildCount:返回每组成员的个数
getChildView : 显示每组成员
在构造方法里面实例化LayoutInflater的对象,下面是两个实例化对象的方法
1、
inflater = LayoutInflater.from(context);
2、
inflater=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
下面是该程序的代码
java代码:
package com.xiaoke.expandablelistview;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.provider.Settings.System;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.SimpleExpandableListAdapter;
import android.widget.TextView;
public class MainActivity extends Activity {
private final String GROUP = "group";
private final String CHILD = "child";
private ArrayList> data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
data = new ArrayList>();
// 创建分组的信息
String[] strGroup = { "A", "B", "C", "D", "E" };
for (int i = 0; i < strGroup.length; i++) {
HashMap map = new HashMap();
map.put(GROUP, strGroup[i]);
ArrayList temp = new ArrayList();
for (int j = 0; j < 5; j++) {
temp.add("数据" + j);
}
map.put(CHILD, temp);
data.add(map);
}
// 获取ExpandableListView对象
ExpandableListView elv = (ExpandableListView) findViewById(R.id.expandableListView);
elv.setGroupIndicator(null);
// 加载adapter,注意参数的对应
ExpandableListAdapter mExpandableListAdapter = new MyExpand(this, null,
0, 0, null, null, null, 0, 0, null, null);
elv.setAdapter(mExpandableListAdapter);
// // 演示
//
// // 展开0组
// elv.expandGroup(0);
// // 收起1组
// elv.collapseGroup(1);
// // 展开2组
// elv.expandGroup(2);
// 添加点击事件,点击打开或者关闭分组
elv.setOnGroupClickListener(new OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
// TODO Auto-generated method stub
return false;
}
});
}
// /*
// * 重写6个方法
// * getGroup:返回对应位置的组的参数
// * getGroupCount:返回分了多少组
// * getGroupView:显示组
// * getChild:返回每组的成员
// * getChildCount:返回每组成员的个数
// * getChildView : 显示每组成员
// */
public class MyExpand extends SimpleExpandableListAdapter {
private LayoutInflater inflater = null;
// 构造方法中实例化LayoutInflater对象
public MyExpand(Context context,
List extends Map> groupData,
int expandedGroupLayout, int collapsedGroupLayout,
String[] groupFrom, int[] groupTo,
List extends List extends Map>> childData,
int childLayout, int lastChildLayout, String[] childFrom,
int[] childTo) {
super(context, groupData, expandedGroupLayout,
collapsedGroupLayout, groupFrom, groupTo, childData,
childLayout, lastChildLayout, childFrom, childTo);
// TODO Auto-generated constructor stub
// 两个方法实例化LayoutInflater对象
inflater = LayoutInflater.from(context);
// inflater=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
ArrayList items = (ArrayList) data.get(
groupPosition).get(CHILD);
return items.get(childPosition);
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
View view = inflater.inflate(android.R.layout.simple_list_item_1,
null);
TextView text = (TextView) view.findViewById(android.R.id.text1);
text.setText(getChild(groupPosition, childPosition) + "");
return view;
}
@Override
public int getChildrenCount(int groupPosition) {
ArrayList items = (ArrayList) data.get(
groupPosition).get(CHILD);
return items.size();
}
@Override
public Object getGroup(int groupPosition) {
return data.get(groupPosition).get(GROUP);
}
@Override
public int getGroupCount() {
return data.size();
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
View view = inflater.inflate(android.R.layout.simple_list_item_1,
null);
TextView text = (TextView) view.findViewById(android.R.id.text1);
text.setText(getGroup(groupPosition) + "");
view.setBackgroundColor(Color.RED);
return view;
}
}
}
xml布局文件为:
该代码大部分根据以下博客改编而来