ListView组件可以为用户提供列表的显示功能,但是如果想对这些列表数据进行分组管理,则需要使用android.widget.ExpandableListView组件完成。
与ListView组件一样,如果想要进行数据显示的设置,也需要一个适配器类,但是此时不再继承之前的BaseAdapter,而是继承BaseExpandableListAdapter类完成,此类为抽象类,所以要实现其中的所有抽象方法。
一、创建ExpandableListView
1.定义适配器类-MyExpandableListAdapter.java
package org.yayun.demo;
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 MyExpandableListAdapter extends BaseExpandableListAdapter {
public String[] groupStrings = { "我的好友", "我的家人", "同事", "狐朋狗友" };
public String[][] childStrings = { { "瓜瓜", "太湖", "老皮", "磨叽" },
{ "大姐", "肥肥", "二姐", "爸爸" }, { "张工", "程序猿" }, { "大鹏", "二妞" } };
private Context context;
MyExpandableListAdapter(Context context) {
this.context = context;
}
public int getGroupCount() {// 自动覆写
return this.groupStrings.length;
}
public int getChildrenCount(int groupPosition) {// 自动覆写
return childStrings[groupPosition].length;
}
public Object getGroup(int groupPosition) {// 自动覆写
return groupStrings[groupPosition];
}
public Object getChild(int groupPosition, int childPosition) {// 自动覆写
return childStrings[groupPosition][childPosition];
}
public long getGroupId(int groupPosition) {// 自动覆写
return groupPosition;
}
public long getChildId(int groupPosition, int childPosition) {// 自动覆写
return childPosition;
}
public boolean hasStableIds() {// 自动覆写
return true;
}
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {// 自动覆写
TextView textView = buildTextView();
textView.setText(this.getGroup(groupPosition).toString());
return textView;
}
private TextView buildTextView() {
AbsListView.LayoutParams params = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT, 35);// 指定布局参数
TextView textView = new TextView(this.context);// 创建TextView
textView.setLayoutParams(params);// 设置布局参数
textView.setTextSize(15.0f);
textView.setGravity(Gravity.LEFT);// 左对齐
textView.setPadding(40, 8, 3, 3);// 间距
return textView;
}
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {// 自动覆写
TextView textView = buildTextView();
textView.setText(getChild(groupPosition, childPosition).toString());
return textView;
}
public boolean isChildSelectable(int groupPosition, int childPosition) {// 自动覆写
return true;
}
}
2.定义布局文件:
<?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>
3.定义MainActivity.java:
package org.yayun.demo;
import android.app.Activity;
import android.database.DataSetObserver;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.View;
import android.view.ViewGroup;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.Toast;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.ExpandableListView.OnGroupExpandListener;
public class MainActivity extends Activity {
private ExpandableListView expandableListView;
private ExpandableListAdapter adapter;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); // 生命周期方法
super.setContentView(R.layout.main); // 设置要使用的布局管理器
expandableListView=(ExpandableListView)findViewById(R.id.elistview);
adapter=new MyExpandableListAdapter(this);
expandableListView