安卓--购物车的全选反选以及价格

public class MainActivity extends AppCompatActivity implements Contract.Contract_View_Interface {


    private RecyclerView rv;
    private CheckBox cb_quan;
    private Button btn_jian;
    private Button btn_jia;
    private TextView tv_shu;
    private int i=1;        //初始化数量1
    private int heji=0;
    private List<ShangPin_Bean.ResultBean.PzshBean.CommodityListBeanX> pzsh_list;
    private zjm.com.yue2.ui.adapter.Adapter adapter;
    private TextView tv_heji;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_main );
        rv = findViewById( R.id.rv );//数据展示
        tv_heji = findViewById( R.id.tv_heji );//合计金额
        tv_heji.setText( "合计:"+heji );
        cb_quan = findViewById( R.id.cb_quan );//全选按钮
        cb_quan.setOnCheckedChangeListener( null );//全选避免焦点枪战
        Subtractor Subtractor = findViewById( R.id.Subtractor );//自定义加减器
        cb_quan();//点击全选
        //得到P层  绑定  请求
        Presenter presenter = new Presenter();
        presenter.attahView(this);
        presenter.requestData();
    }


    //点击全选
    private void cb_quan() {
        cb_quan.setOnCheckedChangeListener( new CompoundButton.OnCheckedChangeListener() {
            private int price;
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                for (int i = 0; i < pzsh_list.size(); i++) {//遍历集合
                    pzsh_list.get( i ).setPzsh_checkbox( isChecked );//设置集合中checkbox的状态
                    //得到价格
                    price = pzsh_list.get( i ).getPrice();
                    heji+= price;//全选时的价钱
                    if (isChecked == false) {//反选
                        heji=0;
                        tv_heji.setText( "合计:"+heji );//赋值总价
                    }
                }
                tv_heji.setText( "合计:"+heji );//赋值总价
                heji=0;
                adapter.notifyDataSetChanged();//刷新适配器
            }
        } );
    }//点击全选



    @Override
    public void showData(final String message) {
        runOnUiThread( new Runnable() {
            @Override
            public void run() {
                //Toast.makeText( MainActivity.this, ""+message, Toast.LENGTH_SHORT ).show();
                //解析数据
                Gson gson = new Gson();
                ShangPin_Bean shangPinBean = gson.fromJson( message, ShangPin_Bean.class );
                //--------------------数据源 品质生活
                pzsh_list = shangPinBean.getResult().getPzsh().getCommodityList();
                //适配器
                LinearLayoutManager layoutManager = new LinearLayoutManager( MainActivity.this, LinearLayoutManager.VERTICAL, false );
                rv.setLayoutManager( layoutManager );
                adapter = new zjm.com.yue2.ui.adapter.Adapter( R.layout.item, pzsh_list );
                rv.setAdapter( adapter );
            }
        } );
    }

}切记在写checkbox的时候一定要避免焦点抢占(代码中有注释)适配器中也是一样的

Adapter
public class Adapter extends BaseQuickAdapter<ShangPin_Bean.ResultBean.PzshBean.CommodityListBeanX,BaseViewHolder> {

public Adapter(int layoutResId, @Nullable List<ShangPin_Bean.ResultBean.PzshBean.CommodityListBeanX> data) {
        super( layoutResId, data );
    }

    @Override
    protected void convert(BaseViewHolder helper, ShangPin_Bean.ResultBean.PzshBean.CommodityListBeanX item) {
        //避免焦点枪战
        CheckBox pzsh_checkbox = helper.getView( R.id.pzsh_checkbox );
        pzsh_checkbox.setOnCheckedChangeListener( null );//避免焦点抢占
        pzsh_checkbox.setChecked( item.getpzsh_checkbox() );//给条目赋值
        helper.setText( R.id.tv_name ,item.getCommodityName());//name
        helper.setText( R.id.tv_price ,"$"+item.getPrice());//价格
        ImageView img_item = helper.getView( R.id.img_item );
        Glide.with( mContext ).load( item.getMasterPic() ).into( img_item );//图片

    }

}

-----------------------------------------二级购物车----------------------------------------=
布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".ui.activity.Main2Activity"
    android:orientation="vertical">

    //显示商家名称
    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1">

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

    //结算栏--全选--总价--结算
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <CheckBox
            android:id="@+id/cb_qx"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:text="全选"/>
        <TextView
            android:id="@+id/tv_price"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_marginLeft="20dp"
            android:layout_gravity="center_vertical"
            android:text="合计:$1242.50"/>
    </LinearLayout>
</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="wrap_content"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        //多选框
        <CheckBox
            android:id="@+id/cb_business"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"/>
        //商家名称
        <TextView
            android:id="@+id/tv_businessname"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="23dp"
            android:textColor="#f00"
            android:background="#ffcd42"
            android:text="肯德基(上地店)"/>
    </LinearLayout>

    //商品展示
    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv_goods"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </android.support.v7.widget.RecyclerView>
</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="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="horizontal">

    //多选框
    <CheckBox
        android:id="@+id/cb_good"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_gravity="center_vertical"/>
    //卡片布局
    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_marginRight="5dp"
        android:layout_marginBottom="5dp"
        app:cardCornerRadius="10dp">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:orientation="horizontal">
            //图片
            <ImageView
                android:id="@+id/img_good"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:src="@mipmap/ic_launcher"/>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginLeft="10dp"
                android:orientation="vertical">
                //商品名称
                <TextView
                    android:id="@+id/tv_goodname"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="15dp"
                    android:text="麦香鸡块"/>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:orientation="horizontal">
                    //评价
                    <TextView
                        android:id="@+id/tv_good_price"
                        android:layout_width="wrap_content"
                        android:layout_height="match_parent"
                        android:layout_weight="1"
                        android:textColor="#f00"
                        android:gravity="center_vertical"
                        android:text="好吃"/>
                    //加减器
                    <zjm.com.yue2.data.Subtractor
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginRight="5dp">

                    </zjm.com.yue2.data.Subtractor>
                </LinearLayout>
            </LinearLayout>
        </LinearLayout>
    </android.support.v7.widget.CardView>
</LinearLayout>

Bean类
public class Cart_Bean {

		private String sellerName;
        private String sellerid;
        private List<ListBean> list;
        private boolean busine_checkbox;//商家的cb

        public boolean isBusine_checkbox() {
            return busine_checkbox;
        }
        public boolean getBusine_checkbox() {
            return busine_checkbox;
        }
        public void setBusine_checkbox(boolean busine_checkbox) {
            this.busine_checkbox = busine_checkbox;
        }

			private int selected;
            private int sellerid;
            private String subhead;
            private String title;
            private boolean good_checkbox;//商品的cb

            public boolean isGood_checkbox() {
                return good_checkbox;
            }
            public boolean getGood_checkbox() {
                return good_checkbox;
            }
            public void setGood_checkbox(boolean good_checkbox) {
                this.good_checkbox = good_checkbox;
            }

}

Main2Activity
public class Main2Activity extends AppCompatActivity implements Contract.Contract_View_Interface {

    private RecyclerView recyclerview;
    private CheckBox cb_qx;
    private List<ERJI_Bean.DataBean> shangjia_list;
    private MyAdapter myAdapter;
    private int price=0;
    private TextView tv_price;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_main2 );
        recyclerview = findViewById( R.id.recyclerview );
        //全选按钮
        cb_qx = findViewById( R.id.cb_qx );
        cb_qx.setOnCheckedChangeListener( null );//避免焦点抢占
        tv_price = findViewById( R.id.tv_price );
        tv_price.setText( "总价:"+price );
        //新建P层
        Presenter presenter = new Presenter();
        presenter.attahView( this );//绑定
        presenter.requestErJiData();//发送到P层

        quanxuan();//全选按钮
    }

    //全选按钮
    private void quanxuan() {
        cb_qx.setOnCheckedChangeListener( new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                //循环遍历集合
                for (int i = 0; i <shangjia_list.size() ; i++) {
                    //一级
                    shangjia_list.get( i ).setchoice_business( isChecked );

                    for (int j = 0; j < shangjia_list.get( i ).getSpus().size(); j++) {
                        shangjia_list.get( i ).getSpus().get( j ).setchoice_good( isChecked );

                        String price1 = shangjia_list.get( i ).getSpus().get( j ).getSkus().get( 0 ).getPrice();
                        double v = Double.parseDouble( price1 );
                        price+=v;
                        if (isChecked==false){//未选中--则为0
                            price=0;
                            tv_price.setText( "总价:"+price );
                        }
                    }
                }
                tv_price.setText( "总价:"+price );
                price=0;
                myAdapter.notifyDataSetChanged();//刷新适配器
            }
        } );
    }//全选按钮


    @Override
    public void showData(String message) {

    }

    @Override
    public void showErJiData(final String message) {
        runOnUiThread( new Runnable() {
            @Override
            public void run() {
                //Toast.makeText( Main2Activity.this, ""+message, Toast.LENGTH_SHORT ).show();
                //解析得到数据源
                Gson gson = new Gson();
                ERJI_Bean erji_bean = gson.fromJson( message, ERJI_Bean.class );
                //商家的list
                shangjia_list = erji_bean.getData();
                //设置适配器
                LinearLayoutManager layoutManager = new LinearLayoutManager( Main2Activity.this, LinearLayoutManager.VERTICAL, false );
                recyclerview.setLayoutManager( layoutManager );
                myAdapter = new MyAdapter( R.layout.rv_item, shangjia_list );
                recyclerview.setAdapter( myAdapter );

              /*  myAdapter.setOnItemClickListener( new BaseQuickAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                        Toast.makeText( Main2Activity.this, "wwww", Toast.LENGTH_SHORT ).show();
                    }
                } );*/
            }
        } );
    }

}

第一层适配器MyAdapter
public class MyAdapter extends BaseQuickAdapter<ERJI_Bean.DataBean,BaseViewHolder> {

    private GoodAdapter goodAdapter;
    private List<ERJI_Bean.DataBean.SpusBean> goods_list;

    public MyAdapter(int layoutResId, @Nullable List<ERJI_Bean.DataBean> data) {
        super( layoutResId, data );
    }

    @Override
    protected void convert(BaseViewHolder helper, final ERJI_Bean.DataBean item) {
        helper.setText( R.id.tv_businessname,item.getName() );//给商家名称赋值
        RecyclerView rv_goods = helper.getView( R.id.rv_goods );
        CheckBox cb_business = helper.getView( R.id.cb_business );//多选框
        cb_business.setOnCheckedChangeListener( null );//避免焦点抢占
        cb_business.setChecked( item.getchoice_business());

        //点击商家多选框
        cb_business.setOnCheckedChangeListener( new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                for (int i = 0; i <item.getSpus().size() ; i++) {
                    //给商品选中
                    item.getSpus().get( i ).setchoice_good( isChecked );
                }
                goodAdapter.notifyDataSetChanged();//刷新适配器
            }
        } );//点击商家多选框

        //商品数据源
        goods_list = item.getSpus();
        //设置适配器
        LinearLayoutManager layoutManager = new LinearLayoutManager( mContext, LinearLayoutManager.VERTICAL, false );
        rv_goods.setLayoutManager( layoutManager );
        goodAdapter = new GoodAdapter( R.layout.rv_good, goods_list );
        rv_goods.setAdapter( goodAdapter );

        //给适配器设置监听--长按删除
       goodAdapter.setOnItemLongClickListener( new OnItemLongClickListener() {
           @Override
           public boolean onItemLongClick(BaseQuickAdapter adapter, View view, int position) {
               goodAdapter.remove( position );
               Toast.makeText( mContext, "删除成功!!!!!!!!", Toast.LENGTH_SHORT ).show();
               return false;
           }
       } );//给适配器设置监听--长按删除
        goodAdapter.notifyDataSetChanged();//刷新适配器
    }

}

第二层适配器GoodAdapter
public class GoodAdapter extends BaseQuickAdapter<ERJI_Bean.DataBean.SpusBean,BaseViewHolder> {

public GoodAdapter(int layoutResId, @Nullable List<ERJI_Bean.DataBean.SpusBean> data) {
        super( layoutResId, data );
    };

    @Override
    protected void convert(BaseViewHolder helper, ERJI_Bean.DataBean.SpusBean item) {
        CheckBox cb_good = helper.getView( R.id.cb_good );
        cb_good.setOnCheckedChangeListener( null );
        cb_good.setChecked( item.getchoice_good() );
        helper.setText( R.id.tv_goodname,item.getName() );//名称
        helper.setText( R.id.tv_good_price,"¥"+item.getSkus().get( 0 ).getPrice());//价格
        ImageView img_good = helper.getView( R.id.img_good );
        Glide.with( mContext ).load( item.getPic_url() ).into( img_good );

    }

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值