android 之 ExpandableListView列表中的列表
有时候,我们需要设计这样一个界面,外面有一个列表,当我们点击其中列表中的某个条目时,就会展开这个条目,出现一个新的列表。比如下图:(程序运行的效果图,在这里贴出来)

当我们点击第一项时,视图变为:

------------------------------------------------------------------------------------------------------------------------------
为了实现这样的效果,需要定义三个布局,包括显示的main_activity(主布局),group(第一级列表布局),child(第二级列表布局),下面贴代码,解释都在注释里面:
main_activity:
<LinearLayout 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"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.expandlistaacitvity.MainActivity" > <!-- android文档中要求ExpandableListView的id必须为list,否则会抛出错误,有兴趣的可以试试
#ff0000 为红色
drawSelectorOntop=false是设置当你选中某一项时,任能清楚显示当前项(我试了试好像没有什么改变。。。。)-->
<ExpandableListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawSelectorOnTop="false"/> <!-- 当上面没有数据可以显示时,就会显示这个TextView。这个id也规定必须用empty,否者前面的话就没作用了 -->
<TextView
android:id="@android:id/empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff0000"
android:text="no data" /> </LinearLayout>
group.xml:
<?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" > <!-- 这个是定义的第一级列表的布局
#0000ff是深青色 -->
<TextView
android:id="@+id/groupTo"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="#0000ff"
android:paddingTop="20dp"
android:paddingLeft="60dp"
android:paddingBottom="10dp"
android:textSize="26sp"
android:text="no data"/>
</LinearLayout>
child.xml:
<?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:layout_margin="5dp"
android:orientation="vertical" > <!-- 这里定义的是列表中的第二级列表 ,当没有数据时,显示no data-->
<TextView
android:id="@+id/childTo"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:paddingTop="20dp"
android:paddingLeft="50dp"
android:paddingBottom="10dp"
android:background="#00ff00"
android:text="no data"/>
</LinearLayout>
接下来是主活动了,同样解释在代码里:
package com.example.expandlistaacitvity; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import android.app.ExpandableListActivity;
import android.os.Bundle;
import android.widget.SimpleExpandableListAdapter; public class MainActivity extends ExpandableListActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //定义两个一级条目
List<Map<String, String>> groups = new ArrayList<Map<String,String>>();
Map<String, String> group1 = new HashMap<String, String>();
group1.put("group", "group1第一级列表one");
groups.add(group1);
Map<String,String> group2 = new HashMap<String, String>();
group2.put("group", "group2第一级列表two");
groups.add(group2); //定义一个list,里面存放的是第一个一级条目中的二级条目
List<Map<String, String>> child1 = new ArrayList<Map<String,String>>();
Map<String, String> child1Date1 = new HashMap<String, String>();
child1Date1.put("child", "clild1date1第二级列表");
child1.add(child1Date1);
Map<String, String> child1Date2 = new HashMap<String, String>();
child1Date2.put("child", "child1Date2第二级列表");
child1.add(child1Date2); //定义一个List,为第二个一级条目的二级条目
List<Map<String, String>> child2 = new ArrayList<Map<String,String>>();
Map<String, String> child2Date1 = new HashMap<String, String>();
child2Date1.put("child", "child222Date1第二级列表");
child2.add(child2Date1); //将两个二级条目放入到list中
List<List<Map<String, String>>> childs = new ArrayList<List<Map<String,String>>>();
childs.add(child1);
childs.add(child2); /*
* 1.上下文
* 2.一级列表的数据,包含各个group
* 3.指定一级列表的布局
* 4.指定一级列表的key
* 5.指定一级条目的控件显示的id
* 下面同上
*/
SimpleExpandableListAdapter simpleExpandableListAdapter = new SimpleExpandableListAdapter
(this, groups, R.layout.group,new String[]{"group"}, new int[]{R.id.groupTo},
childs,R.layout.child,new String[]{"child"},new int[]{R.id.childTo}
);
setListAdapter(simpleExpandableListAdapter);
}
}
android 之 ExpandableListView列表中的列表的更多相关文章
- Python3基础 list 访问列表中的列表的元素
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- Python3基础 not in列表名 判断一个元素是否不在列表中列表中
镇场诗:---大梦谁觉,水月中建博客.百千磨难,才知世事无常.---今持佛语,技术无量愿学.愿尽所学,铸一良心博客.------------------------------------------ ...
- 每日学习心得:SharePoint 为列表中的文件夹添加子项(文件夹)、新增指定内容类型的子项、查询列表中指定的文件夹下的内容
前言: 这里主要是针对列表中的文件下新增子项的操作,同时在新建子项时,可以为子项指定特定的内容类型,在某些时候需要查询指定的文件夹下的内容,针对这些场景都一一给力示例和说明,都是一些很小的知识点,希望 ...
- Python中关于列表嵌套列表的处理
在处理列表的时候我们经常会遇到列表中嵌套列表的结构,如果我们要把所有元素放入一个新列表,或者要计算所有元素的个数的话应该怎么做呢? 第一个例子 对于上图中的这样一组数据,如果我们要知道这个CSV文件中 ...
- python学习一(Python中的列表)
python中有两种列表,分别用()和[]表示: 例如: letter = ('a','b','c') letter = ['a','b','c'] 用小括号表示的列表初始化后不允许修改,而中中括号生 ...
- flutter中的列表组件
列表布局是我们项目开发中最常用的一种布局方式.Flutter 中我们可以通过 ListView 来定义列表项,支持垂直和水平方向展示.通过一个属性就可以控制列表的显示方向.列表有以下分类: 垂直列表 ...
- Python中list列表的常见操作
Python的list是一个列表,用方括号包围,不同元素间用逗号分隔. 列表的数据项不需要具有相同的类型.(列表还可以嵌套,即列表中的列表) 每个元素都有一个索引(表示位置),从0开始:可以用索引-1 ...
- Python12之列表3(列表常用操作符,以及常用的内置函数)
一.比较操作符: list1 > list2 比较两个列表的大小,Python会直接从列表的第0个元素进行比较,若相等,则继续下一个比较 得到的结果是布尔值 二.逻辑操作 ...
- Android开发 ExpandableListView 可折叠列表详解
前言 在需要实现一个List的item需要包含列表的时候,我们就可以选择ExpandableListView. 其实这个View的原始设计还是ListView的那套.就是增加2层的ListView而已 ...
随机推荐
- 后端码农谈前端(HTML篇)第一课:HTML概述
一.什么是HTML? HTML不是编程语言,是用来描述网页文档(页面结构)的一种标记语言: HTML指超文本标记语言(Hyper Text Markup Language),之所以称为超文本标记语言, ...
- Unity 3D 一个简单的角色控制脚本
之所以写这个脚本,是因为我想起了我还是新手的时候,那时为了一个角色控制脚本百度了半天还是一无所获,因为看不懂啊,都写的太高级了 希望这个脚本能够帮助那些 像曾经的我一样迷失于代码中的新手们能够清晰的理 ...
- Node.js Web 开发框架大全《静态文件服务器篇》
这篇文章与大家分享优秀的 Node.js 静态服务器模块.Node 是一个服务器端 JavaScript 解释器,它将改变服务器应该如何工作的概念.它的目标是帮助程序员构建高度可伸缩的应用程序,编写能 ...
- IOS开发UI基础之UIButton
什么是按钮?
- iOS第三方类库汇总【持续更新】
在我们平时开发中会经常使用一些第三方开发的开源类库.这样会有效地提高我们开发项目的效率,在这里我找了好几十个进行一个汇总,供大家参考使用,方便大家在需要的时候能容易找到. UI篇 awesome-io ...
- 重新想象 Windows 8 Store Apps (55) - 绑定: MVVM 模式
[源码下载] 重新想象 Windows 8 Store Apps (55) - 绑定: MVVM 模式 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 绑定 通过 M ...
- nginx配合modsecurity实现WAF功能
一.准备工作 系统:centos 7.2 64位.nginx1.10.2, modsecurity2.9.1 owasp3.0 1.nginx:http://nginx.org/download/ng ...
- mybatis中自建的类型别名
在使用mybatis过程中经常用到类型别名,除了我们自己新建的别名外,mybatis还自带了很多类型别名和java中的类型的映射,下面先看一个自建的别名的配置 <typeAliases> ...
- Asp.net发布的CheckList
Asp.net Web 应用程序正式发布前,我们还是做一些检查,所以需要这个CheckList,如下图今天的Asp.net 已演化这样的了: 但不管是什么组件,目前的Web最终还得通过H ...
- ASP.NET WebAPI 15 CORS
同源策略 首先基于安全的原因,浏览器是存在同源策略这个机制的,同源策略阻止从一个源加载的文档或脚本获取或设置另一个源加载的文档的属性. 对于同源必须要求URL在如下几个方面相同: 网络协议(http与 ...