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而已 ...
随机推荐
- 网络通信分享(二):外网ip和内网ip
一.内网ip包括两类: 1:tcp/ip协议中,专门保留了三个IP地址区域作为私有地址,其地址范围如下: 10.0.0.0/8:10.0.0.0-10.255.255.255 172.16.0.0/ ...
- UML系列02之 UML类图(一)
概要 本章介绍类图中类的UML表示方法.内容包括:类图介绍实体类的UML表示抽象类和接口的UML表示 转载请注明出处:http://www.cnblogs.com/skywang12345/p/352 ...
- iOS-微信-分享
一.微信原生的分享--准备工作. 1. 需要申请微信AppId. 2. 导入系统架包. SDK文件包括 libWeChatSDK.a,WXApi.h,WXApiObject.h,WechatAuthS ...
- java io系列15之 DataOutputStream(数据输出流)的认知、源码和示例
本章介绍DataOutputStream.我们先对DataOutputStream有个大致认识,然后再深入学习它的源码,最后通过示例加深对它的了解. 转载请注明出处:http://www.cnblog ...
- linux 系统管理 使用技巧
一.这篇文章讲了什么? 这篇文章很有参考性哈.本来是想等一段时间有更多条技巧后在发布的,不过,突然发现,我是去年的今天在博客园落户了,祝我的博客一周岁快乐,希望以后多分享一些文章啦.所以就把草稿箱的其 ...
- Windows及Linux平台下的计时函数总结
本文对Windows及Linux平台下常用的计时函数进行总结,包括精度为秒.毫秒.微秒三种精度的各种函数.比如Window平台下特有的Windows API函数GetTickCount().timeG ...
- IE11之F12 Developer Tools--DOM Explorer
使用DOM Explorer工具查看网页的DOM状态.检查HTML结构和CSS样式,并测试更改以解决显示问题.这可以在元素位置错误或行为异常时帮助你诊断问题,然后解决问题. DOM Explorer图 ...
- js不间断滚动
CSS ul, li { margin: 0; padding: 0; } #scrollDiv { width: 300px; height: 25px; line-height: 25px; bo ...
- C# ICSharpCode.SharpZipLib.dll文件压缩和解压功能类整理,上传文件或下载文件很常用
工作中我们很多时候需要进行对文件进行压缩,比较通用的压缩的dll就是ICSharpCode.SharpZipLib.dll,废话不多了,网上也有很多的资料,我将其最常用的两个函数整理了一下,提供了一个 ...
- LeetCode128:Longest Consecutive Sequence
题目: Given an unsorted array of integers, find the length of the longest consecutive elements sequenc ...