ListView 实现多选/单选

来源:互联网 发布:睡觉前吃水果好吗 知乎 编辑:程序博客网 时间:2024/06/15 06:03
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地址:
    http://download.csdn.net/detail/ljfbest/8109731

0 0
原创粉丝点击