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图:
下面是几个获取/设置 选中条目信息的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地址:
版权声明:本文博客原创文章,博客,未经同意,不得转载。