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);
需要对每个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