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而已 ...
随机推荐
- 2次成功投诉EMS和中国移动的经验
上个月要找房子,搬家很多事情,真实头疼...搬家还把腰闪了....现在还有点痛.然后中间碰到 移动宽带 移机的事情,搞得我非常火.然后想起去年投诉EMS的事情,在事情处理完成后,我果断总结了下来,让大 ...
- 谷歌联合 Adobe 发布 Noto 字体【免费下载】
Noto 涵盖了世界上所有主要语言,包括欧洲,非洲,中东,印度语,南亚和东南亚,中亚,美洲和东亚语言.也支持几个少数民族和历史语言.不久前,还发布了针对文.日文.韩文的开源字体——Noto Sans ...
- Node.js Web 开发框架大全《静态文件服务器篇》
这篇文章与大家分享优秀的 Node.js 静态服务器模块.Node 是一个服务器端 JavaScript 解释器,它将改变服务器应该如何工作的概念.它的目标是帮助程序员构建高度可伸缩的应用程序,编写能 ...
- UITextField-secureTextEntry
1.UITextFiled的密文输入 secureTextEntry 安全文本输入 secure:安全 Entry:入口
- [linux]收集一些好玩的命令
1.rev命令 反转输出,输入的字符串. 在终端中输入:rev 输入需要字符串(支持中文) 2.asciiview命令 安装aview:apt-get install aview 再安装imagema ...
- ASP.NET MVC和WebForm 轻松实现前端和后端的双重验证 jquery.validate+ValidationSugar
上次不足的改进 可能上一个贴子给大家带来很多误解,所以我这次把DEMO完善了两个版本 [ASP.NET WEBFROM]和[ ASP.NET MVC] 修改了一些BUG,并且修改了一些细了 在上个贴子 ...
- css中px,em和rem的区别
css中px,em和rem的区别 今天,突然间发现一个特别有意思的问题,就是无意间看到一个网站中的em并不是16px,下面展开了对于px和em以及rem的探究. 首先,px是绝对长度单位,是相对于显示 ...
- 在一个XAML中点击按钮,界面跳转到另一个XAML界面方法
private void ButtonGo_camerapage(object sender, RoutedEventArgs e) { this.Content = new cameraPage() ...
- Wijmo金融图表系列之等量图&成交量柱状图
Wijmo金融图表有很多类型,我们来一一介绍.之前介绍了平均K线图(Heikin-Ashi)和砖形图,现在我们来一起看看等量图和成交量柱状图. 图表 #3: 等量图(EquiVolume) 等量图和K ...
- eclipse优化与标准化记录
1.文件使用UTF-8格式: 2.取消js验证: 3.设置java文件模板