多级列表——ExpandableListView
-
ExpandableListView控件提供的是一个多级列表(一般是两级),我们先来看一下效果图,如图4.18所示为头部列表,单击其中的每一项下面会显示第二级列表,如图4.19所示。

从图4.18和图4.19中可以看出,ExpandableListView为我们提供了一个极好的两级列表的展示控件。
但是如何实现这个两级列表呢?既然ExpandableListView采用列表的形式,它也应该有一个适配器,但是它的适配器不是继承
BaseAdapter,而是要继承它独有的适配器BaseExpandableListAdapter,同时也需要实现其中的几个方法,如表4.3所
示。表4.3 BaseExpandableListAdapter中的方法
方法名称 参数 说明 getGroupId int groupPosition 获取组在给定的位置编号 getChildId int groupPosition, int childPosition 获取给定组的孩子的ID getGroupCount 获取第一级列表的列数 getChildrenCount int groupPosition 获取指定组中孩子的数量 getGroup int groupPosition 获取给定组相关的数据 getGroupView int groupPosition, boolean isExpanded, View convertView, ViewGroup parent 获取一个显示的视图给定组 getChild int groupPosition, int childPosition 获取与孩子在给定的组相关的数据 getChildView int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent 获取一个视图显示在给定的组的孩子的数据 表4.3简单地介绍了BaseExpandableListAdapter中的方法,下面来实现图4.18和图4.19中的效果。
(1)布局文件。从图中可以看出我们需要用到3个布局文件:一个是声明ExpandableListView,一个声明第一级菜单,一个是第二级菜单的布局文件。
声明ExpandableListView的布局文件:<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:background="@drawable/default_bg"><ExpandableListView android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"/></RelativeLayout>
第一级菜单布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="40dip"
android:layout_gravity="center_horizontal" >
<LinearLayout
android:id="@+id/layout_013"
android:layout_width="fill_parent"
android:layout_height="40dip"
android:orientation="horizontal" ><ImageView
android:id="@+id/ImageView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:paddingTop="10dip"
android:src="@drawable/user_group" >
</ImageView>
<RelativeLayout
android:id="@+id/layout_013"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/content_001"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:paddingLeft="10px"
android:textColor="#FFFFFF"
android:textSize="26px" >
</TextView>
<ImageView
android:id="@+id/tubiao"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true" >
</ImageView>
</RelativeLayout>
</LinearLayout>
</LinearLayout>第二级菜单布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/childlayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<ImageView
android:id="@+id/child_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="40dip"
android:background="@drawable/child_image"
android:paddingTop="10dip" >
</ImageView>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/child_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:text=""
android:textSize="16dip" >
</TextView>
<TextView
android:id="@+id/child_text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:text=""
android:textSize="12dip" >
</TextView>
</LinearLayout>
</LinearLayout>(2)写一个类ExAdapter继承BaseExpandableListAdapter,并且实现它的方法。获取给定组的一个显示的视图:
//获取一个显示的视图给定组
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
// 通过getSystemService方法实例化一个视图的填充器
LayoutInflater inflater = (LayoutInflater) getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.member_listview, null);
}
TextView title = (TextView) view.findViewById
(R.id.content_001);
title.setText(getGroup(groupPosition).toString());ImageView image=(ImageView) view.findViewById(R.id.tubiao);
//判断实例可以展开,如果可以则改变右侧的图标
if(isExpanded)
image.setBackgroundResource(R.drawable.btn_browser2);
else image.setBackgroundResource(R.drawable.btn_browser);
return view;
}获取给定组的相关数据及显示的列数:
//获取给定组相关的数据
public Object getGroup(int groupPosition) {
return groupData.get(groupPosition).get(G_TEXT).toString();
}
//获取第一级列表的列数
public int getGroupCount() {
return groupData.size();
}获取一个视图显示在给定的组的孩子的数据:
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
//填充视图
LayoutInflater inflater = (LayoutInflater) getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.member_childitem, null);
}
final TextView title = (TextView) view.findViewById
(R.id.child_text);
title.setText(childData.get(groupPosition).
get(childPosition).get(C_TEXT1).toString());
final TextView title2 = (TextView) view.findViewById
(R.id.child_text2);
title2.setText(childData.get(groupPosition).
get(childPosition).get(C_TEXT2).toString());
return view;
}获取与孩子在给定的组相关的数据,以及孩子组显示的列数:
//获取与孩子在给定的组相关的数据
public Object getChild(int groupPosition, int childPosition) {
return childData.get(groupPosition).get(childPosition).get(C_TEXT1).toString();
}
//获取指定组中孩子的数量
public int getChildrenCount(int groupPosition) {
return childData.get(groupPosition).size();
}(3)通过方法findViewById获取ExpandableListView的实例,为其设置数据显示的适配器。
for (int i = 0; i < 5; i++) {
Map<String, String> curGroupMap = new HashMap<String, String>();
groupData.add(curGroupMap);
curGroupMap.put(G_TEXT, "Group " + i);
List<Map<String, String>> children = new ArrayList<Map<String, String>>();
for (int j = 0; j < 5; j++) {
Map<String, String> curChildMap = new HashMap<String, String>();
children.add(curChildMap);
curChildMap.put(C_TEXT1, "Child " + j);
curChildMap.put(C_TEXT2, "Child " + j);
}
childData.add(children);
}
adapter=new ExAdapter(ExpanListViewDemoActivity.this);
exList = (ExpandableListView) findViewById(R.id.list);
exList.setAdapter(adapter);
exList.setGroupIndicator(null);
exList.setDivider(null);从这个实例中可以看到,ExpandableListView的功能主要在于它的适配器,只要写好了适配器,列表就会显示出来。上面的例子中,我们
没有添加,单击第二级列表不会有任何反应,如果要有点击的效果,需要为ExpandableListView设置一个事
件:setOnChildClickListener,实例化OnChildClickListener类,实现其中的public boolean
onChildClick(ExpandableListView parent, View v,int groupPosition, int
childPosition, long id)方法,这个事件就留给读者去实现。
多级列表——ExpandableListView的更多相关文章
- android--------ExpandableListView的使用多级列表
多级列表ExpandableListView 扩展列表能够显示一个指示在每项显示项的当前状态(状态通常是一个扩展的组,组的孩子,或倒塌,最后一个孩子).使用setchildindicator(draw ...
- Android UI 之实现多级列表TreeView
所谓TreeView就是在Windows中常见的多级列表树,在Android中系统只默认提供了ListView和ExpandableListView两种列表,最多只支持到二级列表的实现,所以如果想要实 ...
- [转]彻底征服Word 2007标题多级列表
[转]彻底征服Word 2007标题多级列表 用Word编写文档的人都知道,一篇长文档一般是需要分章节来划分段落的.在Word中也有对应的工具来完成这项任务,这就是多级列表.然而绝大多数使用Micro ...
- word 多级列表设置
今天写论文碰到了这个问题, 希望能出现这样的效果: 第一章 1.1 1.2 第二章 2.1 2.2 ...... 为了达到这个效果,晕死了.因为我的标题不是普通的默认标题一标题二 比如同济一标题 ...
- WORD2007多级列表
转自玄鸟翩翩 http://hi.baidu.com/shine_yen http://hi.baidu.com/shine_yen/item/01ff2255043bc1aeacc85722 用Wo ...
- Word2010编号列表&多级列表
1.引用场景 对于一份标准.漂亮的word文档,编号列表和多级列表的设置时必不可少的,正因为有它们,文档看起来才更专业,使用起来才更加的方便.如下面截图一般,这是十分常见的多级列表设置 ...
- word 2013 标题设置多级列表
1.问题 要设置标题为多级列表,批量应用 2.解决 1选标题1 2选标题2 ...以此类推.点确定保存即可
- Word自定义多级列表样式
Word自定义多级列表样式: 1. 2. 3.取个名字 在这里鼠标移上时显示 : 4. 5. 定义完成,即可使用:
- 2016word多级列表 一级标题居中后偏左
一.如下图所示,定义好多级列表之后设置标题,但是发现标题居中后偏左. 二.选择多级列表,设置居左对齐
随机推荐
- ckeditor详解
源网页编辑软件FCKEditor在09年发布更新到3.0,并改名为CKEditor.改进后的ckeditor更加模块话,配置更加灵活,和以前的fckeditor使用方式上也有所不同.在我的mvc项目中 ...
- soap和http(转)
http:是一个客户端和服务器端请求和应答的标准(TCP).http协议其目的是为了提供一种发布和接收http页面的方法 一 http协议的客户端与服务器的交互:由HTTP客户端发起一个请求,建立一个 ...
- centos6.4x64安装vncserver
参考文章:http://blog.csdn.net/mchdba/article/details/43058849 主要是远程安装oracle11g需要用到这个东西: 进入系统依次执行下列命令: #查 ...
- Custom.pm
1) 只有一种事情比你培训员工.培养员工然后他们离开要更糟糕,那就是你不培训他们.不培养他们,但他们仍然留下来. 2) PM的含义: Product Manager, Project Manager, ...
- KEIL C51中const和code的使用
code是KEIL C51 扩展的关键字,用code修饰的变量将会被放到CODE区里.但C语里的const关键字好像也有定义不能改变的变量的功能,这两个关键字有什么区别呢?在帮助手册里查找const, ...
- 互斥体与互锁 <第五篇>
互斥体实现了“互相排斥”(mutual exclusion)同步的简单形式(所以名为互斥体(mutex)).互斥体禁止多个线程同时进入受保护的代码“临界区”.因此,在任意时刻,只有一个线程被允许进入这 ...
- Remove Duplicates from Sorted Array 解答
Question Given a sorted array, remove the duplicates in place such that each element appear only onc ...
- Cdev
1,#和##操作符Operator,使用 首个参数返回为一个带引号的字符串 predefined variable was not declared in the scope;
- iOS 之点击按钮改变状态的图片
.h 文件中 @property (strong, nonatomic) IBOutletUIButton *publishBtton; @property (strong, nonatomic) ...
- Eclipse文件覆盖问题
window-preferences-General-Search找到第一行的一个选项 Reuse editors to show matches他的意思是说在同一个编辑里面显示匹配的文件,如果后面有 ...