ExpandableListActivity:
An activity that displays an expandable list of items by binding to a data source implementing the ExpandableListAdapter, and exposes event handlers when the user selects an item.
即,可扩展的list,单击某个item后,又可显示一个子list。它的数据通过绑定到ExpandableListAdapter或者ExpandableListAdapter的子类上。
示例1—通过SimpelExpandableListAdapter绑定数据:
注意:android.R.layout.simple_expandable_list_item_1:表示只显示一个TextView的数据,即R.id.text1
android.R.layout.simple_expandable_list_item_2:表示显示二个TextView的数据,即R.id.text1,R.id,text2
android.R.layout.simple_expandable_list_item_2.xml(在R.layout中)文件的布局如下:
示例2—通过SimpleCussorTreeAdapter绑定数据:
示例3—通过BaseExpandableListAdapter绑定数据:
- public class ExpandableList1 extends ExpandableListActivity {
- ExpandableListAdapter mAdapter;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- // Set up our adapter
- mAdapter = new MyExpandableListAdapter();
- setListAdapter(mAdapter);
- // register context menu, when long click the item, it will show a dialog
- registerForContextMenu(getExpandableListView());
- }
- @Override
- public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
- menu.setHeaderTitle("Sample menu");
- menu.add(0, 0, 0, R.string.expandable_list_sample_action);
- }
- @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 + ": Child " + childPos + " clicked in group " + groupPos,
- Toast.LENGTH_SHORT).show();
- return true;
- } else if (type == ExpandableListView.PACKED_POSITION_TYPE_GROUP) {
- int groupPos = ExpandableListView.getPackedPositionGroup(info.packedPosition);
- Toast.makeText(this, title + ": Group " + groupPos + " clicked", Toast.LENGTH_SHORT).show();
- return true;
- }
- return false;
- }
- /**
- * A simple adapter which maintains an ArrayList of photo resource Ids.
- * Each photo is displayed as an image. This adapter supports clearing the
- * list of photos and adding a new photo.
- *
- */
- public class MyExpandableListAdapter extends BaseExpandableListAdapter {
- // Sample data set. children[i] contains the children (String[]) for groups[i].
- private String[] groups = { "People Names", "Dog Names", "Cat Names", "Fish Names" };
- private String[][] children = {
- { "Arnold", "Barry", "Chuck", "David" },
- { "Ace", "Bandit", "Cha-Cha", "Deuce" },
- { "Fluffy", "Snuggles" },
- { "Goldy", "Bubbles" }
- };
- public Object getChild(int groupPosition, int childPosition) {
- return children[groupPosition][childPosition];
- }
- public long getChildId(int groupPosition, int childPosition) {
- return childPosition;
- }
- public int getChildrenCount(int groupPosition) {
- return children[groupPosition].length;
- }