Recyclerview+checkbox

布局

单行布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:background="@color/white"
    android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:layout_marginTop="@dimen/margin14"
        android:layout_marginLeft="@dimen/margin14"
        android:orientation="horizontal">
        <TextView
            android:id="@+id/item_addressName"
            style="@style/orderText"

            android:text=""/>
        <TextView
            android:layout_marginLeft="@dimen/margin30"
            android:id="@+id/item_addressPhone"
            style="@style/orderText"
            android:text="1111"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginRight="@dimen/margin14"
        android:layout_marginTop="@dimen/margin10"
        android:layout_marginLeft="@dimen/margin14"
        android:layout_marginBottom="@dimen/margin14"
        android:orientation="horizontal">
        <TextView
            android:id="@+id/item_addressAddress"
            style="@style/orderText"
            android:layout_weight="1"
            android:text=""/>
        <TextView
            android:layout_marginLeft="@dimen/margin30"
            android:id="@+id/item_addressPostcode"
            style="@style/orderText"
            android:text="100000"/>
    </LinearLayout>
    <include layout="@layout/liner1"></include>
    <RelativeLayout
        android:layout_marginBottom="@dimen/margin10"
        android:layout_width="match_parent"
        android:layout_marginTop="@dimen/margin14"
        android:layout_marginLeft="@dimen/margin16"
        android:layout_marginRight="@dimen/margin16"
        android:layout_gravity="center"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <CheckBox
            android:id="@+id/item_shopping_check"
            style="@style/check"
            android:paddingRight="@dimen/margin14"
            android:paddingBottom="@dimen/margin14"
            android:paddingTop="@dimen/margin8"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="设为默认"
            android:textSize="@dimen/margin14"
            android:textColor="@color/black"
            />
        <TextView
            android:layout_toLeftOf="@+id/orderEditor"
            android:id="@+id/orderDelete"
            style="@style/address"
            android:text="删除"
            android:textColor="@color/black"></TextView>
        <TextView
            android:layout_alignParentRight="true"
            android:id="@+id/orderEditor"
            style="@style/address"
            android:layout_marginRight="0dp"
            android:text="编辑"
            android:background="@color/bottomFont"
            android:textColor="@color/white"></TextView>

    </RelativeLayout>
    <include layout="@layout/liner"></include>


</LinearLayout>

主布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/address_recyclerview"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />


</LinearLayout>

实现单选的主要代码

第一种方法:bean里面check来判断

 mCheck.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for (addressInfo info:mDataList){//单选用这个。多选将这删除掉
                    info.setCheck(false);
                }
                mDataList.get(position).setCheck(true);
                notifyDataSetChanged();//记得刷新


            }
        });


        if (data.isCheck()) {
            mCheck.setChecked(true);
        } else {
            mCheck.setChecked(false);
        }

第二种通过Map记录状态实现

 private Map<Integer, Boolean> isCheck = new HashMap<Integer, Boolean>();
//初始化
chSelect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setStates(position,true);
                notifyDataSetChanged();//必须刷新
            }
        });
        /**
         * 设置状态
         */
        if (getStates(position)==null){
            isCheck.put(position,false);
        }
        chSelect.setChecked(isCheck.get(position));


/**
     * 设置默认的状态值
     *
     * @param positon
     */
    public void setStates(int positon, boolean b) {
//        isCheck.put(positon, b);

        for (int i = 0; i < mDataList.size(); i++) {
            isCheck.put(i,false);
        }
        isCheck.put(positon,true);
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值