ExpanableList 折叠列表
主要示例如何实现一个ExpanableList
涵括的知识点:
1.MyExpandableListAdapter适配器 方法说明
2. onChildClick 与 onGroupExpand
3. 使用上下文菜单
下面见Java代码,这里没有用到布局文件,所以没有使用setContentView()
public class mExpandable extends ExpandableListActivity { private ExpandableListAdapter mAdapter; private final static String TAG = "mExpandable ====>"; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //setContentView(R.layout.activity_m_expandable); //设置标题 setTitle("可扩展的列表"); // 实例化Adapter mAdapter = new MyExpandableListAdapter(); //为列表设置adapter setListAdapter(mAdapter); //为list注册context菜单 registerForContextMenu(this.getExpandableListView()); } // ChildClick 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); } // GroupClick public void onGroupExpand(int groupPosition) { Toast.makeText(this, " 组元素索引: " + groupPosition, Toast.LENGTH_SHORT).show(); super.onGroupExpand(groupPosition); } // 上下文菜单 public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) { menu.setHeaderTitle("上下文菜单"); menu.add(0, 0, 0, "通过按下轨迹球"); menu.add(0, 1, 0, "D-pad的中键"); menu.add(0, 2, 0, "长按View3秒"); } public class MyExpandableListAdapter extends BaseExpandableListAdapter { // 父列表数据 private String[] groups = { "浙江省", "江苏省", "黑龙江省", "山东省" }; // 子列表数据 private String[][] children = { { "杭州","湖州","金华","温州" }, { "南京", "泰州","苏州","常州","南通", }, { "哈尔滨市", "齐齐哈尔市", "牡丹江市" }, { "济南市", "青岛市", "淄博市", "潍坊市" } }; /** 1.点击 Group时, 会依次调用getChild()方法显示 @return children里对应的数据,比如这里的 [市名] */ public Object getChild(int groupPosition, int childPosition) { Log.v(TAG, "getChild : "+children[groupPosition][childPosition]); return children[groupPosition][childPosition]; } /** 1.点击 具体的某个Child时 回调该方法 @return 该Child在Group内的位置 ,比如 Group内第一个,返回为0 */ public long getChildId(int groupPosition, int childPosition) { Log.v(TAG, "getChildId : "+childPosition); return childPosition; } /** 1.点击某个Group时, 回调该方法 @return Group下的Child数量 * */ public int getChildrenCount(int groupPosition) { Log.v(TAG, "getChildrenCount : "+children[groupPosition].length); 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; } /** 1.点击Group时 ,回调该方法 @return Group的数据 比如这里的[省名] * */ @Override public Object getGroup(int groupPosition) { Log.v(TAG, "getGroup : "+groups[groupPosition]); return groups[groupPosition]; } /** 1.点击Group时 回调该方法 2.点击Child时也会 回调该方法 @return Group的数量 */ public int getGroupCount() { Log.v(TAG, "getGroupCount : "+groups.length); return groups.length; } /** 1.点击Group时 回调该方法 2.点击Child时也会 回调该方法 @return 当前点击Group的位置 */ public long getGroupId(int groupPosition) { Log.v(TAG, "getGroupId : "+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(mExpandable.this); textView.setLayoutParams(lp); textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT); textView.setPadding(60, 0, 0, 0); return textView; } } }