Android ExpandableListActivity 学习笔记

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. publicclassExpandableList1extendsExpandableListActivity{
  2. ExpandableListAdaptermAdapter;
  3. @Override
  4. publicvoidonCreate(BundlesavedInstanceState){
  5. super.onCreate(savedInstanceState);
  6. //Setupouradapter
  7. mAdapter=newMyExpandableListAdapter();
  8. setListAdapter(mAdapter);
  9. //registercontextmenu,whenlongclicktheitem,itwillshowadialog
  10. registerForContextMenu(getExpandableListView());
  11. }
  12. @Override
  13. publicvoidonCreateContextMenu(ContextMenumenu,Viewv,ContextMenuInfomenuInfo){
  14. menu.setHeaderTitle("Samplemenu");
  15. menu.add(0,0,0,R.string.expandable_list_sample_action);
  16. }
  17. @Override
  18. publicbooleanonContextItemSelected(MenuItemitem){
  19. ExpandableListContextMenuInfoinfo=(ExpandableListContextMenuInfo)item.getMenuInfo();
  20. Stringtitle=((TextView)info.targetView).getText().toString();
  21. inttype=ExpandableListView.getPackedPositionType(info.packedPosition);
  22. if(type==ExpandableListView.PACKED_POSITION_TYPE_CHILD){
  23. intgroupPos=ExpandableListView.getPackedPositionGroup(info.packedPosition);
  24. intchildPos=ExpandableListView.getPackedPositionChild(info.packedPosition);
  25. Toast.makeText(this,title+":Child"+childPos+"clickedingroup"+groupPos,
  26. Toast.LENGTH_SHORT).show();
  27. returntrue;
  28. }elseif(type==ExpandableListView.PACKED_POSITION_TYPE_GROUP){
  29. intgroupPos=ExpandableListView.getPackedPositionGroup(info.packedPosition);
  30. Toast.makeText(this,title+":Group"+groupPos+"clicked",Toast.LENGTH_SHORT).show();
  31. returntrue;
  32. }
  33. returnfalse;
  34. }
  35. /**
  36. *AsimpleadapterwhichmaintainsanArrayListofphotoresourceIds.
  37. *Eachphotoisdisplayedasanimage.Thisadaptersupportsclearingthe
  38. *listofphotosandaddinganewphoto.
  39. *
  40. */
  41. publicclassMyExpandableListAdapterextendsBaseExpandableListAdapter{
  42. //Sampledataset.children[i]containsthechildren(String[])forgroups[i].
  43. privateString[]groups={"PeopleNames","DogNames","CatNames","FishNames"};
  44. privateString[][]children={
  45. {"Arnold","Barry","Chuck","David"},
  46. {"Ace","Bandit","Cha-Cha","Deuce"},
  47. {"Fluffy","Snuggles"},
  48. {"Goldy","Bubbles"}
  49. };
  50. publicObjectgetChild(intgroupPosition,intchildPosition){
  51. returnchildren[groupPosition][childPosition];
  52. }
  53. publiclonggetChildId(intgroupPosition,intchildPosition){
  54. returnchildPosition;
  55. }
  56. publicintgetChildrenCount(intgroupPosition){
  57. returnchildren[groupPosition].length;
  58. }
  59. publicTextViewgetGenericView(){
  60. //LayoutparametersfortheExpandableListView
  61. AbsListView.LayoutParamslp=newAbsListView.LayoutParams(
  62. ViewGroup.LayoutParams.FILL_PARENT,64);
  63. TextViewtextView=newTextView(ExpandableList1.this);
  64. textView.setLayoutParams(lp);
  65. //Centerthetextvertically
  66. textView.setGravity(Gravity.CENTER_VERTICAL|Gravity.LEFT);
  67. //Setthetextstartingposition
  68. textView.setPadding(36,0,0,0);
  69. returntextView;
  70. }
  71. publicViewgetChildView(intgroupPosition,intchildPosition,booleanisLastChild,
  72. ViewconvertView,ViewGroupparent){
  73. TextViewtextView=getGenericView();
  74. textView.setText(getChild(groupPosition,childPosition).toString());
  75. returntextView;
  76. }
  77. publicObjectgetGroup(intgroupPosition){
  78. returngroups[groupPosition];
  79. }
  80. publicintgetGroupCount(){
  81. returngroups.length;
  82. }
  83. publiclonggetGroupId(intgroupPosition){
  84. returngroupPosition;
  85. }
  86. publicViewgetGroupView(intgroupPosition,booleanisExpanded,ViewconvertView,
  87. ViewGroupparent){
  88. TextViewtextView=getGenericView();
  89. textView.setText(getGroup(groupPosition).toString());
  90. returntextView;
  91. }
  92. publicbooleanisChildSelectable(intgroupPosition,intchildPosition){
  93. returntrue;
  94. }
  95. publicbooleanhasStableIds(){
  96. returntrue;
  97. }
  98. }
  99. }
  1. publicclassExpandableList2extendsExpandableListActivity{
  2. privateintmGroupIdColumnIndex;
  3. privateStringmPhoneNumberProjection[]=newString[]{
  4. People.Phones._ID,People.Phones.NUMBER
  5. };
  6. privateExpandableListAdaptermAdapter;
  7. /*
  8. *CursorTreeAdapter'smethod.
  9. *GetstheCursorforthechildrenatthegivengroup
  10. */
  11. @Override
  12. publicvoidonCreate(BundlesavedInstanceState){
  13. super.onCreate(savedInstanceState);
  14. //Queryforpeople
  15. CursorgroupCursor=managedQuery(People.CONTENT_URI,
  16. newString[]{People._ID,People.NAME},null,null,null);
  17. //CachetheIDcolumnindex
  18. mGroupIdColumnIndex=groupCursor.getColumnIndexOrThrow(People._ID);
  19. //Setupouradapter
  20. mAdapter=newMyExpandableListAdapter(groupCursor,
  21. this,
  22. android.R.layout.simple_expandable_list_item_1,
  23. android.R.layout.simple_expandable_list_item_1,
  24. newString[]{People.NAME},//Nameforgrouplayouts
  25. newint[]{android.R.id.text1},
  26. newString[]{People.NUMBER},//Numberforchildlayouts
  27. newint[]{android.R.id.text1});
  28. setListAdapter(mAdapter);
  29. }
  30. publicclassMyExpandableListAdapterextendsSimpleCursorTreeAdapter{
  31. publicMyExpandableListAdapter(Cursorcursor,Contextcontext,intgroupLayout,
  32. intchildLayout,String[]groupFrom,int[]groupTo,String[]childrenFrom,
  33. int[]childrenTo){
  34. super(context,cursor,groupLayout,groupFrom,groupTo,childLayout,childrenFrom,
  35. childrenTo);
  36. }
  37. @Override
  38. protectedCursorgetChildrenCursor(CursorgroupCursor){
  39. //Giventhegroup,wereturnacursorforallthechildrenwithinthatgroup
  40. //Returnacursorthatpointstothiscontact'sphonenumbers
  41. Uri.Builderbuilder=People.CONTENT_URI.buildUpon();
  42. ContentUris.appendId(builder,groupCursor.getLong(mGroupIdColumnIndex));
  43. builder.appendEncodedPath(People.Phones.CONTENT_DIRECTORY);
  44. UriphoneNumbersUri=builder.build();
  45. //ThereturnedCursorMUSTbemanagedbyus,soweuseActivity'shelper
  46. //functionalitytomanageitforus.
  47. returnmanagedQuery(phoneNumbersUri,mPhoneNumberProjection,null,null,null);
  48. }
  49. }
  50. }
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="wrap_content"
  5. android:orientation="vertical">
  6. <TextViewandroid:id="@+id/text1"
  7. android:textSize="16sp"
  8. android:textStyle="bold"
  9. android:layout_width="fill_parent"
  10. android:layout_height="wrap_content"/>
  11. <TextViewandroid:id="@+id/text2"
  12. android:textSize="16sp"
  13. android:layout_width="fill_parent"
  14. android:layout_height="wrap_content"/>
  15. </LinearLayout>
  1. publicclassExpandableList3extendsExpandableListActivity{
  2. privatestaticfinalStringNAME="NAME";
  3. privatestaticfinalStringIS_EVEN="IS_EVEN";
  4. privateExpandableListAdaptermAdapter;
  5. @Override
  6. publicvoidonCreate(BundlesavedInstanceState){
  7. super.onCreate(savedInstanceState);
  8. List<Map<String,String>>groupData=newArrayList<Map<String,String>>();
  9. List<List<Map<String,String>>>childData=newArrayList<List<Map<String,String>>>();
  10. for(inti=0;i<20;i++){
  11. Map<String,String>curGroupMap=newHashMap<String,String>();
  12. groupData.add(curGroupMap);
  13. curGroupMap.put(NAME,"Group"+i);
  14. curGroupMap.put(IS_EVEN,(i%2==0)?"Thisgroupiseven":"Thisgroupisodd");
  15. List<Map<String,String>>children=newArrayList<Map<String,String>>();
  16. for(intj=0;j<15;j++){
  17. Map<String,String>curChildMap=newHashMap<String,String>();
  18. children.add(curChildMap);
  19. curChildMap.put(NAME,"Child"+j);
  20. curChildMap.put(IS_EVEN,(j%2==0)?"Thischildiseven":"Thischildisodd");
  21. }
  22. childData.add(children);
  23. }
  24. //Setupouradapter
  25. mAdapter=newSimpleExpandableListAdapter(
  26. this,
  27. groupData,//存储父list的数据
  28. android.R.layout.simple_expandable_list_item_2,//父list的现实方式
  29. newString[]{NAME,IS_EVEN},//父list需要显示的数据
  30. newint[]{android.R.id.text1,android.R.id.text2},//父list的数据绑定到的view
  31. childData,//子list的数据
  32. android.R.layout.simple_expandable_list_item_2,
  33. newString[]{NAME,IS_EVEN},
  34. newint[]{android.R.id.text1,android.R.id.text2}
  35. );
  36. setListAdapter(mAdapter);
  37. }
  38. }

你可能感兴趣的:(ListActivity)