ExpandableListView(可扩展的ListView)

ExpandableListVivew是ListView的子类,它在普通ListView的基础上进行了扩展,它把应用中的列表项分为几组,每组里 又可包含多个列表项。ExpandableListVivew的用法与普通ListView的用法非常相似,只是ExpandableListVivew 显示的列表项应该由ExpandableAdapter提供。

实现ExpandableAdapter的三种方式

一是扩展BaseExpandableListAdpter实现ExpandableAdapter。

二是使用SimpleExpandableListAdpater将两个List集合包装成ExpandableAdapter

三是使用simpleCursorTreeAdapter将Cursor中的数据包装成SimpleCuroTreeAdapter

下图为ExpandableListVivew支持的xml属性

XML Attributes

属性名

Related Method

描述

android:childDivider

指定各组内子类表项之间的分隔条

android:childIndicator

显示在子列表旁边的Drawable对象

android:childIndicatorLeft

子列表项指示符的左边约束位置

android:childIndicatorRight

子列表项指示符的右边约束位置

android:groupIndicator

显示在组列表旁边的Drawable对象

android:indicatorLeft

组列表项指示器的左边约束位置

android:indicatorRight

组列表项指示器的右边约束位置

备注:

注:图片不会完全显示,分离子列表项的是一条直线

注:可以是一个图片

注:即从左端0位置开始计数,比如,假设指示符是一个图标,给定这个属性值为

3dip,则表示从左端起3dip开始显示此图标。

注:表示右端到什么位置结束

注:可以是一个图片。

注:表示左端从什么位置开始。

注:表示右端到什么位置结束。

一般适用于ExpandableListViewAdapter都要继承BaseExpandableListAdapter这个类,并且必须重载getGroupViewgetChildView这两个最为重要的方法。

当扩展BaseExpandableListAdapter时,关键是实现如下四个方法:

public abstract ViewgetChildView (int groupPosition, intchildPosition, boolean isLastChild, ViewconvertView, ViewGroup parent)

取得显示给定分组给定子位置的数据用的视图.

参数

groupPosition包含要取得子视图的分组位置.

childPosition 分组中子视图(要返回的视图)的位置.

isLastChild 该视图是否为组中的最后一个视图.

convertView 如果可能,重用旧的视图对象.使用前你应该保证视图对象为非空,并且是否是合适的类型.如果该对象不能转换为可以正确显示数据的视图,该方法就创建新视图.不保证使用先前由getChildView(int, int,boolean, View, ViewGroup)创建的视图.

parent 该视图最终从属的父视图.

返回

指定位置相应的子视图.

public abstract intgetChildrenCount (int groupPosition)

取得指定分组的子元素数.

参数

groupPosition要取得子元素个数的分组位置.

返回

指定分组的子元素个数.

public abstract ViewgetGroupView (int groupPosition, booleanisExpanded, View convertView, ViewGroupparent)

取得用于显示给定分组的视图.这个方法仅返回分组的视图对象,要想获取子元素的视图对象,就需要调用getChildView(int, int, boolean, View, ViewGroup).

参数

groupPosition决定返回哪个视图的组位置.

isExpanded 该组是展开状态还是收起状态.

convertView 如果可能,重用旧的视图对象.使用前你应该保证视图对象为非空,并且是否是合适的类型.如果该对象不能转换为可以正确显示数据的视图,该方法就创建新视图.不保证使用先前由getGroupView(int, boolean,View, ViewGroup)创建的视图.

parent 该视图最终从属的父视图.

返回

指定位置相应的组视图.

public abstract intgetGroupCount ()

取得分组数.

返回

分组数.

BaseExpandableListAdapter的重载的其它方法如下:

public abstract Object getChild(int groupPosition, int childPosition)

取得与指定分组、指定子项目关联的数据.

参数

groupPosition包含子视图的分组的位置.

childPosition 指定的分组中的子视图的位置.

返回

与子视图关联的数据.

public abstract long getChildId(int groupPosition, intchildPosition)

取得给定分组中给定子视图的ID.该组ID必须在组中是唯一的.必须不同于其他所有ID(分组及子项目的ID.

参数

groupPosition包含子视图的分组的位置.

childPosition 要取得ID的指定的分组中的子视图的位置.

返回

与子视图关联的ID.

public abstract longgetCombinedChildId (long groupId, long childId)

取得一览中可以唯一识别子条目的ID(包括分组ID和子条目ID.可扩展列表要求每个条目(分组条目和子条目)具有一个可以唯一识别列表中子条目和分组条目的ID.该方法根据给定子条目ID和分组条目ID返回唯一识别ID.另外,如果hasStableIds()为真,该函数返回的ID必须是固定不变的.

参数

groupId 包含子条目ID的分组条目ID.

childId 子条目的ID.

返回

可以在所有分组条目和子条目中唯一识别该子条目的ID(可能是固定不变的).

public abstract longgetCombinedGroupId (long groupId)

取得一览中可以唯一识别子条目的ID(包括分组ID和子条目ID.可扩展列表要求每个条目(分组条目和子条目)具有一个可以唯一识别列表中子条目和分组条目的ID.该方法根据给定子条目ID和分组条目ID返回唯一识别ID.另外,如果hasStableIds()为真,该函数返回的ID必须是固定不变的.

参数

groupId 分组条目ID.

返回

可以在所有分组条目和子条目中唯一识别该分组条目的ID(可能是固定不变的).

public abstract Object getGroup(int groupPosition)

取得与给定分组关联的数据.

参数

groupPosition分组的位置.

返回

指定分组的数据.

public abstract long getGroupId(int groupPosition)

取得指定分组的ID.该组ID必须在组中是唯一的.必须不同于其他所有ID(分组及子项目的ID.

参数

groupPosition要取得ID的分组位置.

返回

与分组关联的ID.

public abstract booleanhasStableIds ()

是否指定分组视图及其子视图的ID对应的后台数据改变也会保持该ID.

返回

是否相同的ID总是指向同一个对象.

public abstract booleanisChildSelectable (int groupPosition, intchildPosition)

指定位置的子视图是否可选择.

参数

groupPosition包含要取得子视图的分组位置.

childPosition 分组中子视图的位置.

返回

是否子视图可选择.

注意:

XML布局文件中,如果ExpandableListView上一级视图的大小没有严格定义的话,则不能对ExpandableListViewandroid:layout_height属性使用wrap_content值。(例如,如果上一级视图是ScrollView的话,则不应该指定wrap_content的值,因为它可以是任意的长度。不过,如果ExpandableListView的上一级视图有特定的大小的话,比如100像素,则可以使用wrap_content

应用实例:

运行效果图:


项目结构图:


?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 package com.jph.expandablelistviewdemo; import android.os.Bundle; import android.app.Activity; import android.graphics.Color; import android.view.Gravity; import android.view.Menu; import android.view.View; import android.view.ViewGroup; import android.widget.AbsListView; import android.widget.BaseExpandableListAdapter; import android.widget.ExpandableListAdapter; import android.widget.ExpandableListView; import android.widget.ExpandableListView.OnChildClickListener; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast; /** * Describe:</br> * 可扩展的ListView</br> * 本实例主要通过扩展BaseExpandableListAdapter来实现ExpandableListAdapter</br> * 并通过ExpandableListAdapter为ExpandableListView设置数据适配器</br> * 另外,本实例为ExpandableListView的子列表单击事件设置监听器* * @author jph * Date:2014.07.14 * */ public class ExpandableListViewDemoextends Activity { ExpandableListView list; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_expandable_list_view); list=(ExpandableListView)findViewById(R.id.list); //创建一个BaseExpandableListAdapter对象 final ExpandableListAdapter adapter=new BaseExpandableListAdapter() { //设置组视图的图片 int[] logos =new int[] { R.drawable.js, R.drawable.mfzw,R.drawable.yczw}; //设置组视图的显示文字 private String[] category =new String[] {"僵尸 ","魔法植物","远程植物" }; //子视图显示文字 private String[][] subcategory =new String[][] { {"旗帜僵尸","铠甲僵尸","书生见识","铁桶僵尸","尸娃僵尸","舞蹈僵尸" }, {"黄金蘑菇","贪睡蘑菇","大头蘑菇","诱惑植物","多嘴蘑菇","七彩蘑菇" }, {"满天星","风车植物","带刺植物","贪睡植物","双子植物","胆怯蘑菇" } }; //子视图图片 public int[][] sublogos =new int[][] { { R.drawable.js_1,R.drawable.js_2,R.drawable.js_3, R.drawable.js_4,R.drawable.js_5,R.drawable.js_6}, { R.drawable.mfzw_1,R.drawable.mfzw_2,R.drawable.mfzw_3, R.drawable.mfzw_4,R.drawable.mfzw_5,R.drawable.mfzw_6}, { R.drawable.yczw_1,R.drawable.yczw_2,R.drawable.yczw_3, R.drawable.yczw_4,R.drawable.yczw_5,R.drawable.yczw_6 } }; //定义一个显示文字信息的方法 TextView getTextView(){ AbsListView.LayoutParams lp=new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,64); TextView textView=new TextView(ExpandableListViewDemo.this); //设置 textView控件的布局 textView.setLayoutParams(lp); //设置该textView中的内容相对于textView的位置 textView.setGravity(Gravity.CENTER_VERTICAL); //设置txtView的内边距 textView.setPadding(36,0,0,0); //设置文本颜色 textView.setTextColor(Color.BLACK); return textView; } @Override public boolean isChildSelectable(int groupPosition,int childPosition) { // TODO Auto-generated method stub return true; } @Override public boolean hasStableIds() { // TODO Auto-generated method stub return true; } //取得用于显示给定分组的视图. 这个方法仅返回分组的视图对象 @Override public View getGroupView(int groupPosition,boolean isExpanded, View convertView, ViewGroup parent) { // TODO Auto-generated method stub //定义一个LinearLayout用于存放ImageView、TextView LinearLayout ll=new LinearLayout(ExpandableListViewDemo.this); //设置子控件的显示方式为水平 ll.setOrientation(0); //定义一个ImageView用于显示列表图片 ImageView logo=new ImageView(ExpandableListViewDemo.this); logo.setPadding(50,0,0,0); //设置logo的大小(50(padding)+46=96) AbsListView.LayoutParams lparParams=new AbsListView.LayoutParams(96,46); logo.setLayoutParams(lparParams); logo.setImageResource(logos[groupPosition]); ll.addView(logo); TextView textView=getTextView(); textView.setTextSize(20); textView.setText(category[groupPosition]); ll.addView(textView); return ll; } //取得指定分组的ID.该组ID必须在组中是唯一的.必须不同于其他所有ID(分组及子项目的ID). @Override public long getGroupId(int groupPosition) { // TODO Auto-generated method stub return groupPosition; } //取得分组数 @Override public int getGroupCount() { // TODO Auto-generated method stub return category.length; } //取得与给定分组关联的数据 @Override public Object getGroup(int groupPosition) { // TODO Auto-generated method stub return category[groupPosition]; } //取得指定分组的子元素数. @Override public int getChildrenCount(int groupPosition) { // TODO Auto-generated method stub return subcategory[groupPosition].length; } //取得显示给定分组给定子位置的数据用的视图 @Override public View getChildView(int groupPosition,int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { // TODO Auto-generated method stub //定义一个LinearLayout用于存放ImageView、TextView LinearLayout ll=new LinearLayout(ExpandableListViewDemo.this); //设置子控件的显示方式为水平 ll.setOrientation(0); //定义一个ImageView用于显示列表图片 ImageView logo=new ImageView(ExpandableListViewDemo.this); logo.setPadding(0,0,0,0); //设置logo的大小 LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(40,40); logo.setLayoutParams(lp); logo.setImageResource(sublogos[groupPosition][childPosition]); ll.addView(logo); TextView textView=getTextView(); textView.setText(subcategory[groupPosition][childPosition]); ll.addView(textView); return ll; } //取得给定分组中给定子视图的ID. 该组ID必须在组中是唯一的.必须不同于其他所有ID(分组及子项目的ID). @Override public long getChildId(int groupPosition,int childPosition) { // TODO Auto-generated method stub return childPosition; } @Override public Object getChild(int groupPosition,int childPosition) { // TODO Auto-generated method stub return subcategory[groupPosition][childPosition]; } }; list.setAdapter(adapter); //为ExpandableListView的子列表单击事件设置监听器 list.setOnChildClickListener(new OnChildClickListener() { @Override public boolean onChildClick(ExpandableListView parent, View v, int groupPosition,int childPosition,long id) { // TODO Auto-generated method stub Toast.makeText(ExpandableListViewDemo.this,"你单击了:" +adapter.getChild(groupPosition, childPosition), Toast.LENGTH_LONG).show(); return true; } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.expandable_list_view, menu); return true; } }

布局文件:

?
1 2 3 4 5 6 7 8 9 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> <ExpandableListView android:id="@+id/list" android:layout_width="match_parent" android:layout_height="wrap_content" ></ExpandableListView>a </RelativeLayout>

更多相关文章

  1. Android(安卓)Material Design初步认识
  2. android 自定义PickerView 日期滑动选择器
  3. ParallaxViewPager:ViewPager的视差背景效果
  4. Android(安卓)ExpandableListView分组效果
  5. Android(安卓)PopupWindow增加半透明蒙层
  6. Android学习之抽象类AbsListView
  7. android RadioButton单选按钮的使用
  8. Android(安卓)ExpandableListView使用小结(一)
  9. 过期Gallery取代方案horizontalscrollView之幻灯片效果

随机推荐

  1. Ubuntu11.10 + android环境配置流程
  2. 做 Android Layout 达人~
  3. 整理 酷炫 Android 开源UI框架 表格
  4. Android systemserver 解析
  5. 全面了解Android热修复技术
  6. 2013.08.08——— android 中文简繁体转
  7. Android 自定义CheckBox 样式
  8. 关于CoordinatorLayout展开与折叠相关属
  9. Android中向服务器上传图片
  10. Android Layout_weight 属性