//多选接口
public interface Checkable { void setChecked(boolean var1); boolean isChecked(); void toggle(); }//自定义checkbox
public class CheckableSecondLayout extends RelativeLayout implements Checkable { private static final String TAG = CheckableLastLayout.class.getSimpleName(); private Checkable child; public CheckableSecondLayout(Context context) { this(context, null); } public CheckableSecondLayout(Context context, AttributeSet attrs) { this(context, attrs, 0); } public CheckableSecondLayout(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override public void setChecked(boolean checked) { if (child == null) { throw new NullPointerException("child shouldn't be null"); } child.setChecked(checked); // this.refreshDrawableState(); } @Override protected void onFinishInflate() { super.onFinishInflate();
RelativeLayout pLayout = (RelativeLayout) getChildAt(0);//第一个控件 组名//此处需要根据item布局文件来写 child = (Checkable)pLayout.getChildAt(1); } @Override public boolean isChecked() { if (child == null) { throw new NullPointerException("child shouldn't be null"); } return child.isChecked(); } @Override public void toggle() { if (child == null) { throw new NullPointerException("child shouldn't be null"); } child.toggle(); // this.refreshDrawableState(); } }//listview item的布局
<?xml version="1.0" encoding="utf-8"?> <com.meetrend.haopingdian.widget.CheckableSecondLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@android:color/white" android:orientation="horizontal" > <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingRight="18dip" > <TextView android:id="@+id/tv_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:ellipsize="end" android:padding="@dimen/margin_18" android:singleLine="true" android:textColor="#111111" android:textSize="@dimen/txt_18" /> <com.meetrend.haopingdian.widget.CheckableImageView android:id="@+id/contact_radio" android:layout_width="20dip" android:layout_height="20dip" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_gravity="center_vertical" android:layout_marginRight="10dip" android:src="@drawable/checkview_bg" /> <TextView android:id="@+id/tv_status" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_toLeftOf="@id/contact_radio" android:ellipsize="end" android:layout_alignParentRight="true" android:singleLine="true" android:text="已添加" android:textColor="@color/gray_1" android:textSize="16dip" android:visibility="gone"/> </RelativeLayout> </com.meetrend.haopingdian.widget.CheckableSecondLayout>//获取勾选的项
//获取所有选中项 public List<People> getCheckMemberList() { List<People> templist = new ArrayList<People>(); SparseBooleanArray checkedItems = mListView.getCheckedItemPositions(); for (int i = 0; i < checkedItems.size(); i++) { if (checkedItems.valueAt(i)) { int index = checkedItems.keyAt(i); People item = resultList.get(index); templist.add(item); } } return templist; }
//listview需要设置为多选模式
<ListView android:id="@+id/lv_contact_phone" android:layout_width="match_parent" android:layout_height="match_parent" android:cacheColorHint="@null" android:choiceMode="multipleChoice" android:divider="@color/listview_line_color" android:dividerHeight="1.2px" android:fastScrollEnabled="false"/>