定义一个适配器的操作类MyExpandableAdapter.java
package com.li.expandablelistview;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
//这是一个专门用于数据填充的组件
public class MyExpandableAdapter extends BaseExpandableListAdapter {
//一个组下有多个字选项
private String[] groups = new String[]{"我的好友","亲人","我认识的人","黑名单"};
private String[][] children = new String[][]{
{"李叶文","张三","李四","王五"},
{"老弟","堂哥","堂姐"},
{"杨志","鲁智深","林冲","吴用"},
{"恶意网站","造价商","低俗人员"} }; //定义子菜单选项
private Context context = null;
public MyExpandableAdapter(Context context){
this.context = context;
}
public Object getChild(int groupPosition, int childPosition) { //取得子选项
return this.children[groupPosition][childPosition];
}
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
private TextView buildTextView(){
AbsListView.LayoutParams params = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,60);
TextView textView = new TextView(this.context);
textView.setLayoutParams(params);
textView.setTextSize(20.0f);
textView.setGravity(Gravity.LEFT); //设置对齐方式
textView.setPadding(80, 8, 3, 3); //设置上下左右间距
//textView.setPadding(left, top, right, bottom)
return textView;
}
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
TextView textView = this.buildTextView();
textView.setText(this.getChild(groupPosition, childPosition).toString());
return textView;
}
public int getChildrenCount(int groupPosition) {
return this.children[groupPosition].length;
}
public Object getGroup(int groupPosition) {
return this.groups[groupPosition];
}
public int getGroupCount() {
return this.groups.length;
}
public long getGroupId(int groupPosition) {
return groupPosition;
}
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
TextView textView = this.buildTextView();
textView.setText(this.getGroup(groupPosition).toString());
return textView;
}
public boolean hasStableIds() {
return true;
}
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
在main.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/elistview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
在MyExpandableListViewDemo.java程序中
package com.li.expandablelistview;
import android.app.Activity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.ExpandableListContextMenuInfo;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.Toast;
public class MyExpandableListViewDemo extends Activity {
private ExpandableListView elistview = null;
private ExpandableListAdapter adapter = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main);
this.elistview = (ExpandableListView)super.findViewById(R.id.elistview);
this.adapter = new MyExpandableAdapter(this);
this.elistview.setAdapter(this.adapter);
super.registerForContextMenu(this.elistview);
this.elistview.setOnChildClickListener(new OnChildClickListenerImpl());
this.elistview.setOnGroupClickListener(new OnGroupClickListenerImpl());
this.elistview.setOnGroupExpandListener(new OnGroupExpandListenerImpl());
this.elistview.setOnGroupCollapseListener(new OnGroupCollapseListenerImpl());
}
private class OnChildClickListenerImpl implements OnChildClickListener{
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition,long id) {
Toast.makeText(
MyExpandableListViewDemo.this, "子选项被选中,groupPosition=" +
groupPosition + "childPosition=" + childPosition,
Toast.LENGTH_LONG).show();
return false;
}
}
private class OnGroupClickListenerImpl implements OnGroupClickListener{
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
Toast.makeText(MyExpandableListViewDemo.this,
"分组被选中,groupPosition=" + groupPosition,
Toast.LENGTH_LONG).show();
return false;
}
}
private class OnGroupExpandListenerImpl implements OnGroupExpandListener{
public void onGroupExpand(int groupPosition) {
Toast.makeText(MyExpandableListViewDemo.this,
"打开分组,groupPosition=" + groupPosition,
Toast.LENGTH_LONG).show();
}
}
private class OnGroupCollapseListenerImpl implements OnGroupCollapseListener{
public void onGroupCollapse(int groupPosition) {
Toast.makeText(MyExpandableListViewDemo.this,
"关闭分组,groupPosition=" + groupPosition,
Toast.LENGTH_LONG).show();
}
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
ExpandableListView.ExpandableListContextMenuInfo info = (
ExpandableListView.ExpandableListContextMenuInfo) menuInfo;
int type = ExpandableListView
.getPackedPositionType(info.packedPosition); //取得菜单项
int group = ExpandableListView.getPackedPositionGroup(info.packedPosition); //取得所在组的索引
int child = ExpandableListView.getPackedPositionChild(info.packedPosition); //取得子菜单的索引
Toast.makeText(MyExpandableListViewDemo.this,
"type=" + type + ",group=" + group + ",child" + child, Toast.LENGTH_LONG).show();
}
}