效果图:
代码实现
package com.zhou.activity; import android.app.ExpandableListActivity; import android.os.Bundle; import android.view.ContextMenu; import android.view.Gravity; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.view.ContextMenu.ContextMenuInfo; import android.widget.AbsListView; import android.widget.BaseExpandableListAdapter; import android.widget.ExpandableListAdapter; import android.widget.ExpandableListView; import android.widget.TextView; import android.widget.Toast; import android.widget.ExpandableListView.ExpandableListContextMenuInfo; public class ExpandableList extends ExpandableListActivity { //声明adapter private ExpandableListAdapter mAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //设这标题 setTitle("可扩展的列表"); //实例化adapter mAdapter = new MyExpandableListAdapter(); //为列表设置adapter setListAdapter(mAdapter); //为list注册context菜单 registerForContextMenu(this.getExpandableListView()); } @Override public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { Toast.makeText(this, " 组元素索引: " + groupPosition + " 子元素索引: " + childPosition, Toast.LENGTH_SHORT).show(); return super.onChildClick(parent, v, groupPosition, childPosition, id); } @Override public void onGroupExpand(int groupPosition) { Toast.makeText(this, " 组元素索引: " + groupPosition, Toast.LENGTH_SHORT).show(); super.onGroupExpand(groupPosition); } @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { menu.setHeaderTitle("上下文菜单"); menu.add(0, 0, 0, "单击我"); } // 单击上下文菜单后的逻辑 @Override public boolean onContextItemSelected(MenuItem item) { ExpandableListContextMenuInfo info = (ExpandableListContextMenuInfo) item.getMenuInfo(); String title = ((TextView) info.targetView).getText().toString(); int type = ExpandableListView.getPackedPositionType(info.packedPosition); if (type == ExpandableListView.PACKED_POSITION_TYPE_CHILD) { int groupPos = ExpandableListView.getPackedPositionGroup(info.packedPosition); int childPos = ExpandableListView.getPackedPositionChild(info.packedPosition); Toast.makeText(this, title + " 组元素索引: " + groupPos + " 子元素索引: " + childPos, Toast.LENGTH_SHORT).show(); return true; } else if (type == ExpandableListView.PACKED_POSITION_TYPE_GROUP) { int groupPos = ExpandableListView.getPackedPositionGroup(info.packedPosition); Toast.makeText(this, title + " 组元素索引: " + groupPos, Toast.LENGTH_SHORT).show(); return true; } return false; } //自定义Adapter public class MyExpandableListAdapter extends BaseExpandableListAdapter { // 父列表数据 private String[] groups = { "吉林省", "辽宁省", "黑龙江省", "山东省" }; // 子列表数据 private String[][] children = { { "长春市" }, { "沈阳市", "铁岭市" }, { "哈尔滨市", "齐齐哈尔市", "牡丹江市" }, { "济南市", "青岛市", "淄博市", "潍坊市" } }; @Override public Object getChild(int groupPosition, int childPosition) { return children[groupPosition][childPosition]; } @Override public long getChildId(int groupPosition, int childPosition) { return childPosition; } @Override public int getChildrenCount(int groupPosition) { return children[groupPosition].length; } // 取子列表中的某一项的 View @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { TextView textView = getGenericView(); textView.setText(getChild(groupPosition, childPosition).toString()); return textView; } @Override public Object getGroup(int groupPosition) { return groups[groupPosition]; } @Override public int getGroupCount() { return groups.length; } @Override public long getGroupId(int groupPosition) { return groupPosition; } // 取父列表中的某一项的 View @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { TextView textView = getGenericView(); textView.setText(getGroup(groupPosition).toString()); return textView; } @Override public boolean hasStableIds() { return true; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } // 获取某一项的 View 的逻辑 private TextView getGenericView() { AbsListView.LayoutParams lp = new AbsListView.LayoutParams( ViewGroup.LayoutParams.FILL_PARENT, 48); TextView textView = new TextView(ExpandableList.this); textView.setLayoutParams(lp); textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT); textView.setPadding(32, 0, 0, 0); return textView; } } }