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"
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/c1" android:state_pressed="true"/>
<item android:drawable="@color/c1" android:state_checked="true"/>
<item android:drawable="@color/c2"/>
</selector>

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


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

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbGpmYmVzdA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">                                   

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbGpmYmVzdA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">


下面是几个获取/设置 选中条目信息的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时可能会报下面错误:
    java.lang.ClassCastException: android.widget.HeaderViewListAdapter cannot be cast to android.widget.SimpleAdapter
ListView有headerView/footerView时,它的原来的Adapter会被封装一下成为HeaderViewListAdapter:

ListAdapter used when a ListView has header views. This ListAdapter wraps another one and also keeps track of the header views and their associated data objects.
This is intended as a base class; you will probably not need to use this class directly in your own code.

可通过下面方式获取原来的Adapter:
    HeaderViewListAdapter hAdapter = (HeaderViewListAdapter) listview.getAdapter();
    MyAdapter my = ( MyAdapter) hAdapter.getWrappedAdapter();

所以,当添加一条header/footer时lv_data.getAdapter()).getWrappedAdapter().getCount()与((HeaderViewListAdapter)listview.getAdapter()).getWrappedAdapter().getCount()是相差1的

Demo地址:

版权声明:本文博客原创文章,博客,未经同意,不得转载。

ListView 实现多选/无线电的更多相关文章

  1. ListView 实现多选/单选

    http://blog.csdn.net/ljfbest/article/details/40685327 ListView自身带了单选.多选模式,可通过listview.setChoiceMode来 ...

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

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

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

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

  4. 【转】Android 带checkbox的listView 实现多选,全选,反选 -- 不错

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

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

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

  6. Android ListView条目全选功能,不用checkbox实现!

    大家好,翻了翻曾经的笔记,发现了一个我特别标记的功能,那就是ListView全选功能,顿时想起了我那个时候苦逼的生涯,因为我大学机械出身,大学毕业了都不知道什么叫代码,在58干了一段销售.实在是干不下 ...

  7. ListView鼠标框选实现蓝色蒙板

    此问题留心已久,今日方悉心求之,记录心得. ListView控件,不论Delphi中的TListView还是c#中的ListView,在开启其MultiSelect属性时,鼠标框选只是显示框张,如下图 ...

  8. WPF DataGrid ListView 等等 改变 选中行 颜色;以及 不变的原因

    WPF中改变选中行的颜色是很简单的,就是用触发器:比如:以DataGrid为例: DataGrid.RowStyle Style TargetType= DataGridRow SetterPrope ...

  9. ListView总结(多选框ListViiew,动态加载,多线程更新ListView中的进度条)

    Why ListView? ListView 如果仅仅出于功能上的需求ListView可能没有存在的必要,ListView能作的事情基本上ScrollView也能胜任.ListView存在的最根本的原 ...

随机推荐

  1. 阿里2016实习offer五面经验与总结(转)

    前言 目前楼主已经拿到阿里实习offer,一共经历了5次面试,其中4轮技术面,1轮HR面试.在这里分享一下自己的面试经验和学习总结.写这篇面经主要是希望能够帮助更多的小伙伴.我本科毕业于中南大学信管专 ...

  2. poj3206(bfs+最小生成树)

    传送门:Borg Maze 题意:有一个迷宫,里面有一些外星人,外星人用字母A表示,#表示墙,不能走,空格可以走,从S点出发,在起点S和A处可以分叉走,问找到所有的外星人的最短路径是多少? 分析:分别 ...

  3. hdu2870(dp求最大子矩阵)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2870 分析:分别转换成'a','b','c'三种来求,其实就跟hdu1505一样了... #inclu ...

  4. A Game of Thrones(1) - Bran

    The morning had dawned clear and cold, with a crispness(易碎:清新) that hinted(暗示:示意) at the end of summ ...

  5. STL源代码剖析 容器 stl_hashtable.h

    本文为senlie原创.转载请保留此地址:http://blog.csdn.net/zhengsenlie hashtable ------------------------------------ ...

  6. html+css实现登录界面

    <!DOCTYPE html> <style type="text/css"> body{ background-color: #555555; } #ti ...

  7. ecshop首页调用指定分类的所有产品(指定一级调二级)

    第一种方法 第一 在/includes/lib_goods.php下增加如下代码,用过网上的直接换掉就可以 function index_get_cat_id_goods_best_list($cat ...

  8. [Unity3D]Unity3D持久性数据的游戏开发PlayerPrefs采用

    大家好,我是秦培,欢迎关注我的博客,我的博客地址">blog.csdn.net/qinyuanpei. 博主今天研究了在Unity3D中的数据持久化问题.数据持久化在不论什么一个开发领 ...

  9. poj1260

    给定n类等级的珍珠 每类的珍珠都有需求的个数ai,和价格pi 为了防止游客只买1颗珍珠,所以购买ai个珍珠时,要加上10个的价格 即(ai+10)*pi 有时,购买高等级的珍珠代替低等级的珍珠时,可能 ...

  10. 《Javascript高级程序设计》读书笔记之对象创建

    <javascript高级程序设计>读过有两遍了,有些重要内容总是会忘记,写一下读书笔记备忘 创建对象 工厂模式 工厂模式优点:有了封装的概念,解决了创建多个相似对象的问题 缺点:没有解决 ...