ListView 实现多选/单选
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);//没用过,不知道用来干嘛的
- <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,不知道为什么?
- 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>
获取选中条目的ids,是long[]类型,注意需要adapter的hasStableIds()返回true,并且这些id是adapter的
getItemId(int position)返回的.demo中有演示
listview.setItemChecked(position, value);//设置某行的状态
ListView 实现多选/单选的更多相关文章
- ListView 实现多选/无线电
ListView本身与无线电.多选模式.由listview.setChoiceMode设置: listview.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE) ...
- vue开发购物车,解决全选单选问题
实现全选单选,在vue中无法通过this获取input中的checkbox的checked属性,但是可以通过vue对input的特殊方式v-model来实现对应数据的绑定,同样也可以通过这种方式实现购 ...
- Android在listview添加checkbox实现单选多选操作问题(转)
转自:http://yangshen998.iteye.com/blog/1310183 在Android某些开发需求当中,有时候需要在listveiw中加入checkbox实现单选,多选操作.表面上 ...
- Android在listview添加checkbox实现单选多选操作问题
android根据View的不同状态更换不同的背景http://www.eoeandroid.com/thread-198029-1-1.html android 模仿朋友网推出的菜单效果[改进版]h ...
- 完美解决Android在listview添加checkbox实现单选多选操作问题
在Android某些开发需求当中,有时候需要在listveiw中加入checkbox实现单选,多选操作.表面上看上去只是改变checkbox那么简单,然而实际开发中,实现起来并不是那么得心应手.尤其当 ...
- Android 带checkbox的listView 实现多选,全选,反选,删除
activity_main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android&qu ...
- Android 带checkbox的listView 实现多选,全选,反选
由于listview的一些特性,刚开始写这种需求的功能的时候都会碰到一些问题,重点就是存储每个checkbox的状态值,在这里分享出了完美解决方法: 布局文件: [html] <?x ...
- bootstrap 列表 表格 表单 复选 单选 多选 输入框组
一.列表 ul li 二.表格 table (http://www.runoob.com/bootstrap/bootstrap-tables.html) 1. 基本表格 <table cla ...
- 邮箱性质--全选单选的操作和传值 用属性的name传值
封装类 using System; using System.Collections.Generic; using System.Web; /// <summary> /// Ha 的摘要 ...
随机推荐
- java.lang.IllegalStateException: Required view 'text1' with ID 2131492943 for field 'mText' was not found. If this view is optional add '@Nullable' annotation
使用ButterKnife 8.2的时候遇到这个问题 很明显空指针问题 按照提示添加 import android.support.annotation.Nullable;@Nullable 造成原 ...
- [King.yue]Grid列赋值文本,隐藏Value
例:public string InputFormat 加扩展属性:public string InputFormatText 构造函数中根据Key取到Value的值: var data = Data ...
- ModelSim仿真
(1)出现“Fatal: SDF files require Altera primitive library” 书上的方法是在ModelSim-SE下的使用,而我用的是ModelSim-Alter ...
- poj2192(搜索)
这个题目对于两个字符串A,B是否可以通过规则生成C. import java.util.Scanner; public class Main { public static void main(Str ...
- vijosP1447 开关灯泡
vijosP1447 开关灯泡 链接:https://vijos.org/p/1447 [思路] 数学+高精度. 分析题目:题中有言,i时刻将其所有倍数的灯熄灭,由此得知一个数有多少个倍数就会被操作多 ...
- codeforce 660D Number of Parallelograms
题意:询问多少个矩形. 统计横纵坐标差,放进vector中 #include<cstdio> #include<cstring> #include<iostream> ...
- 使用Windows Azure创建Windows系统虚拟机-上
创建虚拟机来运行Windows 本教程介绍了如何轻松创建运行Windows 的 Azure虚拟机(VM),用作来自Azure管理门户中映像图库的Windows 服务器映像.映像图库提供了多种图像,包括 ...
- cmder使用手册
http://bliker.github.io/cmder/ 使其可以在Win+R中运行 将解压出的路径 放入系统变量 path中 如 :D:\software\cmder 解决中文错位 选个喜欢的字 ...
- poj1273--Drainage Ditches(最大流Edmond-Karp算法 邻接表实现)
最大流模板题 大部分Edmond-Karp算法代码都是邻接矩阵实现,试着改成了邻接表. #include <iostream> #include <cstdio> #inclu ...
- [三]JFreeChart实践二
功能: 1.设置带色彩的柱状图 2.可以设置多组数据的展示 3.可以设置图标的背景色 4.可以设置柱与柱之间的距离 5.可以设置柱子上边是否显示具体的数值