Android中ExpandableListView的使用

ExpandableListView是android中可以实现下拉list的一个控件,具体的实现方法如下:

首先:在layout的xml文件中定义一个ExpandableListView

view plain copy to clipboard print ?
  1. <LinearLayout
  2. android:id="@+id/linearLayout"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. androidrientation="vertical"
  6. >
  7. <ExpandableListView
  8. android:id="@+id/expandableListView"
  9. android:layout_width="fill_parent"
  10. android:layout_height="wrap_content"
  11. />
  12. </LinearLayout>

定义两个List,用来存放控件中Group/Child中的String

view plain copy to clipboard print ?
  1. privateList<String>groupArray;
  2. privateList<List<String>>childArray;

对这两个List进行初始化,并插入一些数据

view plain copy to clipboard print ?
  1. groupArray=newArrayList<String>();
  2. childArray=newArrayList<List<String>>();
  3. groupArray.add("第一行");
  4. groupArray.add("第二行");
  5. List<String>tempArray=newArrayList<String>();
  6. tempArray.add("第一条");
  7. tempArray.add("第二条");
  8. tempArray.add("第三条");
  9. for(intindex=0;index<groupArray.size();++index)
  10. {
  11. childArray.add(tempArray);
  12. }

定义ExpandableListView的Adapter

view plain copy to clipboard print ?
  1. //ExpandableListView的Adapter
  2. publicclassExpandableAdapterextendsBaseExpandableListAdapter
  3. {
  4. Activityactivity;
  5. publicExpandableAdapter(Activitya)
  6. {
  7. activity=a;
  8. }
  9. publicObjectgetChild(intgroupPosition,intchildPosition)
  10. {
  11. returnchildArray.get(groupPosition).get(childPosition);
  12. }
  13. publiclonggetChildId(intgroupPosition,intchildPosition)
  14. {
  15. returnchildPosition;
  16. }
  17. publicintgetChildrenCount(intgroupPosition)
  18. {
  19. returnchildArray.get(groupPosition).size();
  20. }
  21. publicViewgetChildView(intgroupPosition,intchildPosition,
  22. booleanisLastChild,ViewconvertView,ViewGroupparent)
  23. {
  24. Stringstring=childArray.get(groupPosition).get(childPosition);
  25. returngetGenericView(string);
  26. }
  27. //groupmethodstub
  28. publicObjectgetGroup(intgroupPosition)
  29. {
  30. returngroupArray.get(groupPosition);
  31. }
  32. publicintgetGroupCount()
  33. {
  34. returngroupArray.size();
  35. }
  36. publiclonggetGroupId(intgroupPosition)
  37. {
  38. returngroupPosition;
  39. }
  40. publicViewgetGroupView(intgroupPosition,booleanisExpanded,
  41. ViewconvertView,ViewGroupparent)
  42. {
  43. Stringstring=groupArray.get(groupPosition);
  44. returngetGenericView(string);
  45. }
  46. //ViewstubtocreateGroup/Children'sView
  47. publicTextViewgetGenericView(Stringstring)
  48. {
  49. //LayoutparametersfortheExpandableListView
  50. AbsListView.LayoutParamslayoutParams=newAbsListView.LayoutParams(
  51. ViewGroup.LayoutParams.FILL_PARENT,64);
  52. TextViewtext=newTextView(activity);
  53. text.setLayoutParams(layoutParams);
  54. //Centerthetextvertically
  55. text.setGravity(Gravity.CENTER_VERTICAL|Gravity.LEFT);
  56. //Setthetextstartingposition
  57. text.setPadding(36,0,0,0);
  58. text.setText(string);
  59. returntext;
  60. }
  61. publicbooleanhasStableIds()
  62. {
  63. returnfalse;
  64. }
  65. publicbooleanisChildSelectable(intgroupPosition,intchildPosition)
  66. {
  67. returntrue;
  68. }
  69. }

最后,给定义好的ExpandableListView添加上Adapter

view plain copy to clipboard print ?
  1. ExpandableListViewexpandableListView=(ExpandableListView)findViewById(R.id.expandableListView);
  2. expandableListView.setAdapter(newExpandableAdapter(Main.this));

运行即可见效果~~~

----------------------------------------------------------------------------------------------------------------

Android版手风琴(ExpandableListView)

先看效果,过瘾一番。

源码下载:http://files.cnblogs.com/salam/WidgetDemo.rar

  

  ExpandableListView是Android中的手风琴,本人感觉效果相当棒。

  一、ExpandableListView介绍

    一个垂直滚动的显示两个级别(Child,Group)列表项的视图,列表项来自ExpandableListAdapter 。组可以单独展开。

  1.重要方法

      expandGroup(int groupPos) :在分组列表视图中展开一组,

      setSelectedGroup(int groupPosition) :设置选择指定的组。

      setSelectedChild(int groupPosition, int childPosition, boolean shouldExpandGroup) :设置选择指定的子项。

      getPackedPositionGroup(long packedPosition) :返回所选择的组

      getPackedPositionForChild(int groupPosition, int childPosition) :返回所选择的子项

      getPackedPositionType(long packedPosition) :返回所选择项的类型(Child,Group)

      isGroupExpanded(int groupPosition) :判断此组是否展开

  2.代码:

ExpandableListContextMenuInfo menuInfo=(ExpandableListContextMenuInfo)item.getMenuInfo();
String title=((TextView)menuInfo.targetView).getText().toString();
int type=ExpandableListView.getPackedPositionType(menuInfo.packedPosition);

if (type==ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
intgroupPos =ExpandableListView.getPackedPositionGroup(menuInfo.packedPosition);
intchildPos =ExpandableListView.getPackedPositionChild(menuInfo.packedPosition);

二、ExpandableListAdapter

    一个接口,将基础数据链接到一个ExpandableListView。此接口的实施将提供访问Child的数据(由组分类),并实例化的Child和Group。

  1.重要方法

    getChildId(int groupPosition, int childPosition)获取与在给定组给予孩子相关的数据。

    getChildrenCount(int groupPosition) 返回在指定Group的Child数目。

  2.代码

public class MyExpandableListAdapter extends BaseExpandableListAdapter {
// Sample data set. children[i] contains the children (String[]) for groups[i].
public String[] groups = { "我的好友", "新疆同学", "亲戚", "同事" };
public String[][] children = {
{ "胡算林", "张俊峰", "王志军", "二人" },
{ "李秀婷", "蔡乔", "别高", "余音" },
{ "摊派新", "张爱明" },
{ "马超", "司道光" }
};

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;
}

public TextView getGenericView() {
// Layout parameters for the ExpandableListView
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, 64);

TextView textView = new TextView(ExpandableListDemo.this);
textView.setLayoutParams(lp);
// Center the text vertically
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
// Set the text starting position
textView.setPadding(36, 0, 0, 0);
return textView;
}

public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
View convertView, ViewGroup parent) {
TextView textView = getGenericView();
textView.setText(getChild(groupPosition, childPosition).toString());
return textView;
}

public Object getGroup(int groupPosition) {
return groups[groupPosition];
}

public int getGroupCount() {
return groups.length;
}

public long getGroupId(int groupPosition) {
return groupPosition;
}

public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
ViewGroup parent) {
TextView textView = getGenericView();
textView.setText(getGroup(groupPosition).toString());
return textView;
}

public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}

public boolean hasStableIds() {
return true;
}

}

原文地址:http://blog.csdn.net/gyflyx/article/details/6461242

更多相关文章

  1. 使用Android(安卓)Studio搭建Android集成开发环境(图文教程)
  2. Android开发环境配置
  3. Ubuntu10.10上的Android开发环境搭建
  4. android 实现日期选择器
  5. Android(安卓)编程下判断当前设备是手机还是平板
  6. android 自定义导航控件
  7. Ubuntu下建立Android开发环境
  8. android 环境配置
  9. Android编译源码时出现的错误: “_FORTIFY_SOURCE”重定义

随机推荐

  1. Android与Java AIO实现简单Echo服务器与
  2. android 项目串口通信(serialPort)开发遇到
  3. android 已知资源名称获取资源ID
  4. 如果获取android源码
  5. windows8.1下android开发环境搭建(Eclips
  6. AndroidStudio安装apk时失败时提示INSTAL
  7. android:string.xml文件中的整型和string
  8. Android 图片裁剪之三剑式(二)
  9. Android布局_View属性
  10. android与WEB服务器交互时的SESSION问题