Android数据适配-ExpandableListView

Android中ListView的用法基本上学的时候都会使用,其中可以使用ArrayAdapter,SimpleAdapter,BaseAdapter去实现,这次主要使用的ExpandableListView展示一种两层的效果,ExpandableListView是android中可以实现下拉list的一个控件类似于QQ那种我好友之后就是一排自己的好友,就是两层效果,实现的话使用SimpleExpandableListAdapter即可。

布局文件

先看下效果

main中xml代码:

1
2
3
4
5
6
7
8
9
10
11
<Button
      android:onClick="test"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:text="FlyElephant" />
  <ExpandableListView
      android:id="@id/android:list"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:drawSelectorOnTop="false" />

 定义一个省份的province.xml:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <TextView
        android:id="@+id/list_provinceText"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:paddingBottom="8px"
        android:paddingLeft="30px"
        android:paddingRight="5px"
        android:paddingTop="8px"
        android:textSize="20sp" />
</LinearLayout>

定义了一个地区的child.xml:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
     
       <TextView
        android:id="@+id/child_text"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:paddingBottom="8px"
        android:paddingLeft="30px"
        android:paddingRight="5px"
        android:paddingTop="8px"
        android:textSize="20sp" />
     
</LinearLayout>

 Demo实现

主要实现代码,代码中都已经注释,其中最主要的SimpleExpandableListAdapter中的参数,这个参数太多,很容易弄错,可以看下注释或者API文档:

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
// 创建一级条目
 List<Map<String, String>> provinces = new ArrayList<Map<String, String>>();
 //创建两个省份一级条目
 Map<String, String> firstProvince= new HashMap<String, String>();
 firstProvince.put("province""河南");
 Map<String, String> secondProvince= new HashMap<String, String>();
 secondProvince.put("province""北京");
 provinces.add(firstProvince);
 provinces.add(secondProvince);
 // 创建一级条目下的的二级地区条目
 List<Map<String, String>> childList1= new ArrayList<Map<String, String>>();
 //同样是在一级条目目录下创建两个对应的二级条目目录
 Map<String, String> child1= new HashMap<String, String>();
 child1.put("child""郑州");
 Map<String, String> child2 = new HashMap<String, String>();
 child2.put("child""开封");
 childList1.add(child1);
 childList1.add(child2);
 //同上
 List<Map<String, String>> childList2 = new ArrayList<Map<String, String>>();
 Map<String, String> child3 = new HashMap<String, String>();
 child3.put("child""海淀");
 Map<String, String> child4 = new HashMap<String, String>();
 child4.put("child""昌平");
 childList2.add(child3);
 childList2.add(child4);
 // 将二级条目放在一个集合里,供显示时使用
 List<List<Map<String, String>>> childs = new ArrayList<List<Map<String, String>>>();
 childs.add(childList1);
 childs.add(childList2);
 /**
  * 使用SimpleExpandableListAdapter显示ExpandableListView
  * 参数1.上下文对象Context
  * 参数2.一级条目目录集合
  * 参数3.一级条目对应的布局文件
  * 参数4.fromto,就是map中的key,指定要显示的对象
  * 参数5.与参数4对应,指定要显示在groups中的id
  * 参数6.二级条目目录集合
  * 参数7.二级条目对应的布局文件
  * 参数8.fromto,就是map中的key,指定要显示的对象
  * 参数9.与参数8对应,指定要显示在childs中的id
  */
 SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter(
         this, provinces, R.layout.list_group, new String[] { "province" },
         new int[] { R.id.list_groupText }, childs, R.layout.child,
         new String[] { "child" }, new int[] { R.id.child_text });
 setListAdapter(adapter);

这个mainActivity需要继承ExpandableListActivity,当然你可以设置其中的点击事件,只要重写一下方法即可:

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
/**
 * 设置哪个二级目录被默认选中
 */
@Override
public boolean setSelectedChild(int groupPosition, int childPosition,
        boolean shouldExpandGroup) {
        //do something
    return super.setSelectedChild(groupPosition, childPosition,
            shouldExpandGroup);
}
/**
 * 设置哪个一级目录被默认选中
 */
@Override
public void setSelectedGroup(int groupPosition) {
    //do something
    super.setSelectedGroup(groupPosition);
}
/**
 * 当二级条目被点击时响应
 */
@Override
public boolean onChildClick(ExpandableListView parent, View v,
        int groupPosition, int childPosition, long id) {
        //do something
    return super.onChildClick(parent, v, groupPosition, childPosition, id);
}

 效果如下:

 

上面这个例子写的有点单调,其实第二个你子的布局直接是空的也行,例如定义一个images.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >
    <ImageView
        android:src="@drawable/open"
        android:layout_width="20dp"
        android:layout_height="20dp" />
    <TextView
        android:id="@+id/txtName"
       android:paddingLeft="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

然后定义一个items.xml

1
2
3
4
5
6
7
8
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/items"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >
     
</TextView>

 代码调用:

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
public class MyExpandleActivity extends Activity {
    /**
     * 实现可扩展展开列ExpandableListView的三种方式
     * 一是使用SimpleExpandableListAdpater将两个List集合包装成ExpandableListView 二是
     * 扩展BaseExpandableListAdpter
     * 三是使用simpleCursorTreeAdapter将Cursor中的数据包装成SimpleCuroTreeAdapter
     */
    private String[] names = { "腾讯""百度""阿里巴巴" };
    private String[][] childnames = { { "马化腾""张小龙","社交"},
            "李彦宏""马东敏","搜索" }, { "马云""陆兆禧","电商" } };
    private ExpandableListView ep;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_expandle);
        // 定义父列表项List数据集合
        List<Map<String, String>> group = new ArrayList<Map<String, String>>();
        // 定义子列表项List数据集合
        List<List<Map<String, String>>> ss = new ArrayList<List<Map<String, String>>>();
        for (int i = 0; i < names.length; i++) {
            // 提供父列表的数据
            Map<String, String> maps = new HashMap<String, String>();
            maps.put("names", names[i]);
            group.add(maps);
            // 提供当前父列的子列数据
            List<Map<String, String>> child = new ArrayList<Map<String, String>>();
            for (int j = 0; j < names.length; j++) {
                Map<String, String> mapsj = new HashMap<String, String>();
                mapsj.put("map", childnames[i][j]);
                child.add(mapsj);
            }
            ss.add(child);
        }
        /**
         * 第一个参数 应用程序接口 this 第二个父列List<?extends Map<String,Object>>集合 为父列提供数据
         * 第三个参数 父列显示的组件资源文件 第四个参数 键值列表 父列Map字典的key 第五个要显示的父列组件id 第六个 子列的显示资源文件
         * 第七个参数 键值列表的子列Map字典的key 第八个要显示子列的组件id
         */
        SimpleExpandableListAdapter expand = new SimpleExpandableListAdapter(
                this, group, R.layout.images, new String[] { "names" },
                new int[] { R.id.txtName }, ss, R.layout.items,
                new String[] { "map" }, new int[] { R.id.items });
        ep = (ExpandableListView) findViewById(R.id.expanable_mylist);
        ep.setAdapter(expand);
    }
}

  效果跟上面相同:

 本文转自Fly_Elephant博客园博客,原文链接:http://www.cnblogs.com/xiaofeixiang/p/4107356.html,如需转载请自行联系原作者

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/392303.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

JavaWeb 命名规则

命名规范命名规范命名规范命名规范 本规范主要针对java开发制定的规范项目命名项目命名项目命名项目命名 项目创建&#xff0c;名称所有字母均小写&#xff0c;组合方式为&#xff1a;com.company.projectName.component.hiberarchy。1. projectName&#xff1a;项目名称2. com…

多元概率密度_利用多元论把握事件概率

多元概率密度Humans have plenty of cognitive strengths, but one area that most of us struggle with is estimating, explaining and preparing for improbable events. This theme underpins two of Nassim Taleb’s major works: Fooled by Randomness and The Black Swa…

nginx php访问日志配置,nginx php-fpm 输出php错误日志的配置方法

由于nginx仅是一个web服务器&#xff0c;因此nginx的access日志只有对访问页面的记录&#xff0c;不会有php 的 error log信息。nginx把对php的请求发给php-fpm fastcgi进程来处理&#xff0c;默认的php-fpm只会输出php-fpm的错误信息&#xff0c;在php-fpm的errors log里也看不…

阿里的技术愿景_技术技能的另一面:领域知识和长期愿景

阿里的技术愿景by Sihui Huang黄思慧 技术技能的另一面&#xff1a;领域知识和长期愿景 (The other side of technical skill: domain knowledge and long-term vision) When we first start our careers as software engineers, we tend to focus on improving our coding sk…

leetcode 721. 账户合并(并查集)

给定一个列表 accounts&#xff0c;每个元素 accounts[i] 是一个字符串列表&#xff0c;其中第一个元素 accounts[i][0] 是 名称 (name)&#xff0c;其余元素是 emails 表示该账户的邮箱地址。 现在&#xff0c;我们想合并这些账户。如果两个账户都有一些共同的邮箱地址&#…

es6重点笔记:数值,函数和数组

本篇全是重点&#xff0c;捡常用的怼&#xff0c;数值的扩展比较少&#xff0c;所以和函数放一起&#xff1a; 一&#xff0c;数值 1&#xff0c;Number.EPSILON&#xff1a;用来检测浮点数的计算&#xff0c;如果误差小于这个&#xff0c;就无误 2&#xff0c;Math.trunc()&am…

SMSSMS垃圾邮件检测器的专业攻击

Note: The methodology behind the approach discussed in this post stems from a collaborative publication between myself and Irene Anthi.注意&#xff1a; 本文讨论的方法背后的方法来自 我本人和 Irene Anthi 之间 的 合作出版物 。 介绍 (INTRODUCTION) Spam SMS te…

php pdo 缓冲,PDO支持数据缓存_PHP教程

/*** 作者&#xff1a;初十* QQ&#xff1a;345610000*/class myPDO extends PDO{public $cache_Dir null; //缓存目录public $cache_expireTime 7200; //缓存时间&#xff0c;默认两小时//带缓存的查询public function cquery($sql){//缓存存放总目录if ($this->cache_Di…

mooc课程下载_如何使用十大商学院的免费课程制作MOOC“ MBA”

mooc课程下载by Laurie Pickard通过劳里皮卡德(Laurie Pickard) 如何使用十大商学院的免费课程制作MOOC“ MBA” (How to make a MOOC “MBA” using free courses from Top 10 business schools) Back when massive open online courses (MOOCs) were new, I started a proje…

leetcode 1584. 连接所有点的最小费用(并查集)

给你一个points 数组&#xff0c;表示 2D 平面上的一些点&#xff0c;其中 points[i] [xi, yi] 。 连接点 [xi, yi] 和点 [xj, yj] 的费用为它们之间的 曼哈顿距离 &#xff1a;|xi - xj| |yi - yj| &#xff0c;其中 |val| 表示 val 的绝对值。 请你返回将所有点连接的最小…

Nagios学习实践系列

其实上篇Nagios学习实践系列——基本安装篇只是安装了Nagios基本组件&#xff0c;虽然能够打开主页&#xff0c;但是如果不配置相关配置文件文件&#xff0c;那么左边菜单很多页面都打不开&#xff0c;相当于只是一个空壳子。接下来&#xff0c;我们来学习研究一下Nagios的配置…

在Salesforce中处理Email的发送

在Salesforce中可以用自带的 Messaging 的 sendEmail 方法去处理Email的发送 请看如下一段简单代码&#xff1a; public boolean TextFormat {get;set;} public string EmailTo {get;set;} public string EmailCC {get;set;} public string EmailBCC {get;set;} public string …

kvm vnc的使用,鼠标漂移等

1.宿主机的vnc&#xff08;virtual Network Computing&#xff09;配置 安装rpm包 yum install tigervnc-server -y 为了防止干扰直接关闭防火墙和selinux /etc/init.d/iptables stop setenforce 0 配置vnc密码和启动vncserver服务 vncpasswd vncserver 2.客户机的vnc 在qemu…

php深浅拷贝,JavaScript 中的深浅拷贝

工作中经常会遇到需要复制 JavaScript 数据的时候&#xff0c;遇到 bug 时实在令人头疼&#xff1b;面试中也经常会被问到如何实现一个数据的深浅拷贝&#xff0c;但是你对其中的原理清晰吗&#xff1f;一起来看一下吧&#xff01;一、为什么会有深浅拷贝想要更加透彻的理解为什…

使用Python进行地理编码和反向地理编码

Geocoding is the process of taking input text, such as an address or the name of a place, and returning a latitude/longitude location. To put it simply, Geocoding is converting physical address to latitude and longitude.地理编码是获取输入文本(例如地址或地点…

java开发简历编写_如何通过几个简单的步骤编写出色的初级开发人员简历

java开发简历编写So you’ve seen your dream junior developer role advertised, and are thinking about applying. It’s time to write that Resume! Nothing better than sitting down to a blank piece of paper and not knowing how to start, right?因此&#xff0c;您…

leetcode 628. 三个数的最大乘积(排序)

给定一个整型数组&#xff0c;在数组中找出由三个数组成的最大乘积&#xff0c;并输出这个乘积。 示例 1: 输入: [1,2,3] 输出: 6 解题思路 最大的乘积可能有两种情况 1.两个最小负数和一个最大正数 2.三个最大正数 代码 class Solution {public int maximumProduct(int[…

[Object-C语言随笔之三] 类的创建和实例化以及函数的添加和调用!

上一小节的随笔写了常用的打印以及很基础的数据类型的定义方式&#xff0c;今天就来一起学习下如何创建类与函数的一些随笔&#xff1b; 首先类的创建&#xff1a;在Xcode下&#xff0c;菜单File&#xff0d;New File&#xff0c;然后出现选择class模板&#xff0c;如下图&…

2024-AI人工智能学习-安装了pip install pydot但是还是报错

2024-AI人工智能学习-安装了pip install pydot但是还是报错 出现这样子的错误&#xff1a; /usr/local/bin/python3.11 /Users/wangyang/PycharmProjects/studyPython/tf_model.py 2023-12-24 22:59:02.238366: I tensorflow/core/platform/cpu_feature_guard.cc:182] This …

grafana 创建仪表盘_创建仪表盘前要问的三个问题

grafana 创建仪表盘可视化 (VISUALIZATIONS) It’s easier than ever to dive into dashboarding, but are you doing it right?深入仪表板比以往任何时候都容易&#xff0c;但是您这样做正确吗&#xff1f; Tableau, Power BI, and many other business intelligence tools …