ListView 实现多选/单选
来源:互联网 发布:睡觉前吃水果好吗 知乎 编辑:程序博客网 时间:2024/06/15 06:03
ListView自身带了单选、多选模式,可通过listview.setChoiceMode来设置:
![](http://img.blog.csdn.net/20141101214628093?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbGpmYmVzdA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
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图:
以下是几个获取/设置 选中条目信息的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地址:
http://download.csdn.net/detail/ljfbest/8109731
0 0
- listview实现单选
- ListView 实现多选/单选
- ListView实现单选和多选
- ListView实现RaidoButton单选
- ListView实现单选(一)
- 实现listview单选效果
- listview+checkbox实现单选
- Listview+RadioButton实现单选
- android listView实现单选
- ListView实现单选功能
- Android ListView实现单选
- 简单实现单选多选(GrideView实现单选,ListView实现多选),横向ListView
- Android ListView 使用checkbox 实现 单选、多选操作
- listview的单选/多选模式的实现
- ListView单选及多选等功能实现
- 【Android】ListView的单选、多选的实现
- ListView + CheckBox实现单选、多选效果
- ListView实现CheckBox的单选与多选
- Linux Terminal Command Reference
- Struts2 XML配置详解
- Cocos2d-x 3.2 学习笔记
- nyoj915
- Android绘制动态文字和图片
- ListView 实现多选/单选
- 最近点对
- 关于国标的行政区划网页设计方案及实现方法
- IOS学习笔记之十二:Dispatch_sync死锁
- AS3933唤醒帧解释
- 【HDU1024】Max Sum Plus Plus (m个不相交连续子列的最大和)
- Dijkstra算法 c语言实现
- MBMS+EMBMS
- SQLSever语句新建用户、对用户授权、用户对用户授权等等