购物车的全选和反选

Bean类
    private String title;
    private String price;
    private boolean isChecked;

    public ShopBean(String title, String price, boolean isChecked) {
        this.title = title;
        this.price = price;
        this.isChecked = isChecked;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getPrice() {
        return price;
    }

    public void setPrice(String price) {
        this.price = price;
    }

    public boolean isChecked() {
        return isChecked;
    }

    public void setChecked(boolean checked) {
        isChecked = checked;
    }

    @Override
    public String toString() {
        return "ShopBean{" +
                "title='" + title + '\'' +
                ", price='" + price + '\'' +
                ", isChecked=" + isChecked +
                '}';
    }
初始化数据  给RecyclerView赋值
    private void initData() {
        list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            list.add(new ShopBean("购物车的商品"+i,"12"+i,false));
        }
        if(list!=null)
        {
            if(adapter==null)
            {
                adapter = new RecyclerViewAdapter(this,list);
                layoutManager = new LinearLayoutManager(this);
                recycler.setLayoutManager(layoutManager);
                recycler.setAdapter(adapter);
            }
        }

    }




找到CheckBox  设置点击事件
final CheckBox cb_box = (CheckBox) findViewById(R.id.cb_box);
        cb_box.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //如果这个按钮选中的话
                if(cb_box.isChecked())
                {
                    //就把列表里的所有按钮状态都改为true
                    for (int i = 0; i <list.size() ; i++) {
                        list.get(i).setChecked(true);
                    }
                    //刷新适配器
                    adapter.notifyDataSetChanged();
                }else{
                    //如果不是true就把列表里的所有按钮状态都改为false
                    for (int i = 0; i <list.size() ; i++) {
                        list.get(i).setChecked(false);
                    }
                    //刷新适配器
                    adapter.notifyDataSetChanged();
                }
            }
        });



主布局 main仅供参考
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="licancan.com.licancan20171019.MainActivity"
    android:weightSum="1">
    <LinearLayout
        android:id="@+id/ll_layout"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:background="@android:color/holo_red_light">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="购物车"
            android:gravity="center"
            android:padding="10dp"
            android:layout_gravity="center"
            android:textColor="@android:color/white"/>
    </LinearLayout>

    <android.support.v7.widget.RecyclerView
        android:layout_below="@+id/ll_layout"
        android:id="@+id/recycler"
        android:layout_width="match_parent"
        android:layout_height="400dp">

    </android.support.v7.widget.RecyclerView>

    <LinearLayout
        android:layout_below="@+id/recycler"
        android:layout_width="match_parent"
        android:layout_height="40dp">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <CheckBox
                android:id="@+id/cb_box"
                android:layout_marginLeft="10dp"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_centerVertical="true"
                android:background="@drawable/select"
                android:button="@null"
                />
            <TextView
                android:id="@+id/sum"
                android:layout_toRightOf="@+id/cb_box"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_marginLeft="20dp"
                android:text="合计:"/>
            <TextView
                android:id="@+id/tv_money"
                android:layout_toRightOf="@+id/sum"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:text="money"/>
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:background="@android:color/holo_red_light"
                android:text="付款"
                android:textColor="@android:color/white"/>
        </RelativeLayout>
    </LinearLayout>
</RelativeLayout>



适配器item条目布局  参考
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <CheckBox
            android:layout_marginLeft="10dp"
            android:id="@+id/cb_item"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_centerVertical="true"
            android:background="@drawable/select"
            android:button="@null"
            />
        <ImageView
            android:id="@+id/img_item"
            android:layout_toRightOf="@+id/cb_item"
            android:layout_marginLeft="30dp"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:src="@drawable/bizhi"/>
        <LinearLayout
            android:layout_marginLeft="20dp"
            android:layout_toRightOf="@+id/img_item"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:orientation="vertical">
            <TextView
                android:id="@+id/tv_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="商品描述"
                android:padding="10dp"/>
            <TextView
                android:id="@+id/tv_price"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="价格"
                android:padding="10dp"/>
        </LinearLayout>
    </RelativeLayout>
</LinearLayout>



CheckBox的选择器
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:drawable="@drawable/xuan"/>
    <item android:drawable="@drawable/wei"/>
</selector>




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值