Listview列表收缩与展开——ExpandableListView

ExpandableListView是一个垂直滚动显示两级列表项的视图,与ListView不同的是,它可以有两层:每一层都能够被独立的展开并显示其子项。


好友QQ列表,可以展开,可以收起,在android中,以往用的比较多的是listview,虽然可以实现列表的展示,但在某些情况下,我们还是希望用到可以分组并实现收缩的列表,那就要用到android的ExpandableListView,今天研究了一下这个的用法,也参考了很多资料动手写了一个小demo,实现了基本的功能,下面直接上效果图以及源代码~!

本文效果:


目录:

一、实现原理
二、布局与代码
三、自定义图标
四、图标放置右边
一、实现原理

1,首先必须在布局文件中定义一个ExpandableListView

2,其次创建一级条目对应的布局文件group

3,创建二级条目对应的布局文件child

4,加载ExpandableListView组件的Activity必须继承自ExpandableListActivity

二、布局与代码
1、首先在主布局中activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical" >
  <ExpandableListView
    android:id="@id/android:list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />
</LinearLayout>
2、其次在drawable文件夹定义布局一级列表groups.xml
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
>
  <TextView
    android:id="@+id/textGroup"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingLeft="40px"
    android:paddingTop="6px"
    android:paddingBottom="6px"
    android:textSize="25sp"
    android:text="No data"
  />
</LinearLayout>
3、接着在drawable文件夹定义布局二级列表childs.xml
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
>
    <TextView 
     android:id="@+id/textChild"
   android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:paddingLeft="60px"
     android:paddingTop="10px"
     android:paddingBottom="10px"
     android:textSize="20sp"
   android:text="No Data"
/>
</LinearLayout>
4、然后就是初始化和使用了
package com.example.expandablelistview;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.os.Bundle;
import android.app.ExpandableListActivity;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.SimpleExpandableListAdapter;
import android.widget.Toast;

public class MainActivity extends ExpandableListActivity {
  /**
   * 创建一级条目容器
   */
  List<Map<String, String>> gruops = new ArrayList<Map<String, String>>();
  /**
   * 存放内容, 以便显示在列表中
   */
  List<List<Map<String, String>>> childs = new ArrayList<List<Map<String, String>>>();

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    setListData();
  }

  /**
   * 设置列表内容
   */
  public void setListData() {
    // 创建二个一级条目标题
    Map<String, String> title_1 = new HashMap<String, String>();
    Map<String, String> title_2 = new HashMap<String, String>();
    Map<String, String> title_3 = new HashMap<String, String>();
    title_1.put("group", "林炳文");
    title_2.put("group", "文炳林");
    gruops.add(title_1);
    gruops.add(title_2);

    // 创建二级条目内容
    // 内容一
    Map<String, String> title_1_content_1 = new HashMap<String, String>();
    Map<String, String> title_1_content_2 = new HashMap<String, String>();
    Map<String, String> title_1_content_3 = new HashMap<String, String>();
    title_1_content_1.put("child", "工人");
    title_1_content_2.put("child", "学生");
    title_1_content_3.put("child", "农民");

    List<Map<String, String>> childs_1 = new ArrayList<Map<String, String>>();
    childs_1.add(title_1_content_1);
    childs_1.add(title_1_content_2);
    childs_1.add(title_1_content_3);

    // 内容二
    Map<String, String> title_2_content_1 = new HashMap<String, String>();
    Map<String, String> title_2_content_2 = new HashMap<String, String>();
    Map<String, String> title_2_content_3 = new HashMap<String, String>();
    title_2_content_1.put("child", "猩猩");
    title_2_content_2.put("child", "老虎");
    title_2_content_3.put("child", "狮子");
    List<Map<String, String>> childs_2 = new ArrayList<Map<String, String>>();
    childs_2.add(title_2_content_1);
    childs_2.add(title_2_content_2);
    childs_2.add(title_2_content_3);

    childs.add(childs_1);
    childs.add(childs_2);

    /**
     * 创建ExpandableList的Adapter容器 参数: 1.上下文 2.一级集合 3.一级样式文件 4. 一级条目键值
     * 5.一级显示控件名 6. 二级集合 7. 二级样式 8.二级条目键值 9.二级显示控件名
     * 
     */
    SimpleExpandableListAdapter sela = new SimpleExpandableListAdapter(
        this, gruops, R.drawable.groups, new String[] { "group" },
        new int[] { R.id.textGroup }, childs, R.drawable.childs,
        new String[] { "child" }, new int[] { R.id.textChild });
    // 加入列表
    setListAdapter(sela);
  }

  /**
   * 列表内容按下
   */
  @Override
  public boolean onChildClick(ExpandableListView parent, View v,
      int groupPosition, int childPosition, long id) {
    Toast.makeText(
        MainActivity.this,
        "您选择了"
            + gruops.get(groupPosition).toString()
            + "子编号"
            + childs.get(groupPosition).get(childPosition)
                .toString(), Toast.LENGTH_SHORT).show();
    return super.onChildClick(parent, v, groupPosition, childPosition, id);
  }

  /**
   * 二级标题按下
   */
  @Override
  public boolean setSelectedChild(int groupPosition, int childPosition,
      boolean shouldExpandGroup) {
    return super.setSelectedChild(groupPosition, childPosition,
        shouldExpandGroup);
  }

  /**
   * 一级标题按下
   */
  @Override
  public void setSelectedGroup(int groupPosition) {
    super.setSelectedGroup(groupPosition);
  }
}
5、效果

这是我手机上的效果,点击工人。学生等二级列表时,我手机上会有提示框出现的,但是不知为什么录制下来就是没有。

源码免费下载

三、自定义列表图标
上面的图标是系统自己生成的,下面我们要改成自己的
1、更改自定义图标

在drawable文件夹下新建expandablelistview_change.xml

<?xml version = "1.0"   encoding = "utf-8"?>
<selector   xmlns:android = "http://schemas.android.com/apk/res/android" >      
     <item android:state_expanded = "true"   android:drawable = "@drawable/w2"/>      
     <item android:drawable = "@drawable/w1"/>      
</selector >
2、修改上面布局Activity.main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical" >
  <ExpandableListView
    android:id="@id/android:list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#f5f5f5"
    android:cacheColorHint="#f5f5f5"
    android:groupIndicator="@drawable/expandablelistview_change" />
</LinearLayout>

其实就是加了一句android:groupIndicator="@drawable/expandablelistview_change"

下面我们再来看看效果:

源码免费下载

四、图标放置右边

在上面MainActivity.java的函数setListData()加中:

// 得到屏幕的大小
    DisplayMetrics dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm);
    //图标设置在右边
    getExpandableListView().setIndicatorBounds(dm.widthPixels-60, dm.widthPixels); // 设置指示图标的位置

效果:

源码免费下载:


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Android应用开发过程中,有时候我们需要实现全屏滚动的效果,或者在滚动页面中嵌入ListView等组件。下面就来介绍一下如何实现这些效果。 一、全屏滚动 实现全屏滚动需要用到Android系统提供的ScrollView组件。ScrollView可以包含多个子视图,并且可以在垂直方向上进行滚动。下面是一个简单的例子: ``` <ScrollView android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="这是第一行"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="这是第二行"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="这是第三行"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="这是第四行"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="这是第五行"/> </LinearLayout> </ScrollView> ``` 上面的代码中,我们将ScrollView作为根布局,然后在ScrollView内部添加了一个垂直方向的LinearLayout,这个LinearLayout包含了多个TextView,每个TextView显示一行文本。运行这个应用,可以看到整个页面可以在垂直方向上滚动。 二、在滚动页面中嵌入ListView 有时候我们需要在滚动页面中嵌入ListView,这时候可以使用Android系统提供的NestedScrollView组件。NestedScrollView是ScrollView的子类,可以包含多个子视图,并且可以在垂直方向上进行滚动。和ScrollView不同的是,NestedScrollView可以嵌套其他可滚动的组件,例如ListView等。 下面是一个简单的例子: ``` <android.support.v4.widget.NestedScrollView android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="这是第一行"/> <ListView android:layout_width="match_parent" android:layout_height="wrap_content" android:divider="@null" android:dividerHeight="0dp" android:scrollbars="none"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="这是第三行"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="这是第四行"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="这是第五行"/> </LinearLayout> </android.support.v4.widget.NestedScrollView> ``` 上面的代码中,我们将NestedScrollView作为根布局,然后在NestedScrollView内部添加了一个垂直方向的LinearLayout。这个LinearLayout包含了多个TextView和一个ListView。由于ListView也可以滚动,所以我们需要将它的滚动条隐藏掉,然后就可以在滚动页面中嵌入ListView了。 以上就是Android开发中如何实现全屏滚动和在滚动页面中嵌入ListView的方法。希望对你有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值