checkbox的listView 实现多选,全选,反选
主函數<span style="font-family: Arial, Helvetica, sans-serif;">Activity的设置</span>
import java.util.ArrayList;import com.example.all_other.MyAdapter.ViewHolder;import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;public class MainActivity extends Activity {private ListView lv; private MyAdapter mAdapter; private ArrayList<String> list; private Button bt_selectall; private Button bt_cancel; private Button bt_deselectall; private int checkNum; // 记录选中的条目数量 private TextView tv_show;// 用于显示选中的条目数量 /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); /* 实例化各个控件 */ lv = (ListView) findViewById(R.id.lv); bt_selectall = (Button) findViewById(R.id.bt_selectall); bt_cancel = (Button) findViewById(R.id.bt_cancleselectall); bt_deselectall = (Button) findViewById(R.id.bt_deselectall); tv_show = (TextView) findViewById(R.id.tv); list = new ArrayList<String>(); // 为Adapter准备数据 initDate(); // 实例化自定义的MyAdapter mAdapter = new MyAdapter(list, this); // 绑定Adapter lv.setAdapter(mAdapter); // 全选按钮的回调接口 bt_selectall.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // 遍历list的长度,将MyAdapter中的map值全部设为true for (int i = 0; i < list.size(); i++) { MyAdapter.getIsSelected().put(i, true); } // 数量设为list的长度 checkNum = list.size(); // 刷新listview和TextView的显示 dataChanged(); } }); // 反选按钮的回调接口 bt_cancel.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // 遍历list的长度,将已选的设为未选,未选的设为已选 for (int i = 0; i < list.size(); i++) { if (MyAdapter.getIsSelected().get(i)) { MyAdapter.getIsSelected().put(i, false); checkNum--; } else { MyAdapter.getIsSelected().put(i, true); checkNum++; } } // 刷新listview和TextView的显示 dataChanged(); } }); // 取消按钮的回调接口 bt_deselectall.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // 遍历list的长度,将已选的按钮设为未选 for (int i = 0; i < list.size(); i++) { if (MyAdapter.getIsSelected().get(i)) { MyAdapter.getIsSelected().put(i, false); checkNum--;// 数量减1 } } // 刷新listview和TextView的显示 dataChanged(); } }); // 绑定listView的监听器 lv.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // 取得ViewHolder对象,这样就省去了通过层层的findViewById去实例化我们需要的cb实例的步骤 ViewHolder holder = (ViewHolder) arg1.getTag(); // 改变CheckBox的状态 holder.cb.toggle(); // 将CheckBox的选中状况记录下来 MyAdapter.getIsSelected().put(arg2, holder.cb.isChecked()); // 调整选定条目 if (holder.cb.isChecked() == true) { checkNum++; } else { checkNum--; } // 用TextView显示 tv_show.setText("已选中" + checkNum + "项"); } }); } // 初始化数据 private void initDate() { for (int i = 0; i < 15; i++) { list.add("data" + " " + i); } ; } // 刷新listview和TextView的显示 private void dataChanged() { // 通知listView刷新 mAdapter.notifyDataSetChanged(); // TextView显示最新的选中数目 tv_show.setText("已选中" + checkNum + "项"); }; }
适配器Adapter
import java.util.ArrayList;
import java.util.HashMap; import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView; public class MyAdapter extends BaseAdapter { // 填充数据的list private ArrayList<String> list; // 用来控制CheckBox的选中状况 private static HashMap<Integer, Boolean> isSelected; // 上下文 private Context context; // 用来导入布局 private LayoutInflater inflater = null; // 构造器 public MyAdapter(ArrayList<String> list, Context context) { this.context = context; this.list = list; inflater = LayoutInflater.from(context); isSelected = new HashMap<Integer, Boolean>(); // 初始化数据 initDate(); } // 初始化isSelected的数据 private void initDate() { for (int i = 0; i < list.size(); i++) { getIsSelected().put(i, false); } } @Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { return list.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder = null; if (convertView == null) { // 获得ViewHolder对象 holder = new ViewHolder(); // 导入布局并赋值给convertview convertView = inflater.inflate(R.layout.listviewitem, null); holder.tv = (TextView) convertView.findViewById(R.id.item_tv); holder.cb = (CheckBox) convertView.findViewById(R.id.item_cb); // 为view设置标签 convertView.setTag(holder); } else { // 取出holder holder = (ViewHolder) convertView.getTag(); } // 设置list中TextView的显示 holder.tv.setText(list.get(position)); // 根据isSelected来设置checkbox的选中状况 holder.cb.setChecked(getIsSelected().get(position)); return convertView; } public static HashMap<Integer, Boolean> getIsSelected() { return isSelected; } public static void setIsSelected(HashMap<Integer, Boolean> isSelected) { MyAdapter.isSelected = isSelected; } public static class ViewHolder { TextView tv; CheckBox cb; }
}
主函数界面的布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" > <TextView android:id="@+id/tv" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center_vertical" /> <LinearLayout android:id="@+id/line" android:layout_width="fill_parent" android:layout_height="50dp" android:layout_below="@+id/tv" android:orientation="horizontal" > <Button android:id="@+id/bt_selectall" android:layout_width="80dp" android:layout_height="fill_parent" android:text="全选" /> <Button android:id="@+id/bt_cancleselectall" android:layout_width="80dp" android:layout_height="fill_parent" android:text="反选" /> <Button android:id="@+id/bt_deselectall" android:layout_width="80dp" android:layout_height="fill_parent" android:text="取消选择" /> </LinearLayout> <ListView android:id="@+id/lv" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_below="@+id/line" /> </RelativeLayout>
适i配器界面listview_ltem
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="horizontal" ><TextViewandroid:id="@+id/item_tv"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_gravity="center_vertical"android:layout_weight="1" /><CheckBoxandroid:id="@+id/item_cb"android:layout_width="wrap_content"android:layout_height="wrap_content"android:clickable="false"android:focusable="false"android:focusableInTouchMode="false"android:gravity="center_vertical" /></LinearLayout>
checkbox的listView 实现多选,全选,反选相关推荐
- VUE:checkbox 单选框(单选、多选)、一选全选 / 全选一选、表格单选列实现
前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家.点击跳转到教程. 1. 效果:如红框中部分. 一选全选:表头上的单选框选中则下面每行都选中. 全选一选:表中数据每行都 ...
- vue 带全选和多选的表格怎么写_vue实现下拉列表多选全选以及模糊查询的vue组件...
vue实现下拉列表多选全选以及模糊查询的vue组件 发布时间:2018-09-12 17:41, 浏览次数:3776 , 标签: vue <>前端,有时有这样的需求,需要一个下拉列表,还要 ...
- Android开发中ListView多屏的全选、反选功能
[size=medium] 鄙人最近刚开始学习Android,在练习的时候写到一个ListView的全选反选功能.本来以为这个功能很简单,随随便便就能搞定,结果真的下手去做的时候被虐的死去活来,不知道 ...
- 在Indicator中添加动态Checkbox,无需绑定数据源,支持全选 - Ehlib学习(二)
先做设置 DBGrideh属性设置: IndicatorOptions = [gioShowRowIndicatorEh, //小三角指示 gioShowRecNoEh, //数据源行号 gioSho ...
- JQuery全选反选 随其他checkbox自动勾选全选反选
工作中的代码片段 写出来留作以后对比参考 网上没找到自己想要的全选反选代码 干脆自己写了个比较臃肿的 相信还有更加简单的方法 jquery代码 ============================ ...
- 判断复选框checkbox是否被选中,及全选实现
判断checkbox是否被选中 在用vue实现购物车的选中功能时,总价变为只计算选中商品的总价,出现无法判断复选框是否选中的问题. 最后发现应该用document.getElementById去获取i ...
- 统计订单:复选+全选+计算 的列表
话不多说,先上效果图.带选择框和全选按钮的列表,因一些特殊要求还加上些计算功能,这并不是难就是稍微繁琐了些,如 "统计订单功能" 不要说,就是干,那就直接上代码 1,主页面的布局文 ...
- JQuery 循环遍历 , 复选框全选全消反选checked属性
数组 集合的遍历 $.each(list,function(index,item){ this. //this就表示item}); jquery对象的循环遍历 jquery对象.each(func ...
- 计算机快捷键全选,全选快捷键,详细教您电脑全选快捷键是什么
在使用电脑的时候经常会遇到要全选的情况,使用鼠标右键进行全选在频繁使用全选的情况下会影响到编辑的速度,那么全选的快捷键是什么呢?没有使用过的用户就不知道了,下面,小编就来跟大家讲讲电脑全选快捷键. 经 ...
- el-select多选全选
<template><div><el-select @change="handleSelect($event)" v-model="sele ...
最新文章
- 模拟游客一天的生活与旅游java程序代写源码
- [Python]--爬虫 Resources Collection
- 王垠:怎样尊重一个程序员?
- linux系统获取光盘信息api,C++ 通过WIN32 API 获取逻辑磁盘详细信息的几种方法
- [USACO 07DEC]Best Cow Line, Gold
- 将我人生的第一篇博客献给伟大的软件工程这门课程
- Project facet Java version 1.7 is not supported.解决方法
- PHP接口设置10分钟失效
- C/C++[codeup 2080]整数奇偶排序
- JS基础知识大总结史上最全(已完结~)
- 使用pdfobject.js实现在线浏览PDF--前端显示PDF
- Mac修改hosts文件 配置一些IP地址和域名的映射
- python-科研绘图系列(7)-箱型图(盒型图)
- 单播、广播、组播的区别和特点
- ESP8266驱动RDA5807实现的FM收音机,可连接WiFi显示B站粉丝数
- Python开发【模块】:Urllib(二)
- 机器学习学习过程记录
- 王炸!10分钟把ChatGPT部署成24小时微信机器人!
- docker 问题集
- iOS Background Fetch 后台应用程序刷新的使用
热门文章
- 高通 android P remount指令
- Excel单元格如何增加下拉选项(类似于web中的select选项框)
- 黑苹果 Cpu halted Power OFF自动关机
- android Lua下拉框,LuaView
- 麒麟980能运行Linux吗,麒麟980是否可以达到骁龙845或者A12的水准?
- 【Redis :非关系型数据库】
- 【论文笔记】《Social Influence-Based Group Representation Learning for Group Recommendation》
- 小程序中回调函数(callback)的理解
- 如何为中文在上方添加拼音
- 清华这个宝塔的基座——“人文日新”一解