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的更多相关文章

  1. android--------ExpandableListView的使用多级列表

    多级列表ExpandableListView 扩展列表能够显示一个指示在每项显示项的当前状态(状态通常是一个扩展的组,组的孩子,或倒塌,最后一个孩子).使用setchildindicator(draw ...

  2. Android UI 之实现多级列表TreeView

    所谓TreeView就是在Windows中常见的多级列表树,在Android中系统只默认提供了ListView和ExpandableListView两种列表,最多只支持到二级列表的实现,所以如果想要实 ...

  3. [转]彻底征服Word 2007标题多级列表

    [转]彻底征服Word 2007标题多级列表 用Word编写文档的人都知道,一篇长文档一般是需要分章节来划分段落的.在Word中也有对应的工具来完成这项任务,这就是多级列表.然而绝大多数使用Micro ...

  4. word 多级列表设置

    今天写论文碰到了这个问题, 希望能出现这样的效果: 第一章 1.1 1.2 第二章 2.1 2.2 ...... 为了达到这个效果,晕死了.因为我的标题不是普通的默认标题一标题二   比如同济一标题 ...

  5. WORD2007多级列表

    转自玄鸟翩翩 http://hi.baidu.com/shine_yen http://hi.baidu.com/shine_yen/item/01ff2255043bc1aeacc85722 用Wo ...

  6. Word2010编号列表&多级列表

    1.引用场景         对于一份标准.漂亮的word文档,编号列表和多级列表的设置时必不可少的,正因为有它们,文档看起来才更专业,使用起来才更加的方便.如下面截图一般,这是十分常见的多级列表设置 ...

  7. word 2013 标题设置多级列表

    1.问题 要设置标题为多级列表,批量应用 2.解决 1选标题1 2选标题2 ...以此类推.点确定保存即可

  8. Word自定义多级列表样式

    Word自定义多级列表样式: 1. 2. 3.取个名字 在这里鼠标移上时显示 : 4. 5. 定义完成,即可使用:

  9. 2016word多级列表 一级标题居中后偏左

    一.如下图所示,定义好多级列表之后设置标题,但是发现标题居中后偏左. 二.选择多级列表,设置居左对齐

随机推荐

  1. DOSUSB 2.0 免费版的限制原理

    两年前,我在写USB的文章时,多次提到了DOSUSB这个东东,这两年也没有关注这方面的变化,最近,有机会重新进入DOSUSB的官方网站(www.dosusb.net),欣喜地发现,这个网站不仅依然存在 ...

  2. C语言中预定义符 __FILE__, __LINE__, __FUNCTION__, __DATE__, __TIME__ 的使用演示

    本文演示了C语言中预定义符 __FILE__, __LINE__, __FUNCTION__, __DATE__, __TIME__ 的使用. 这几个预定义符的名称就没必要再介绍了,顾名思义嘛. // ...

  3. C#安装程序制作让安装后的程序开机自动运行

    1.创建安装项目后要在自己的解决方案是添加一个新的类库项目(ClassLibrary1),并在新类库中添加一下安装程序类(Installer1),在Installer1类中添加如下代码: string ...

  4. css案例学习之用thead、tbody、tfoot实现漂亮的table布局

    首先说说thead.tbody.tfoot <thead> <tbody> <tfoot> 无论前后顺序如何改变, <thead> 内的元素总是在表的最 ...

  5. PHP伪静态与短链接

    如今,Web服务高速发展的时代,各式各类的门户网站,如新浪http://www.sina.com.腾讯http://www.qq.com,这些网站大家都很容易记住,因为这种名称都是有规则和含义的.如果 ...

  6. [Ext JS 4] 实战之多选下拉单 (带checkbox)

    前言 Ext js 创建一个多选下拉单的方式很简单, 使用Ext.form.ComboBox, 设置 multiSelect 为true 就可以了. 但是如果要在每个下拉之前加上一个checkbox, ...

  7. [置顶] 九度笔记之 1434:今年暑假不AC

    题目1434:今年暑假不AC 时间限制:1 秒 内存限制:128 兆 特殊判题:否 提交:307 解决:180 题目描述: “今年暑假不AC?”“是的.”“那你干什么呢?”“看世界杯呀,笨蛋!”“@# ...

  8. jQuery.extend()、jQuery.fn.extend()扩展方法示例详解

    jQuery自定义了jQuery.extend()和jQuery.fn.extend()方法.其中jQuery.extend()方法能够创建全局函数或者选择器,而jQuery.fn.extend()方 ...

  9. js中的注意事项(持续整理)

    1.兼容性 <div class="toutiao_r fl_r" id ="toutiao_r"></div> 这个div中有两个样式 ...

  10. hpux操作系统的关机与重新启动命令

    关机  shutdown -hy 0 重新启动: shutdown -ry 0