首先我们要自定义一个adapter来继承BaseExpandableListAdapter;
package com.example.tree; import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.widget.AbsoluteLayout; import android.widget.AbsoluteLayout.LayoutParams; import android.widget.AbsListView; import android.widget.BaseExpandableListAdapter; import android.widget.TextView; public class TreeAdapter extends BaseExpandableListAdapter { private String[] groups; private String[][] chlids; private Context context; public TreeAdapter(String[] groups, String[][] chlids, Context context) { this.groups = groups; this.chlids = chlids; this.context = context; } //父项长度 @Override public int getGroupCount() { // TODO Auto-generated method stub return groups.length; } //子项长度 @Override public int getChildrenCount(int groupPosition) { // TODO Auto-generated method stub return chlids[groupPosition].length; } //返回父项对象 @Override public Object getGroup(int groupPosition) { // TODO Auto-generated method stub return groups[groupPosition]; } //返回子项对象 @Override public Object getChild(int groupPosition, int childPosition) { // TODO Auto-generated method stub return chlids[groupPosition][childPosition]; } //返回父项id @Override public long getGroupId(int groupPosition) { // TODO Auto-generated method stub return groupPosition; } //返回子项id @Override public long getChildId(int groupPosition, int childPosition) { // TODO Auto-generated method stub return childPosition; } @Override public boolean hasStableIds() { // TODO Auto-generated method stub return true; } //创建textview方法 private TextView buildTextView() { AbsListView.LayoutParams p = new AbsListView.LayoutParams( ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); TextView tv = new TextView(context); tv.setLayoutParams(p); tv.setTextSize(20); tv.setPadding(60, 5, 10, 5); return tv; } //返回父项视图 @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { TextView tv = buildTextView(); tv.setText(groups[groupPosition].toString()); return tv; } //返回子项视图 @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { TextView tv = buildTextView(); tv.setText(chlids[groupPosition][childPosition].toString()); return tv; } //设置子项是否可以点击 @Override public boolean isChildSelectable(int groupPosition, int childPosition) { // TODO Auto-generated method stub return true; } }
布局文件
<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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <ExpandableListView android:id="@+id/ex" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RelativeLayout>
activity
package com.example.tree; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.widget.ExpandableListView; import android.widget.ExpandableListView.OnGroupClickListener; import android.widget.ExpandableListView.OnGroupCollapseListener; import android.widget.ExpandableListView.OnGroupExpandListener; import android.widget.Toast; import android.widget.ExpandableListView.OnChildClickListener; public class MainActivity extends Activity { private ExpandableListView ex; private TreeAdapter adapter; private String[] groups = { "吉林", "辽宁", "黑龙江", "湖南" }; private String[][] childs = { { "长春", "四平", "通话" }, { "大连", "沈阳" }, { "绥芬河", "哈尔冰" }, { "长沙" } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ex = (ExpandableListView) findViewById(R.id.ex); adapter = new TreeAdapter(groups, childs, this); //注册上下文菜单 //super.registerForContextMenu(ex); ex.setAdapter(adapter); // 子项的点击事件 ex.setOnChildClickListener(new OnChildClickListener() { @Override public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { Toast.makeText(MainActivity.this, childs[groupPosition][childPosition], 1000).show(); return false; } }); //父项点击事件 ex.setOnGroupClickListener(new OnGroupClickListener() { @Override public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, groups[groupPosition], 1000).show(); return false; } }); //父项关闭事件 ex.setOnGroupCollapseListener(new OnGroupCollapseListener() { @Override public void onGroupCollapse(int groupPosition) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, "关闭", 1000).show(); } }); //父项打开事件 ex.setOnGroupExpandListener(new OnGroupExpandListener() { @Override public void onGroupExpand(int groupPosition) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, "打开", 1000).show(); } }); } }