列表多选框的选择

//多选接口

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"/>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值