原文网址:http://blog.csdn.net/onlyonecoder/article/details/8687811

Demo地址(0分资源)http://download.csdn.net/detail/onlyonecoder/5154352

由于listview的一些特性,刚开始写这种需求的功能的时候都会碰到一些问题,重点就是存储每个checkbox的状态值,在这里分享出了完美解决方法:

布局文件:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="horizontal" >
  6. <TextView
  7. android:id="@+id/tv"
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:layout_gravity="center_vertical" />
  11. <LinearLayout
  12. android:id="@+id/line"
  13. android:layout_width="fill_parent"
  14. android:layout_height="50dp"
  15. android:layout_below="@+id/tv"
  16. android:orientation="horizontal" >
  17. <Button
  18. android:id="@+id/bt_selectall"
  19. android:layout_width="80dp"
  20. android:layout_height="fill_parent"
  21. android:text="全选" />
  22. <Button
  23. android:id="@+id/bt_cancleselectall"
  24. android:layout_width="80dp"
  25. android:layout_height="fill_parent"
  26. android:text="反选" />
  27. <Button
  28. android:id="@+id/bt_deselectall"
  29. android:layout_width="80dp"
  30. android:layout_height="fill_parent"
  31. android:text="取消选择" />
  32. </LinearLayout>
  33. <ListView
  34. android:id="@+id/lv"
  35. android:layout_width="fill_parent"
  36. android:layout_height="fill_parent"
  37. android:layout_below="@+id/line" />
  38. </RelativeLayout>

listView 的item布局文件:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="horizontal" >
  6. <TextView
  7. android:id="@+id/item_tv"
  8. android:layout_width="0dp"
  9. android:layout_height="wrap_content"
  10. android:layout_gravity="center_vertical"
  11. android:layout_weight="1" />
  12. <CheckBox
  13. android:id="@+id/item_cb"
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. android:clickable="false"
  17. android:focusable="false"
  18. android:focusableInTouchMode="false"
  19. android:gravity="center_vertical" />
  20. </LinearLayout>

Activity:

  1. public class Ex_checkboxActivity extends Activity {
  2. private ListView lv;
  3. private MyAdapter mAdapter;
  4. private ArrayList<String> list;
  5. private Button bt_selectall;
  6. private Button bt_cancel;
  7. private Button bt_deselectall;
  8. private int checkNum; // 记录选中的条目数量
  9. private TextView tv_show;// 用于显示选中的条目数量
  10. /** Called when the activity is first created. */
  11. @Override
  12. public void onCreate(Bundle savedInstanceState) {
  13. super.onCreate(savedInstanceState);
  14. setContentView(R.layout.main);
  15. /* 实例化各个控件 */
  16. lv = (ListView) findViewById(R.id.lv);
  17. bt_selectall = (Button) findViewById(R.id.bt_selectall);
  18. bt_cancel = (Button) findViewById(R.id.bt_cancelselectall);
  19. bt_deselectall = (Button) findViewById(R.id.bt_deselectall);
  20. tv_show = (TextView) findViewById(R.id.tv);
  21. list = new ArrayList<String>();
  22. // 为Adapter准备数据
  23. initDate();
  24. // 实例化自定义的MyAdapter
  25. mAdapter = new MyAdapter(list, this);
  26. // 绑定Adapter
  27. lv.setAdapter(mAdapter);
  28. // 全选按钮的回调接口
  29. bt_selectall.setOnClickListener(new OnClickListener() {
  30. @Override
  31. public void onClick(View v) {
  32. // 遍历list的长度,将MyAdapter中的map值全部设为true
  33. for (int i = 0; i < list.size(); i++) {
  34. MyAdapter.getIsSelected().put(i, true);
  35. }
  36. // 数量设为list的长度
  37. checkNum = list.size();
  38. // 刷新listview和TextView的显示
  39. dataChanged();
  40. }
  41. });
  42. // 反选按钮的回调接口
  43. bt_cancel.setOnClickListener(new OnClickListener() {
  44. @Override
  45. public void onClick(View v) {
  46. // 遍历list的长度,将已选的设为未选,未选的设为已选
  47. for (int i = 0; i < list.size(); i++) {
  48. if (MyAdapter.getIsSelected().get(i)) {
  49. MyAdapter.getIsSelected().put(i, false);
  50. checkNum--;
  51. } else {
  52. MyAdapter.getIsSelected().put(i, true);
  53. checkNum++;
  54. }
  55. }
  56. // 刷新listview和TextView的显示
  57. dataChanged();
  58. }
  59. });
  60. // 取消按钮的回调接口
  61. bt_deselectall.setOnClickListener(new OnClickListener() {
  62. @Override
  63. public void onClick(View v) {
  64. // 遍历list的长度,将已选的按钮设为未选
  65. for (int i = 0; i < list.size(); i++) {
  66. if (MyAdapter.getIsSelected().get(i)) {
  67. MyAdapter.getIsSelected().put(i, false);
  68. checkNum--;// 数量减1
  69. }
  70. }
  71. // 刷新listview和TextView的显示
  72. dataChanged();
  73. }
  74. });
  75. // 绑定listView的监听器
  76. lv.setOnItemClickListener(new OnItemClickListener() {
  77. @Override
  78. public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
  79. long arg3) {
  80. // 取得ViewHolder对象,这样就省去了通过层层的findViewById去实例化我们需要的cb实例的步骤
  81. ViewHolder holder = (ViewHolder) arg1.getTag();
  82. // 改变CheckBox的状态
  83. holder.cb.toggle();
  84. // 将CheckBox的选中状况记录下来
  85. MyAdapter.getIsSelected().put(arg2, holder.cb.isChecked());
  86. // 调整选定条目
  87. if (holder.cb.isChecked() == true) {
  88. checkNum++;
  89. } else {
  90. checkNum--;
  91. }
  92. // 用TextView显示
  93. tv_show.setText("已选中" + checkNum + "项");
  94. }
  95. });
  96. }
  97. // 初始化数据
  98. private void initDate() {
  99. for (int i = 0; i < 15; i++) {
  100. list.add("data" + " " + i);
  101. }
  102. ;
  103. }
  104. // 刷新listview和TextView的显示
  105. private void dataChanged() {
  106. // 通知listView刷新
  107. mAdapter.notifyDataSetChanged();
  108. // TextView显示最新的选中数目
  109. tv_show.setText("已选中" + checkNum + "项");
  110. };
  111. }

列表适配器:

  1. package com.notice.listcheck;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import android.content.Context;
  5. import android.view.LayoutInflater;
  6. import android.view.View;
  7. import android.view.ViewGroup;
  8. import android.widget.BaseAdapter;
  9. import android.widget.CheckBox;
  10. import android.widget.TextView;
  11. public class MyAdapter extends BaseAdapter {
  12. // 填充数据的list
  13. private ArrayList<String> list;
  14. // 用来控制CheckBox的选中状况
  15. private static HashMap<Integer, Boolean> isSelected;
  16. // 上下文
  17. private Context context;
  18. // 用来导入布局
  19. private LayoutInflater inflater = null;
  20. // 构造器
  21. public MyAdapter(ArrayList<String> list, Context context) {
  22. this.context = context;
  23. this.list = list;
  24. inflater = LayoutInflater.from(context);
  25. isSelected = new HashMap<Integer, Boolean>();
  26. // 初始化数据
  27. initDate();
  28. }
  29. // 初始化isSelected的数据
  30. private void initDate() {
  31. for (int i = 0; i < list.size(); i++) {
  32. getIsSelected().put(i, false);
  33. }
  34. }
  35. @Override
  36. public int getCount() {
  37. return list.size();
  38. }
  39. @Override
  40. public Object getItem(int position) {
  41. return list.get(position);
  42. }
  43. @Override
  44. public long getItemId(int position) {
  45. return position;
  46. }
  47. @Override
  48. public View getView(int position, View convertView, ViewGroup parent) {
  49. ViewHolder holder = null;
  50. if (convertView == null) {
  51. // 获得ViewHolder对象
  52. holder = new ViewHolder();
  53. // 导入布局并赋值给convertview
  54. convertView = inflater.inflate(R.layout.listviewitem, null);
  55. holder.tv = (TextView) convertView.findViewById(R.id.item_tv);
  56. holder.cb = (CheckBox) convertView.findViewById(R.id.item_cb);
  57. // 为view设置标签
  58. convertView.setTag(holder);
  59. } else {
  60. // 取出holder
  61. holder = (ViewHolder) convertView.getTag();
  62. }
  63. // 设置list中TextView的显示
  64. holder.tv.setText(list.get(position));
  65. // 根据isSelected来设置checkbox的选中状况
  66. holder.cb.setChecked(getIsSelected().get(position));
  67. return convertView;
  68. }
  69. public static HashMap<Integer, Boolean> getIsSelected() {
  70. return isSelected;
  71. }
  72. public static void setIsSelected(HashMap<Integer, Boolean> isSelected) {
  73. MyAdapter.isSelected = isSelected;
  74. }
  75. public static class ViewHolder {
  76. TextView tv;
  77. CheckBox cb;
  78. }
  79. }

版权声明:本文为博主原创文章,未经博主允许不得转载。

【转】Android 带checkbox的listView 实现多选,全选,反选 -- 不错的更多相关文章

  1. Android 带checkbox的listView 实现多选,全选,反选,删除

    activity_main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android&qu ...

  2. Android 带checkbox的listView 实现多选,全选,反选

    由于listview的一些特性,刚开始写这种需求的功能的时候都会碰到一些问题,重点就是存储每个checkbox的状态值,在这里分享出了完美解决方法:     布局文件: [html]   <?x ...

  3. 【转】Android 带checkbox的listView 实现多选,全选,反选----解决checkbox错位问题

    原文网址:http://blog.csdn.net/onlyonecoder/article/details/8687811 Demo地址(0分资源):http://download.csdn.net ...

  4. 带CheckBox美化控件的表格全选

    带CheckBox美化控件 <table class="positionTable commonListTable" id="positionTable" ...

  5. 【转】带checkbox的ListView实现(二)——自定义Checkable控件的实现方法

    原文网址:http://blog.csdn.net/harvic880925/article/details/40475367 前言:前一篇文章给大家展示了传统的Listview的写法,但有的时候我们 ...

  6. Android ListView 中加入CheckBox/RadioButton 选择状态保持、全选、反选实现

    最近在一个项目中,需要在ListView的item中加入CheckBox,但是遇到的一个问题是上下滑动的时候如果有选择了的CheckBox,就会出现选择项错误的问题,下面将个人的解决方法总结如下;先说 ...

  7. Android ListView批量选择(全选、反选、全不选)

    APP的开发中,会常遇到这样的需求:批量取消(删除)List中的数据.这就要求ListVIew支持批量选择.全选.单选等等功能,做一个比较强大的ListView批量选择功能是很有必要的,那如何做呢? ...

  8. 在Indicator中添加动态Checkbox,无需绑定数据源,支持全选 - Ehlib学习(二)

    先做设置 DBGrideh属性设置: IndicatorOptions = [gioShowRowIndicatorEh, //小三角指示 gioShowRecNoEh, //数据源行号 gioSho ...

  9. wpf中为DataGrid添加checkbox支持多选全选

    项目中用到DataGrid, 需要在第一列添加checkbox, 可以多选.全选. 其中涉及的概念DataTemplate, DataGridCellStyle, DataGridCellContro ...

随机推荐

  1. 代码讲解Android Scroller、VelocityTracker

    在编写自定义滑动控件时常常会用到Android触摸机制和Scroller及VelocityTracker.Android Touch系统简介(二):实例详解onInterceptTouchEvent与 ...

  2. PHP简单利用token防止表单重复提交(转)

    <?php/* * PHP简单利用token防止表单重复提交 */function set_token() { $_SESSION['token'] = md5(microtime(true)) ...

  3. is not in the sudoers file.This incident will be reported

    解决方法如下: 1>.进入超级用户模式.也就是输入"su -",系统会让你输入超级用户密码,输入密码后就进入了超级用户模式. 2>.添加文件的写权限.也就是输入命令&q ...

  4. Day5 - Python基础5 常用模块学习

    Python 之路 Day5 - 常用模块学习   本节大纲: 模块介绍 time &datetime模块 random os sys shutil json & picle shel ...

  5. C#HttpWebResponse请求常见的状态码

    成员名称 说明 Continue 等效于 HTTP 状态 100.Continue 指示客户端可能继续其请求. SwitchingProtocols 等效于 HTTP 状态 101.Switching ...

  6. MVC4使用EF6连接mysql数据库

    1.需要安装MySql.Data.Entity.EF6,此dll可以在项目——>管理NuGet程序包里联机搜索MySql.Data.Entity.EF6并安装即可 2.连接字符串需要添加prov ...

  7. new Date()在IE,谷歌,火狐上的一些注意项

    1.new Date()在IE浏览器上IE9以上的可以直接使用new Date("yyyy-MM-dd"),但是在IE8上的时候就要使用new Date("yyyy/MM ...

  8. jquery.cookie用法详细解析

    本篇文章主要是对jquery.cookie的用法进行了详细的分析介绍,需要的朋友可以过来参考下,希望对大家有所帮助 Cookie是由服务器端生成,发送给User-Agent(一般是浏览器),浏览器会将 ...

  9. 电脑安装win8.1后 前面板没有声音的解决办法

    解决部分朋友在给电脑新安装win8.1系统后出现耳机插入电脑前面板音频口没有声音的问题 百度经验:jingyan.baidu.com 方法/步骤 1 1.安装声卡驱动(必须安装,否则无法完成设置) 2 ...

  10. javascript原型prototype的一个你不一定知道的理解

    原型和原型链的故事 相关文章: 为什么原型继承很重要 先来看看一段小代码用以引入要讲的小故事. function Foo() {}; var f1 = new Foo(); Foo.prototype ...