http://blog.csdn.net/ljfbest/article/details/40685327

ListView自身带了单选、多选模式,可通过listview.setChoiceMode来设置:

listview.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);//开启多选模式
listview.setChoiceMode(ListView.CHOICE_MODE_SINGLE);//开启单选模式
listview.setChoiceMode(ListView.CHOICE_MODE_NONE);//默认模式
listview.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);//没用过,不知道用来干嘛的
实现单选
    需要设置:listview.setChoiceMode(ListView.CHOICE_MODE_SINGLE); 
    ListView控件还需要指定一个selector: android:listSelector="@drawable/checkable_item_selector"
  1. <selector xmlns:android="http://schemas.android.com/apk/res/android">
  2. <item android:drawable="@color/c1" android:state_pressed="true"/>
  3. <item android:drawable="@color/c1" android:state_checked="true"/>
  4. <item android:drawable="@color/c2"/>
  5. </selector>

但是单选选中时的颜色还是系统选中的颜色,而不是自己设定的c1,不知道为什么?

实现多选
    设置:listview.setChoiceMode(ListView.CHOICE_MODE_SINGLE); 
    需要对每个item的view实现Checkable接口,以下是LinearLayout实现Checkable接口:
  1. public class CheckableLinearLayout extends LinearLayout implements Checkable {
  2. private boolean mChecked;
  3. public CheckableLinearLayout(Context context, AttributeSet attrs) {
  4. super(context, attrs);
  5. }
  6. @Override
  7. public void setChecked(boolean checked) {
  8. mChecked = checked;
  9. setBackgroundDrawable(checked ? new ColorDrawable(0xff0000a0) : null);//当选中时呈现蓝色
  10. }
  11. @Override
  12. public boolean isChecked() {
  13. return mChecked;
  14. }
  15. @Override
  16. public void toggle() {
  17. setChecked(!mChecked);
  18. }
  19. }
如下使用:
  1. <com.ljfbest.temp.CheckableLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="match_parent"
  3. android:layout_height="wrap_content">
  4. <TextView
  5. android:id="@+id/tv"
  6. android:layout_width="match_parent"
  7. android:layout_height="wrap_content"
  8. android:gravity="center_vertical"
  9. android:minHeight="?android:attr/listPreferredItemHeightSmall"
  10. android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
  11. android:paddingStart="?android:attr/listPreferredItemPaddingStart"
  12. android:textAppearance="?android:attr/textAppearanceListItemSmall" />
  13. </com.ljfbest.temp.CheckableLinearLayout>
以下附上demo图:

                                   

以下是几个获取/设置 选中条目信息的API:
listview.getCheckedItemCount();//获取选中行数:对CHOICE_MODE_NONE无效
listview.getCheckedItemPosition();//获取选中的某行,只针对单选模式有效,返回int
listview.getCheckedItemIds();//
获取选中条目的ids,是long[]类型,注意需要adapter的hasStableIds()返回true,并且这些id是adapter的
getItemId(int position)返回的.demo中有演示

listview.setItemChecked(position, value);//设置某行的状态

ListView 实现多选/单选的更多相关文章

  1. ListView 实现多选/无线电

    ListView本身与无线电.多选模式.由listview.setChoiceMode设置: listview.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE) ...

  2. vue开发购物车,解决全选单选问题

    实现全选单选,在vue中无法通过this获取input中的checkbox的checked属性,但是可以通过vue对input的特殊方式v-model来实现对应数据的绑定,同样也可以通过这种方式实现购 ...

  3. Android在listview添加checkbox实现单选多选操作问题(转)

    转自:http://yangshen998.iteye.com/blog/1310183 在Android某些开发需求当中,有时候需要在listveiw中加入checkbox实现单选,多选操作.表面上 ...

  4. Android在listview添加checkbox实现单选多选操作问题

    android根据View的不同状态更换不同的背景http://www.eoeandroid.com/thread-198029-1-1.html android 模仿朋友网推出的菜单效果[改进版]h ...

  5. 完美解决Android在listview添加checkbox实现单选多选操作问题

    在Android某些开发需求当中,有时候需要在listveiw中加入checkbox实现单选,多选操作.表面上看上去只是改变checkbox那么简单,然而实际开发中,实现起来并不是那么得心应手.尤其当 ...

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

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

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

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

  8. bootstrap 列表 表格 表单 复选 单选 多选 输入框组

    一.列表 ul li 二.表格 table  (http://www.runoob.com/bootstrap/bootstrap-tables.html) 1. 基本表格 <table cla ...

  9. 邮箱性质--全选单选的操作和传值 用属性的name传值

    封装类 using System; using System.Collections.Generic; using System.Web; /// <summary> /// Ha 的摘要 ...

随机推荐

  1. chart控件怎么使x轴标签全部显示出来

    在vs2012中使用chart控件事,x轴的标签过多,致使默认只能显示其中的一部分,如图 当然,我们可以通过设置,使得x轴标签全部显示. 首先,通过chart控件属性,找到   “ChartAreas ...

  2. 【原创】LoadRunner Java Vuser开发环境配置指南

    1 编写目的 本文主要介绍Java运行环境的配置,同时通过编写HelloWorld程序,讲解在LoadRunner下如何开发简单的Java Vuser脚本.关于Java语言的深入学习,大家可以参考其他 ...

  3. HW2.10

    import javax.swing.JOptionPane; public class Solution { public static void main(String[] args) { Str ...

  4. 小波变换和motion信号处理(三)(转)

    这篇文章算太监了,去作者blog提问去吧:http://www.kunli.info/2012/04/08/fourier-wavelet-motion-signal-3/ 从前两篇发布到现在,过去一 ...

  5. Robocopy是微软Windows Server 2003资源工具包中众多多用途的实用程序之一(它是基于强大的拷贝程序

    Robocopy是微软Windows Server 2003资源工具包中众多多用途的实用程序之一(它是基于强大的拷贝程序).没错,Robocopy的功能是拷贝文件,你也许会觉得无聊并且要翻阅下一篇文章 ...

  6. mac使用初级

    imac使用的是login shell,所有开启一个terminal的时候,不会运行.bashrc文件,而是运行.bash_profile文件,因此只需要中home目录新建一个.bash_profil ...

  7. glance image cache

    The Glance Image Cache The Glance API server may be configured to have an optional local image cache ...

  8. 问题-[delphi2007、2010]无法二次启动,报EditorLineEnds.ttr被占用,进程一直有bds.exe?

    问题现象:delphi2007.2010无法二次启动,报EditorLineEnds.ttr被占用,而且进程中一直有bds.exe的进程? 问题原因:问题处理:方法一:可能是系统更新的东东造在的.KB ...

  9. 关于IOS中UIWebView 加载HTML内容

    NSString *strContent=[info objectForKey:@"newContent"]; { NSArray *paths = NSSearchPathFor ...

  10. SVProgressHUD 用法

    SVProgressHUD 是一个第三方的控件,是一个弹出提示层,用来提示 网络加载 或 提示对错,看下面图,你就明白了:     那么,SVProgressHUD 都有什么特点呢:   1. 提示当 ...