【转】Android 带checkbox的listView 实现多选,全选,反选 -- 不错
原文网址:http://blog.csdn.net/onlyonecoder/article/details/8687811
Demo地址(0分资源):http://download.csdn.net/detail/onlyonecoder/5154352
由于listview的一些特性,刚开始写这种需求的功能的时候都会碰到一些问题,重点就是存储每个checkbox的状态值,在这里分享出了完美解决方法:

布局文件:
- <?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>
listView 的item布局文件:
- <?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" >
- <TextView
- android:id="@+id/item_tv"
- android:layout_width="0dp"
- android:layout_height="wrap_content"
- android:layout_gravity="center_vertical"
- android:layout_weight="1" />
- <CheckBox
- android: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>
Activity:
- public class Ex_checkboxActivity 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.main);
- /* 实例化各个控件 */
- lv = (ListView) findViewById(R.id.lv);
- bt_selectall = (Button) findViewById(R.id.bt_selectall);
- bt_cancel = (Button) findViewById(R.id.bt_cancelselectall);
- 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 + "项");
- };
- }
列表适配器:
- package com.notice.listcheck;
- 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;
- }
- }
版权声明:本文为博主原创文章,未经博主允许不得转载。
【转】Android 带checkbox的listView 实现多选,全选,反选 -- 不错的更多相关文章
- Android 带checkbox的listView 实现多选,全选,反选,删除
activity_main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android&qu ...
- Android 带checkbox的listView 实现多选,全选,反选
由于listview的一些特性,刚开始写这种需求的功能的时候都会碰到一些问题,重点就是存储每个checkbox的状态值,在这里分享出了完美解决方法: 布局文件: [html] <?x ...
- 【转】Android 带checkbox的listView 实现多选,全选,反选----解决checkbox错位问题
原文网址:http://blog.csdn.net/onlyonecoder/article/details/8687811 Demo地址(0分资源):http://download.csdn.net ...
- 带CheckBox美化控件的表格全选
带CheckBox美化控件 <table class="positionTable commonListTable" id="positionTable" ...
- 【转】带checkbox的ListView实现(二)——自定义Checkable控件的实现方法
原文网址:http://blog.csdn.net/harvic880925/article/details/40475367 前言:前一篇文章给大家展示了传统的Listview的写法,但有的时候我们 ...
- Android ListView 中加入CheckBox/RadioButton 选择状态保持、全选、反选实现
最近在一个项目中,需要在ListView的item中加入CheckBox,但是遇到的一个问题是上下滑动的时候如果有选择了的CheckBox,就会出现选择项错误的问题,下面将个人的解决方法总结如下;先说 ...
- Android ListView批量选择(全选、反选、全不选)
APP的开发中,会常遇到这样的需求:批量取消(删除)List中的数据.这就要求ListVIew支持批量选择.全选.单选等等功能,做一个比较强大的ListView批量选择功能是很有必要的,那如何做呢? ...
- 在Indicator中添加动态Checkbox,无需绑定数据源,支持全选 - Ehlib学习(二)
先做设置 DBGrideh属性设置: IndicatorOptions = [gioShowRowIndicatorEh, //小三角指示 gioShowRecNoEh, //数据源行号 gioSho ...
- wpf中为DataGrid添加checkbox支持多选全选
项目中用到DataGrid, 需要在第一列添加checkbox, 可以多选.全选. 其中涉及的概念DataTemplate, DataGridCellStyle, DataGridCellContro ...
随机推荐
- 不可错过的手机APP常见8种界面导航样式
前言:相信每个移动开发project师都会遇到,当一个项目开发启动时,须要考虑搭建怎么的框架.一个好的框架.也会决定着一个APP的前途与命运.框架的风格,如今常见的有八种:标签导航.舵式导航.抽屉导航 ...
- [译]C++如何切分字符串
声明: 翻译仅以技术学习和交流为目的,如需转载请务必标明原帖链接. http://stackoverflow.com/questions/236129/how-to-split-a-string-in ...
- TCP异常终止(reset报文)
在之前做智能家居系统时,师弟做的服务端与WiFI盒子(客户端)进行通信时,总是出现异常情况,然后服务端不停地向客户端发送RESET报文,之前一直都不知道是什么情况,因此一直不知道是什么问题导致的,今天 ...
- yii自动登陆的验证机制浅析
一直在使用yii进行开发, 也知道如何去使用, 也仅仅是知道怎么去用罢了, 终归是没研究过源码, 心里发虚, 今天遇到一个问题, 关于自动登陆的问题. 要求就是, 修改登陆保存session天数为自定 ...
- __name__属性
#coding=utf-8#首先我们分别看一下这个模块在不同场景中的__name__的值print __name__ #其次我们看一下__name__属性的常用情况if __name__==" ...
- 鼠标滑动判断与y轴的距离
$(window).scroll(function(){ var y = window.scrollY;//754到达 ...
- (转)php中__autoload()方法详解
转之--http://www.php100.com/html/php/lei/2013/0905/5267.html PHP在魔术函数__autoload()方法出现以前,如果你要在一个程序文件中实例 ...
- oracle问题 《经由直接路径由 EXPORT:V10.02.01 创建的导出文件 IMP-00013: 只有 DBA 才能导入由其他 DBA 导出的文件》
问题: 经由直接路径由 EXPORT:V10.02.01 创建的导出文件 : 只有 DBA 才能导入由其他 DBA 导出的文件 解决方法:用sys 登录,给当前用户授权,授权语句:grant dba ...
- SVN设置钩子文件限制提交文件时必须填写更新日志
进入相应SVN仓库hooks目录,编辑文件pre-commit #!/bin/sh # PRE-COMMIT HOOK## The pre-commit hook is invoked before ...
- <q>标签,短文本引用
想在你的html中加一段引用吗?比如在你的网页的文章里想引用某个作家的一句诗,这样会使你的文章更加出彩,那么<q>标签是你所需要的. 语法: <q>引用文本</q> ...