ExpandableListView结合CheckBox实现单选操作

-
ExpandableListView结合CheckBox实现单选操作
1.17MB
Android 两级都带CheckBox的 ExpandableListView
2014-06-25Android中两级都实现带CheckBox的 ExpandableListView,实现两级联动选择
android ExpandableListView+CheckBox实现组选列表_course
2015-12-30需要用ExpandableListView+CheckBox实现一个组选列表,ExpandableListView组列表为部门,子列表为成员。需要选中部门的CheckBox时,选中所有组内成员CheckBox,取消时全部取消。选择单个组内成员时,未全部选中该部门成员,则部门CheckBox不勾选,如选中了所有成员,则自动勾选部门CheckBox。希望以前做过的朋友可以给个详细的例子 下面附上我的代码 ``` import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.widget.BaseExpandableListAdapter; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; import java.util.ArrayList; import java.util.List; import java.util.Map; public class ReportListAdapter extends BaseExpandableListAdapter { private Context context; private List<DepartmentEntity> parentList; private Map<String, List<EmployeeEntity>> map; private List<Boolean> parentStatusList = new ArrayList<Boolean>(); private List<Boolean> childStatusList = new ArrayList<Boolean>(); public ReportListAdapter(Context context, List<DepartmentEntity> parentList, Map<String, List<EmployeeEntity>> map) { this.context = context; this.parentList = parentList; this.map = map; } // 得到子item需要关联的数据 @Override public Object getChild(int groupPosition, int childPosition) { String key = parentList.get(groupPosition).getId(); return (map.get(key).get(childPosition)); } // 得到子item的ID @Override public long getChildId(int groupPosition, int childPosition) { return childPosition; } // 设置子item的组件 @Override public View getChildView(final int groupPosition, final int childPosition, final boolean isLastChild, View convertView, final ViewGroup parent) { if (convertView == null) { convertView = View.inflate(context, R.layout.report_list_child_item, null); } String key = this.parentList.get(groupPosition).getId(); final EmployeeEntity employee = map.get(key).get(childPosition); String name = employee.getName(); TextView employeeName = BaseViewHolder.get(convertView, R.id.employee_name); employeeName.setText(name); CheckBox childCB = BaseViewHolder.get(convertView, R.id.employee_checkbox); // if (employee.isChecked()) { // cb.setChecked(true); // } else { // cb.setChecked(false); // } childCB.setChecked(employee.isChecked()); // notifyDataSetChanged(); childCB.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { CheckBox nowCB = (CheckBox) view; if (nowCB.isChecked()) { employee.setIsChecked(true); // // //获得子item数量 int childSize = getChildrenCount(groupPosition); //其他子item状态 List<Boolean> otherChildStatus = new ArrayList<Boolean>(); otherChildStatus.clear(); for (int i = 0; i < childSize; i++) { if (i != childPosition) { boolean isLastChild1; if (i == childSize - 1) { isLastChild1 = true; } else { isLastChild1 = false; } //获得子item View view1 = getChildView(groupPosition, i, isLastChild1, null, null); CheckBox childCB = (CheckBox) view1.findViewById(R.id.employee_checkbox); boolean cbStatus; if (childCB.isChecked()) { cbStatus = true; } else { cbStatus = false; } otherChildStatus.add(cbStatus); } } //获得父item View parentView1 = getGroupView(groupPosition, true, null, null); //获取父item的cb CheckBox parentCB = (CheckBox) parentView1.findViewById(R.id.department_checkbox); //设置父Item选项框状态 for (int i = 0; i < otherChildStatus.size(); i++) { if (!otherChildStatus.get(i)) { parentList.get(groupPosition).setIsChecked(false); parentCB.setChecked(false); System.out.println("设置父cb不选"); break; } else { parentList.get(groupPosition).setIsChecked(true); parentCB.setChecked(true); notifyDataSetChanged(); System.out.println("设置父cb全选中"); } } notifyDataSetChanged(); } else { //获得父item View parentView = getGroupView(groupPosition, true, null, null); employee.setIsChecked(false); CheckBox parentCB = (CheckBox) parentView.findViewById(R.id.department_checkbox); parentList.get(groupPosition).setIsChecked(false); parentCB.setChecked(false); System.out.println("设置父cb不选"); notifyDataSetChanged(); } } }); // cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { // @Override // public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) { // //获得父item // View parentView = getGroupView(groupPosition, true, null, null); // if (isChecked) { // employee.setIsChecked(true); // // //获得子item数量 // int childSize = getChildrenCount(groupPosition); // //其他子item状态 // List<Boolean> otherChildStatus = new ArrayList<Boolean>(); // for (int i = 0; i < childSize; i++) { // if (i != childPosition) { // boolean isLastChild1; // if (i == childSize - 1) { // isLastChild1 = true; // } else { // isLastChild1 = false; // } // //获得子item // View view = getChildView(groupPosition, i, isLastChild1, null, null); // CheckBox childCB = (CheckBox) view.findViewById(R.id.employee_checkbox); // boolean cbStatus; // if (childCB.isChecked()) { // cbStatus = true; // } else { // cbStatus = false; // } // otherChildStatus.add(cbStatus); // } // } // //设置父Item选项框状态 // for (int i = 0; i < otherChildStatus.size(); i++) { // CheckBox parentCB = (CheckBox) parentView.findViewById(R.id.department_checkbox); // if (otherChildStatus.get(i) == false) { // parentCB.setChecked(false); // parentList.get(groupPosition).setIsChecked(false); // break; // } else { // parentCB.setChecked(true); // parentList.get(groupPosition).setIsChecked(true); // } // } // notifyDataSetChanged(); // } else { // employee.setIsChecked(false); // CheckBox parentCB = (CheckBox) parentView.findViewById(R.id.department_checkbox); // parentCB.setChecked(false); // parentList.get(groupPosition).setIsChecked(false); // notifyDataSetChanged(); // } // // } // }); return convertView; } // 获取当前父item下的子item的个数 @Override public int getChildrenCount(int groupPosition) { String key = parentList.get(groupPosition).getId(); int size = map.get(key).size(); return size; } // 获取当前父item的数据 @Override public Object getGroup(int groupPosition) { return parentList.get(groupPosition); } @Override public int getGroupCount() { return parentList.size(); } @Override public long getGroupId(int groupPosition) { return groupPosition; } // 设置父item组件 @Override public View getGroupView(final int groupPosition, final boolean isExpanded, View convertView, ViewGroup parent) { if (convertView == null) { convertView = View.inflate(context, R.layout.report_list_parent_item, null); } ImageView mgroupimage = BaseViewHolder.get(convertView, R.id.arrow); mgroupimage.setImageResource(R.mipmap.la); if (!isExpanded) { mgroupimage.setImageResource(R.mipmap.shou); } TextView departmentName = BaseViewHolder.get(convertView, R.id.department_name); departmentName.setText(parentList.get(groupPosition).getName()); final CheckBox cb = BaseViewHolder.get(convertView, R.id.department_checkbox); cb.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View view) { System.out.println("点击了父" + groupPosition); CheckBox nowCB = (CheckBox) view; if (nowCB.isChecked()) { //获取子item的数量 int childSize = getChildrenCount(groupPosition); //子item全选中 for (int i = 0; i < childSize; i++) { //获取子item EmployeeEntity child = (EmployeeEntity) getChild(groupPosition, i); child.setIsChecked(true); boolean isLastChild; if (i == childSize - 1) { isLastChild = true; } else { isLastChild = false; } //获得子item View view1 = getChildView(groupPosition, i, isLastChild, null, null); CheckBox childCB = (CheckBox) view1.findViewById(R.id.employee_checkbox); childCB.setChecked(true); System.out.println("子" + groupPosition + "-" + i + " 已经选中"); } //设置父item被完全选中 parentList.get(groupPosition).setIsChecked(true); cb.setChecked(true); System.out.println("父" + groupPosition + " 已经选中"); notifyDataSetChanged(); } else { //获取子item的数量 int childSize = getChildrenCount(groupPosition); //子Item全都不选中 for (int i = 0; i < childSize; i++) { //获取子item数据 EmployeeEntity child = (EmployeeEntity) getChild(groupPosition, i); child.setIsChecked(false); boolean isLastChild; if (i == childSize - 1) { isLastChild = true; } else { isLastChild = false; } //获得子item View view2 = getChildView(groupPosition, i, isLastChild, null, null); CheckBox childCB = (CheckBox) view2.findViewById(R.id.employee_checkbox); childCB.setChecked(false); System.out.println("子" + groupPosition + "-" + i + " 已经设为没选中"); } parentList.get(groupPosition).setIsChecked(false); notifyDataSetChanged(); } } } ); int childSize = getChildrenCount(groupPosition); List<Boolean> nowChildStatus = new ArrayList<Boolean>(); nowChildStatus.clear(); // cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { // @Override // public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) { // System.out.println("点击了父" + groupPosition); // if (isChecked) { // //获取子item的数量 // int childSize = getChildrenCount(groupPosition); // //子item全选中 // for (int i = 0; i < childSize; i++) { // //获取子item // EmployeeEntity child = (EmployeeEntity) getChild(groupPosition, i); // child.setIsChecked(true); // boolean isLastChild; // if (i == childSize - 1) { // isLastChild = true; // } else { // isLastChild = false; // } // //获得子item // View view = getChildView(groupPosition, i, isLastChild, null, null); // CheckBox childCB = (CheckBox) view.findViewById(R.id.employee_checkbox); // childCB.setChecked(true); // System.out.println("子" + groupPosition + "-" + i + " 已经选中"); // } // //设置父item被完全选中 // parentList.get(groupPosition).setIsChecked(true); // cb.setChecked(true); // System.out.println("父" + groupPosition + " 已经选中"); // // notifyDataSetChanged(); // } else { // //获取子item的数量 // int childSize = getChildrenCount(groupPosition); // //子Item全都不选中 // for (int i = 0; i < childSize; i++) { // //获取子item数据 // EmployeeEntity child = (EmployeeEntity) getChild(groupPosition, i); // child.setIsChecked(false); // boolean isLastChild; // if (i == childSize - 1) { // isLastChild = true; // } else { // isLastChild = false; // } // //获得子item // View view = getChildView(groupPosition, i, isLastChild, null, null); // CheckBox childCB = (CheckBox) view.findViewById(R.id.employee_checkbox); // childCB.setChecked(false); // System.out.println("子" + groupPosition + "-" + i + " 已经设为没选中"); // } // parentList.get(groupPosition).setIsChecked(false); // // notifyDataSetChanged(); // } // // } // }); // if (parentList.get(groupPosition).getIsChecked() == 2) { // cb.setChecked(true); // } else { // cb.setChecked(false); // } return convertView; } @Override public boolean hasStableIds() { return true; } //子集是否可选 @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } } ```
expandablelistview和checkbox结合使用,复用问题,头疼好几天,大神帮帮忙啊_course
2016-05-15用了viewholder复用了,当子项滑出范围,选中的checkbox变为未选中,不过选中的删除时删除是正确的,只是这样重复选择(不知道怎么说),体验不好,头疼好几天了 expandablelistv
ExpandableListView+CheckBox单选的问题,请熟悉的大侠指点下,在线等,多谢啦_course
2014-02-08需求是页面有两个分组:出发城市和目的地,出发城市只能单选一个选项,目的地也只能选一个选项,分别选中后点确定按钮提示所选的内容城市名字,页面图: 我的代码如下: public class MainAct
高并发下的Nginx性能优化实战
2019-12-24【超实用课程内容】 本课程内容包含讲解解读Nginx的基础知识,解读Nginx的核心知识、带领学员进行高并发环境下的Nginx性能优化实战,让学生能够快速将所学融合到企业应用中。 【课程如何观看?】 PC端:https://edu.csdn.net/course/detail/27216 移动端:CSDN 学院APP(注意不是CSDN APP哦) 本课程为录播课,课程永久有效观看时长,大家可以抓紧时间学习后一起讨论哦~ 【学员专享增值服务】 源码开放 课件、课程案例代码完全开放给你,你可以根据所学知识,自行修改、优化 下载方式:电脑登录https://edu.csdn.net/course/detail/27216,播放页面右侧点击课件进行资料打包下载
python入门
2018-12-18您观看课程学习后 免费入群领取【超全Python资料包+17本学习电子书】 帮助与数百万年轻人打开人工智能的学习大门!
Python进阶-Pandas数据分析库
2018-12-18您观看课程学习后 免费入群领取【超全Python资料包+17本学习电子书】 Pandas是python中非常常用的数据分析库,在数据分析,机器学习,深度学习等领域经常被使用。本课程会讲解到pandas中最核心的一些知识点,包括Series以及DataFrame的构建,赋值,操作,选择数据,合并等等,以及使用pandas对文件进行读取和写入,使用pandas绘图等等。
JAVA入门精品课程
2018-12-20课程目标: 1、让初学者从小白开始,善于运用知识点,解脱学习的苦恼 2、能够学习更多的工作中使用技巧,成为编程高手
Java系列技术之JavaWeb入门
2018-09-18JavaWeb里的基础核心技术
535KB
2021年数据建模美赛必备LATEX模板
2018-01-272021数模美赛LATEX模板,美赛必备,CTeX,Texlive都可以用~~~~~年份可以任意修改
C/C++程序员实战基础
2019-08-20大数据的入门视频教程
2018-07-26大数据技术入门视频课程,会从基础思想和原理架构开始,全面介绍大数据的思想体系和架构,为学员进一步学习大数据奠定良好的基础。内容涉及大数据的核心问题、大数据核心思想,Google的三篇论文、GFS,Google的分布式文件系统,MapReduce,BigTable、Hadoop和Spark生态体系以及具体应用演示。
-
下载
面向对象与结构化编程区别及异同
面向对象与结构化编程区别及异同
-
学院
【2021】UI自动化测试Selenium3
【2021】UI自动化测试Selenium3
-
博客
了解 css 变量
了解 css 变量
-
博客
TensorFlow学习笔记之一些低阶API
TensorFlow学习笔记之一些低阶API
-
学院
Java星选一卡通
Java星选一卡通
-
学院
ArcGIS Pro2.6和ArcGIS Enterprise学习
ArcGIS Pro2.6和ArcGIS Enterprise学习
-
下载
2016年7个佳的Java框架
2016年7个佳的Java框架
-
学院
性能测试面面观
性能测试面面观
-
博客
@RestController和@Controller区别及Forward和Redirect介绍
@RestController和@Controller区别及Forward和Redirect介绍
-
学院
阿里云云计算ACP考试必备教程
阿里云云计算ACP考试必备教程
-
学院
【数据分析-随到随学】Hive详解
【数据分析-随到随学】Hive详解
-
学院
微信支付2021系列之扫码支付一学就会java版
微信支付2021系列之扫码支付一学就会java版
-
学院
Laya 2.0 开发3D小游戏 入门教学
Laya 2.0 开发3D小游戏 入门教学
-
博客
【python学习笔记-更新和删除一个元组()】
【python学习笔记-更新和删除一个元组()】
-
博客
集成Google登录并获取个人性别等信息
集成Google登录并获取个人性别等信息
-
博客
第一个Drools项目创建
第一个Drools项目创建
-
下载
5种java数据计算层的解决方法
5种java数据计算层的解决方法
-
学院
Python入门到项目直通车
Python入门到项目直通车
-
博客
继承
继承
-
学院
智联万物,京东IoT技术创新与实践
智联万物,京东IoT技术创新与实践
-
博客
【Mac 系统】VScode从零配置java运行环境
【Mac 系统】VScode从零配置java运行环境
-
博客
easyui获取表单所有下拉框,并设置只读;jquery获取元素数组,并设置属性。jquery设置所有元素
easyui获取表单所有下拉框,并设置只读;jquery获取元素数组,并设置属性。jquery设置所有元素
-
博客
微信小程序背景图不显示问题
微信小程序背景图不显示问题
-
下载
Java的内部类和匿名类剖析
Java的内部类和匿名类剖析
-
学院
python数据分析基础
python数据分析基础
-
博客
【Web前端学习系列03-2】—JavaScript入门
【Web前端学习系列03-2】—JavaScript入门
-
学院
数据类型转换、运算符、方法入门
数据类型转换、运算符、方法入门
-
下载
关于Java网络编程
关于Java网络编程
-
下载
Java初始化顺序
Java初始化顺序
-
下载
Java 内存区域与内存溢出
Java 内存区域与内存溢出