现在的位置: 首页 > 综合 > 正文

Android ExpandableListActivity 学习笔记

2013年10月10日 ⁄ 综合 ⁄ 共 3654字 ⁄ 字号 评论关闭

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绑定数据:

 

  1. public class ExpandableList1 extends ExpandableListActivity {  
  2.   
  3.     ExpandableListAdapter mAdapter;  
  4.   
  5.     @Override  
  6.     public void onCreate(Bundle savedInstanceState) {  
  7.         super.onCreate(savedInstanceState);  
  8.   
  9.         // Set up our adapter  
  10.         mAdapter = new MyExpandableListAdapter();  
  11.         setListAdapter(mAdapter);  
  12.         // register context menu, when long click the item, it will show a dialog  
  13.         registerForContextMenu(getExpandableListView());  
  14.     }  
  15.   
  16.     @Override  
  17.     public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {  
  18.         menu.setHeaderTitle("Sample menu");  
  19.         menu.add(000, R.string.expandable_list_sample_action);  
  20.     }  
  21.       
  22.     @Override  
  23.     public boolean onContextItemSelected(MenuItem item) {  
  24.         ExpandableListContextMenuInfo info = (ExpandableListContextMenuInfo) item.getMenuInfo();  
  25.   
  26.         String title = ((TextView) info.targetView).getText().toString();  
  27.           
  28.         int type = ExpandableListView.getPackedPositionType(info.packedPosition);  
  29.         if (type == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {  
  30.             int groupPos = ExpandableListView.getPackedPositionGroup(info.packedPosition);   
  31.             int childPos = ExpandableListView.getPackedPositionChild(info.packedPosition);   
  32.             Toast.makeText(this, title + ": Child " + childPos + " clicked in group " + groupPos,  
  33.                     Toast.LENGTH_SHORT).show();  
  34.             return true;  
  35.         } else if (type == ExpandableListView.PACKED_POSITION_TYPE_GROUP) {  
  36.             int groupPos = ExpandableListView.getPackedPositionGroup(info.packedPosition);   
  37.             Toast.makeText(this, title + ": Group " + groupPos + " clicked", Toast.LENGTH_SHORT).show();  
  38.             return true;  
  39.         }  
  40.           
  41.         return false;  
  42.     }  
  43.   
  44.     /** 
  45.      * A simple adapter which maintains an ArrayList of photo resource Ids.  
  46.      * Each photo is displayed as an image. This adapter supports clearing the 
  47.      * list of photos and adding a new photo. 
  48.      * 
  49.      */  
  50.     public class MyExpandableListAdapter extends BaseExpandableListAdapter {  
  51.         // Sample data set.  children[i] contains the children (String[]) for groups[i].  
  52.         private String[] groups = { "People Names""Dog Names""Cat Names""Fish Names" };  
  53.         private String[][] children = {  
  54.                 { "Arnold""Barry""Chuck""David" },  
  55.                 { "Ace""Bandit""Cha-Cha""Deuce" },  
  56.                 { "Fluffy""Snuggles" },  
  57.                 { "Goldy""Bubbles" }  
  58.         };  
  59.           
  60.         public Object getChild(int groupPosition, int childPosition) {  
  61.             return children[groupPosition][childPosition];  
  62.         }  
  63.   
  64.         public long getChildId(int groupPosition, int childPosition) {  
  65.             return childPosition;  
  66.         }  
  67.   
  68.         public int getChildrenCount(int groupPosition) {  
  69.             return children[groupPosition].length;  
  70.         }  

抱歉!评论已关闭.