Android ExpandableListView (二级列表)
ExpandableListView是android中可以实现下滑子菜单效果的一个控件
在布局文件(layout)目录下创建三个新的 .xml 文件(android xml layout file),为父级菜单列表布局 、子级菜单列表布局 、主布局文件
(我的分别命名为nodes、childs、activity_main)
nodes 和 childs 中拖入 TextView 用来显示内容
activity_main 中拖入控件 ExpendableListView
nodes.xml
<TextView
android:id="@+id/tv_node"
android:layout_width="fill_parent"
android:layout_height="44sp"
android:text="@string/hello_world"
android:textSize="20sp"
android:gravity="left"
android:layout_marginLeft="36sp"
android:layout_marginTop="16sp" />
childs.xml
<TextView
android:id="@+id/tv_node_nick"
android:layout_width="wrap_content"
android:layout_height="54sp"
android:layout_weight="1"
android:text="@string/hello_world"
android:textSize="20sp"
android:gravity="left"
android:layout_marginLeft="40sp"
android:layout_marginTop="17sp"
/>
<TextView
android:id="@+id/tv_node_name"
android:layout_width="wrap_content"
android:layout_height="54sp"
android:layout_weight="1"
android:text="@string/hello_world"
android:textSize="18sp"
android:gravity="right"
android:layout_marginRight="5sp"
android:layout_marginTop="17sp" />
activity_main.xml
<ExpandableListView
android:id="@+id/explv_qq"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ExpandableListView>
在java 文件中写入代码用于和布局文件交互起来
java 代码块
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 创建父容器
List<Map<String, String>> nodes = new ArrayList<Map<String,String>>();
Map<String, String> map1 = new HashMap<String, String>();
map1.put("node", "艾欧尼亚");
Map<String, String> map2 = new HashMap<String, String>();
map2.put("node", "洛克萨斯");
Map<String, String> map3 = new HashMap<String, String>();
map3.put("node", "德玛西亚");
nodes.add(map1);
nodes.add(map2);
nodes.add(map3);
// 创建子容器
List<List<Map<String, String>>> childs = new ArrayList<List<Map<String,String>>>();
List<Map<String, String>> listCh1 = new ArrayList<Map<String,String>>();
Map<String, String> child1_1 = new HashMap<String, String>();
child1_1.put("ch_nick", "九尾妖狐");
child1_1.put("name", "阿狸");
Map<String, String> child1_2 = new HashMap<String, String>();
child1_2.put("ch_nick", "刀锋意志");
child1_2.put("name", "艾瑞莉亚");
Map<String, String> child1_3 = new HashMap<String, String>();
child1_3.put("ch_nick", "琴瑟仙女");
child1_3.put("name", "娑娜");
Map<String, String> child1_4 = new HashMap<String, String>();
child1_4.put("ch_nick", "天启者");
child1_4.put("name", "卡尔玛");
listCh1.add(child1_1);
listCh1.add(child1_2);
listCh1.add(child1_3);
listCh1.add(child1_4);
List<Map<String, String>> listCh2 = new ArrayList<Map<String,String>>();
Map<String, String> child2_1 = new HashMap<String, String>();
child2_1.put("ch_nick", "放逐之刃");
child2_1.put("name", "锐雯");
Map<String, String> child2_2 = new HashMap<String, String>();
child2_2.put("ch_nick", "刀锋之影");
child2_2.put("name", "泰隆");
Map<String, String> child2_3 = new HashMap<String, String>();
child2_3.put("ch_nick", "盲僧");
child2_3.put("name", "李青");
Map<String, String> child2_4 = new HashMap<String, String>();
child2_4.put("ch_nick", "魔蛇之拥");
child2_4.put("name", "卡西奥佩娅");
listCh2.add(child2_1);
listCh2.add(child2_2);
listCh2.add(child2_3);
listCh2.add(child2_4);
List<Map<String, String>> listCh3 = new ArrayList<Map<String,String>>();
Map<String, String> child3_1 = new HashMap<String, String>();
child3_1.put("ch_nick", "龙血武姬 ");
child3_1.put("name", "希瓦娜");
Map<String, String> child3_2 = new HashMap<String, String>();
child3_2.put("ch_nick", "暗夜猎手");
child3_2.put("name", "薇恩");
Map<String, String> child3_3 = new HashMap<String, String>();
child3_3.put("ch_nick", "光辉女郎");
child3_3.put("name", "拉克丝");
Map<String, String> child3_4 = new HashMap<String, String>();
child3_4.put("ch_nick", "哨兵之殇");
child3_4.put("name", "加里奥");
listCh3.add(child3_1);
listCh3.add(child3_2);
listCh3.add(child3_3);
listCh3.add(child3_4);
childs.add(listCh1);
childs.add(listCh2);
childs.add(listCh3);
// 创建适配器
SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter(this,
nodes, R.layout.nodes, new String[]{"node"}, new int[]{R.id.tv_node},
childs, R.layout.childs, new String[]{"ch_nick","name"}, new int[]{R.id.tv_node_nick,R.id.tv_node_name});
// 找到控件
ExpandableListView listView = (ExpandableListView) findViewById(R.id.explv_qq);
// 绑定适配器
listView.setAdapter(adapter);
}
注意:子容器的格式是 List<List<Map<String, String>>>
Android ExpandableListView (二级列表)相关推荐
- android购物车二级列表实现+MVP+Okhttp
//主界面 package com.example.shop;import android.os.Bundle; import android.os.Handler; import android.o ...
- mvp+ExpandableListView二级列表+全选和全不选+单价合计
效果图 主布局 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:andr ...
- Android ExpandableListView 展开列表控件(手机QQ好友列表)
你是否觉得手机QQ上的好友列表那个控件非常棒? 不是..... 那也没关系, 学多一点知识对自己也有益无害. 那么我们就开始吧. 展开型列表控件, 原名ExpandableListView 是普 ...
- android 股票二级列表,Android原生股票图-分时图讲解(二)
Android原生股票图-分时图讲解(一) Android原生股票图-分时图讲解(二) 在 分时图讲解(一)中我们主要实现分时图的绘制和CJL图.MACD图的绘制,以及各个线段的含义和X轴.Y轴各坐标 ...
- RecycleView 二级列表(多级列表)
RecycleView实现二级列表(可以实现多级列表) RecyclerView做的二级列表比官方的ExpandableListView二级列表效率高一些貌似 1.自己的Adapter继承Recycl ...
- android二级列表展开,ExpandableListView控件实现二级列表
效果图如下: 二级列表附有点击事件. 1.布局文件: 此处加了一个自定义的导航RelativeLayout,记得注activity的时候添加 android:theme="@style/Th ...
- android购物车栏,Android怎么实现二级列表购物车功能
Android怎么实现二级列表购物车功能 发布时间:2021-04-16 12:45:40 来源:亿速云 阅读:61 作者:小新 小编给大家分享一下Android怎么实现二级列表购物车功能,希望大家阅 ...
- Android 泽宇二级列表
在开发 Android APP 的时候,难免会需要实现二级列表的情况,而在自己的项目中使用的列表是ExpandableListView 如若转发标明转载处:https://mp.csdn.net/po ...
- Kotlin ExpandableListView可扩展二级列表,大厂安卓面试真题精选
groupName.add("统计1") groupName.add("统计2") return groupName } - 3.1 StatisticsAda ...
最新文章
- CATransition
- spring boot集成swagger,自定义注解,拦截器,xss过滤,异步调用,定时任务案例...
- 【PD】PowerDesigner生成数据字典
- 【Python】IDLE中文本进度条的单行动态刷新无法实现分析
- ssh(Spring+Spring mvc+hibernate)——EmpDaoImpl.java
- 任正非:只有教师待遇得到提升 教育才会较大发展
- 利用python安装opencv_科学网—Anaconda Python PyCharm PyQT5 OpenCV PyTorch TF2.0 安装指南 - 张重生的博文...
- 【C语言】数学也没辣么难嘛
- 鼠标点击后的CSS3跑马灯效果
- 英特尔资深院士马克·波尔的传奇人生 | 人物志
- html超浪漫的3D动态相册表白网站制作 html程序员专属情人节表白网站
- MyBatis出现参数索引越界
- Android 系统网络框架
- 样本调试之 loadlibrary 报错1114
- 统计独立访客需求mysql_网站流量分析项目day04
- Avfoundation 相机指定裁剪区域
- Vue中分页组件的用法
- React -- Switch的使用
- MySQL(三) 完整性约束
- SQL查询优化方法 提高SQL查询效率 数据库的哪些字段适合添加索引