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而已 ...
随机推荐
- [IR] Information Extraction
阶段性总结 Boolean retrieval 单词搜索 [Qword1 and Qword2] O(x+y) [Qword1 and Qword2]- 改进: Gallo ...
- Hadoop入门进阶课程10--HBase介绍、安装与应用案例
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,博主为石山园,博客地址为 http://www.cnblogs.com/shishanyuan ...
- 简约而不简单:网站着陆页的设计(Landing Page Design)
着陆页是一个在线营销的概念,是指当访客点击一个搜索引擎优化的搜索结果进入的第一个页面或“着陆”页面.这是一个重要的页面,它和提供的产品或服务的广告有点类似,提供了与产品相关的精确的信息,告诉客户可以购 ...
- js-二维数组和多维数组
一.二维数组的表示 myarray[][] 二.二维数组的定义 方法一: var a = new Array(); for(var i=0;i<3;i++){ //一维长度为3 a[i] = n ...
- 100行JS实现HTML5的3D贪吃蛇游戏
js1k.com收集了小于1k的javascript小例子,里面有很多很炫很酷的游戏和特效,今年规则又增加了新花样,传统的classic类型基础上又增加了WebGL类型,以及允许增加到2K的++类型, ...
- 【C#】线程之Parallel
在一些常见的编程情形中,使用任务也许能提升性能.为了简化变成,静态类System.Threading.Tasks.Parallel封装了这些常见的情形,它内部使用Task对象. Parallel.Fo ...
- LeetCode128:Longest Consecutive Sequence
题目: Given an unsorted array of integers, find the length of the longest consecutive elements sequenc ...
- KMA & ex_KMP---Revolving Digits
HDU 4333 Description One day Silence is interested in revolving the digits of a positive integer. I ...
- 常用Keytool 命令
常用Keytool 命令Keytool 是一个JAVA环境下的安全钥匙与证书的管理工具.它管理一个存储了私有钥匙和验证相应公共钥匙的与它们相关联的X.509 证书链的keystore(相当一个数据库, ...
- Intellij idea开发Hadoop MapReduce程序
1.首先下载一个Hadoop包,仅Hadoop即可. http://mirrors.hust.edu.cn/apache/hadoop/common/hadoop-2.6.0/hadoop-2.6.0 ...