关于伸缩列表的ExpandableListView+CheckBox
最近在做关于伸缩列表这块功能,在网上找了许多关于ExpandableListView+CheckBox的例子,不是代码比较臃肿,就是写法混乱,都没什么参考意义,所以动手写了个简洁易懂的Demo.
思路:一、自定义一个adapter,继承BaseExpandableListAdapter,在该类中创建两个列表键值对存储对象,分别将位置id和boolean值存进去,在getGroupView和getChildView中调用相应的方法进行判断
二、同时也可在该类下创建接口,实现选中数据、个数回传。思路简单易理解。
源码介绍:
1、将GroupList和ChildList传入
private List<Map<Integer, Boolean>> gpList = new ArrayList<>();private List<List<Map<Integer, Boolean>>> cdList = new ArrayList<>();public <T> void setItemCheck(List<T> list1, List<List<T>> list2) {gpList = new ArrayList<>();cdList = new ArrayList<>();for (int i = 0; i < list1.size(); i++) {Map<Integer, Boolean> m = new HashMap<>();m.put(i, false);gpList.add(i, m);Map<Integer, Boolean> map = new HashMap<>();List<Map<Integer, Boolean>> list = new ArrayList<>();for (int j = 0; j < list2.get(i).size(); j++) {map.put(j, false);list.add(j, map);}cdList.add(i, list);}}
2、全选和全不选 只需要将上面的的false改为true即可,同时要记得调用notifyDataSetChanged
public <T> void unCheckAll(List<T> list1, List<List<T>> list2) {setItemCheck(list1, list2);this.notifyDataSetChanged();}public <T> void checkAll(List<T> list1, List<List<T>> list2) {gpList = new ArrayList<>();cdList = new ArrayList<>();for (int i = 0; i < list1.size(); i++) {Map<Integer, Boolean> m = new HashMap<>();m.put(i, true);gpList.add(i, m);Map<Integer, Boolean> map = new HashMap<>();List<Map<Integer, Boolean>> list = new ArrayList<>();for (int j = 0; j < list2.get(i).size(); j++) {map.put(j, true);list.add(j, map);}cdList.add(i, list);}this.notifyDataSetChanged();}
3、在onChildClick()方法中调用改类下toggleChild方法
public void toggleChild(int groupPos, int childPos) {if (isShowChk()) {if (cdList.get(groupPos).get(childPos).get(childPos)) {cdList.get(groupPos).get(childPos).put(childPos, false);} else {cdList.get(groupPos).get(childPos).put(childPos, true);}if (cdList.get(groupPos).get(childPos).containsValue(false)) {gpList.get(groupPos).put(groupPos, false);} else {gpList.get(groupPos).put(groupPos, true);}this.notifyDataSetChanged();}}
3、在getChildView和geiGroupView中调用setChildChkView()和setGroupChkView()方法
注意:1、 checkBox.setFocusable(false);让checkbox失去焦点,否则onChildClick()和onGroupClick()方法不会被调用,造成不可点击的问题2、 checkBox.setClickable(false); 让checkbox不可点击,这里要注意,为了不与onChildClick()点击事件冲突
private void toggleGroup(int groupPos) {if (isShowChk()) {if (gpList.get(groupPos).get(groupPos)) {gpList.get(groupPos).put(groupPos, false);} else {gpList.get(groupPos).put(groupPos, true);}if (gpList.get(groupPos).get(groupPos)) {for (int i = 0; i < cdList.get(groupPos).size(); i++) {cdList.get(groupPos).get(i).put(i, true);}} else {for (int i = 0; i < cdList.get(groupPos).size(); i++) {cdList.get(groupPos).get(i).put(i, false);}}this.notifyDataSetChanged();}}protected void setChildChkView(int groupPos, int childPos, CheckBox checkBox) {if (isShowChk()) {checkBox.setVisibility(View.VISIBLE);checkBox.setFocusable(false);checkBox.setClickable(false);if (cdList.get(groupPos).get(childPos).get(childPos)) {checkBox.setChecked(true);} else {checkBox.setChecked(false);}} else {checkBox.setVisibility(View.GONE);}}protected void setGroupChkView(final int groupPos, CheckBox checkBox) {if (isShowChk()) {checkBox.setVisibility(View.VISIBLE);checkBox.setFocusable(false);checkBox.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {toggleGroup(groupPos);callBack.setCheckedNum(getCheckedNum());}});if (gpList.get(groupPos).get(groupPos)) {checkBox.setChecked(true);} else {checkBox.setChecked(false);}} else {checkBox.setVisibility(View.GONE);}}
此实现方式不会照成过多的代码,核心逻辑易写完,易读性强,可修改性强。
源码下载地址
关于伸缩列表的ExpandableListView+CheckBox相关推荐
- android二级列表展开,ExpandableListView控件实现二级列表
效果图如下: 二级列表附有点击事件. 1.布局文件: 此处加了一个自定义的导航RelativeLayout,记得注activity的时候添加 android:theme="@style/Th ...
- Android UI开发第二篇——多级列表(ExpandableListView)
开发中很多地方使用到了多级列表,android可以使用ExpandableListView很好的实现,下面模仿了手机qq的实现,见下图. 多级列表使用了ExpandableListView,自定义了A ...
- 高级控件之分组列表视图(ExpandableListView)
一.ExpandableListView的基础知识 和ListView不同的是它是一个两级的滚动列表视图,每一个组可以展开,显示一些子项,类似于 QQ列表,这些项目来至于ExpandableListA ...
- Android 仿QQ好友分组列表、ExpandableListView的使用详解
ListView只能显示一级列表,如果我们需要像QQ好友列表的那样的效果,就需要用到ExpandableListView,入门新手可能对该控件不是很熟悉,下面就详解一下基本用法,其实跟ListView ...
- android可扩展列表,android ExpandableListView可扩展列表
http://leiwuluan.iteye.com/blog/1508356 先看一效果图. 列表中要有 图片和文字: 所以我们要实现一个自定义的 适配器. 介绍一个类:BaseExpandab ...
- 安卓ListView中CheckBox的使用(支持Item列表项的删除,全选,全不选)
ListView 自身提供了 CheckBox 只需要添加一行代码 getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); 但是这种实现 ...
- 详细讲解ExpandableListView显示和查询仿QQ分组列表用户信息
在我们的项目开发过程中,经常会对用户的信息进行分组,即通过组来显示用户的信息,同时通过一定的查询条件来显示查询后的相关用户信息,并且通过颜色选择器来设置列表信息的背景颜色. 其中借鉴xiaanming ...
- Kotlin ExpandableListView可扩展二级列表,大厂安卓面试真题精选
groupName.add("统计1") groupName.add("统计2") return groupName } - 3.1 StatisticsAda ...
- MVP实现购物车(二级列表),删除结算功能,拦截器+封装okHttp
图片 依赖 compile 'com.squareup.okhttp3:okhttp:3.9.1'compile 'com.google.code.gson:gson:2.8.+'compile 'c ...
最新文章
- C语言诠释--为什么内存是线性分布的。
- SRV记录用来标识某台服务器使用了某个服务,常见于微软系统的目录管理——深入的话需要去折腾Azure Active Directory...
- Apache+Tomcat配置方法
- SpringBoot学习系列之一
- 1029:计算浮点数相除的余
- Spring简介-Spring发展历程
- python的命名空间_python中命名空间的三种方式介绍(附示例)
- 腾讯Node.js基础设施TSW正式开源
- ThinkPHP中的路由是什么意思?
- Atitit 提升科技影响力----软件方面的.docx 目录 1. 大原则	2 1.1. 科技强人必须是创新型[	2 1.2. 要有一定的体量和规模	2 1.3. 产业链齐全 底层基础 --高层应
- 《软件工程与实践》第三版 软工导论知识梳理总结
- linux定义getch函数
- 染成茜色的坂道 文本提取(导出)方法
- 使用libyuv对YUV数据进行缩放,旋转,镜像,裁剪等操作
- 分享2个java j2ee培训的ppt
- 无法安装驱动此计算机,安装Windows系统时,提示“无法在此驱动器上安装Windows”...
- 计算机组装图解,电脑组装图解
- Oracle 服务器 客户端 US7ASCII、 UTF8字符编码问题
- 模拟城市服务器连接中断 正试着,【模拟城市5】确认DRM在线 中断不会被踢
- 建筑群子系统的设计步骤
热门文章
- 全程带阻:记一次授权网络攻防演练(上)
- 苹果三星之后华为放大招,这样的Mate 10绝对值得一等
- PDF编辑技巧1:添加页码和编辑文字
- IDEA怎么查看类继承关系及子类图
- pcm系统设计及matlab仿真实现,PCM系统设计及MATLAB仿真实现.doc
- unity衣服模拟插件MagicaCloth
- 松下壁挂式新风系统推荐_壁挂式新风系统十大品牌有哪些?壁挂式新风系统十大品牌推荐...
- try catch的简单了解
- 四、日志系统:log文件夹下的log.h和log.cpp——TinyWebServer
- 爆笑谷经典对白(3)