购物车两个Adaper,可以删除,全选反选,数量加减,通过RecycleView嵌套实现

在这里插入图片描述
1activity_main.xml主页面布局

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:focusableInTouchMode="true"
    android:focusable="true"
    tools:context=".activity.MainActivity">
    <RelativeLayout
        android:id="@+id/lauout_top"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@color/design_default_color_primary"
        >
        <ImageView
            android:id="@+id/iv_back"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:background="@drawable/back"
            android:layout_centerVertical="true"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerInParent="true"
            android:text="购物车"
            android:textColor="#fff"
            android:textSize="20sp"
            />
    </RelativeLayout>
<!--中间的显示商家的recycleview-->
<android.support.v7.widget.RecyclerView
    android:id="@+id/recycle_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@id/layput_buttom"
    android:layout_below="@id/lauout_top"
    />
    <!--下面的全选-->
    <RelativeLayout
        android:id="@+id/layput_buttom"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true"
        android:background="#ffffff"
        android:paddingLeft="10dp"
        >
    <RelativeLayout
        android:id="@+id/lauout_all"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        >
    <CheckBox
        android:id="@+id/all_check"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        />
        <TextView
            android:id="@+id/text_all"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:text="全选/反选"
            android:layout_marginLeft="5dp"
            android:layout_toRightOf="@id/all_check"
            />
    </RelativeLayout>
        <TextView
            android:id="@+id/all_price"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="5dp"
            android:layout_toRightOf="@id/lauout_all"
            android:text="合计:0.00"
            android:textColor="#222222"
            android:textSize="20sp"
            />
        <RelativeLayout
            android:id="@+id/sum_price"
            android:layout_width="100dp"
            android:layout_height="match_parent"
            android:layout_alignParentRight="true"
            android:background="@color/colorPrimary"
            >
            <TextView
                android:id="@+id/sum_price_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:text="去结算(0)"
                android:textColor="#ffffff"
                />
        </RelativeLayout>
    </RelativeLayout>
</RelativeLayout>

2自定义加减器布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <ImageView
        android:id="@+id/jian_car"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_centerVertical="true"
        android:src="@drawable/ic_jian_name"
        />
    <EditText
        android:id="@+id/edit_car"
        android:layout_width="50dp"
        android:layout_height="30dp"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@id/jian_car"
        android:background="@drawable/edit_shape"
        android:gravity="center_horizontal"
        android:inputType="number"
        android:text="1"
        android:textSize="14sp"
        />
    <ImageView
        android:id="@+id/add_car"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_centerVertical="true"
        android:src="@drawable/ic_add_name"
        android:layout_toRightOf="@id/edit_car"
        />
</RelativeLayout>

3.展示商家的布局(里面有一个Recycleview是为了展示商品)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="wrap_content">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    >
<CheckBox
    android:id="@+id/mearchant_check"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:layout_gravity="center"
    />
    <TextView
        android:id="@+id/mearchant_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:textSize="20sp"
        android:text="商家"
        />
</LinearLayout>
    <android.support.v7.widget.RecyclerView
        android:id="@+id/mearchant_recycle"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</LinearLayout>

4.展示商品的布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#ffffff">

    <CheckBox
        android:id="@+id/shop_check"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="10dp" />

    <ImageView
        android:id="@+id/shop_image"
        android:layout_width="120dp"
        android:layout_height="100dp"
        android:layout_centerVertical="true"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="10dp"
        android:layout_toRightOf="@id/shop_check" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@id/shop_image">

        <TextView
            android:id="@+id/shop_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:maxLines="2"
            android:text="title"
            android:textColor="#222222"
            android:textSize="20sp" />
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_alignParentBottom="true"
            >
            <TextView
                android:id="@+id/shop_price"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:textColor="#f00"
                android:textSize="20sp"
                android:text="price"
                />
            <com.example.shoppingcart02.custom.CustomView
                android:id="@+id/shop_custom"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@id/shop_price"
                android:layout_centerVertical="true"
                android:layout_marginLeft="15dp"
                />
        </RelativeLayout>
    </RelativeLayout>
</RelativeLayout>

5.用于展示商家的适配器

package com.example.shoppingcart02.adaper;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;

import com.example.shoppingcart02.R;
import com.example.shoppingcart02.bean.ShopBean;

import java.util.ArrayList;
import java.util.List;

public class MearchantAdapaer extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<ShopBean.DataBean> mData;
    private Context mContext;

    public MearchantAdapaer(Context mContext) {
        this.mContext = mContext;
        mData = new ArrayList<>();
    }
    public void setmData(List<ShopBean.DataBean> datas){
        mData.addAll(datas);
        notifyDataSetChanged();
    }
    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.merchant_item,viewGroup,false);
        ViewHolderMearchant holderMearchant = new ViewHolderMearchant(view);
        return holderMearchant;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, final int i) {
        final ViewHolderMearchant holderMearchant = (ViewHolderMearchant) viewHolder;
        //设置商家
        holderMearchant.mearchant_text.setText(mData.get(i).getSellerName());
        final ShopAdaper shopAdaper = new ShopAdaper(mContext,mData.get(i).getList());
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        layoutManager.setOrientation(OrientationHelper.VERTICAL);
        holderMearchant.mearchant_recycle.setLayoutManager(layoutManager);
        holderMearchant.mearchant_recycle.setAdapter(shopAdaper);
        holderMearchant.mearchant_check.setChecked(mData.get(i).isCheck());

        shopAdaper.setShopCallBack(new ShopAdaper.ShopCallBack() {
            @Override
            public void callBack() {
                //从商品里回调回来
                if(mearchantCallBack!=null){
                    mearchantCallBack.callBack(mData);
                }
                List<ShopBean.DataBean.ListBean> list = mData.get(i).getList();
                //创建一个临时标志位
                boolean isAllCheck = true;
                for (ShopBean.DataBean.ListBean bean:list){
                    if(!bean.isCheck()){
                        //只要有一个商品未选中,标志位设置成false,并且跳出循环
                        isAllCheck = false;
                        break;
                    }
                }
                //刷新商家状态
                holderMearchant.mearchant_check.setChecked(isAllCheck);
                mData.get(i).setCheck(isAllCheck);
            }
        });

        //监听checkBox的点击事件
        //目的是改变旗下所有商品的选中状态
        holderMearchant.mearchant_check.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //首先改变自己的标志位
                mData.get(i).setCheck(holderMearchant.mearchant_check.isChecked());
                //调用产品adapter的方法,用来全选和反选
                shopAdaper.selectOrRemoveAll(holderMearchant.mearchant_check.isChecked());
            }
        });
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }
    class ViewHolderMearchant extends RecyclerView.ViewHolder{
            public final CheckBox mearchant_check;
            public final TextView mearchant_text;
            public final RecyclerView mearchant_recycle;
        public ViewHolderMearchant(@NonNull View itemView) {
            super(itemView);
            mearchant_check = itemView.findViewById(R.id.mearchant_check);
            mearchant_text = itemView.findViewById(R.id.mearchant_text);
            mearchant_recycle = itemView.findViewById(R.id.mearchant_recycle);
        }
    }

    private MearchantCallBack mearchantCallBack;
    public void setMearchantCallBack(MearchantCallBack mearchantCallBack){
        this.mearchantCallBack = mearchantCallBack;
    }
    //定义接口
    public interface MearchantCallBack{
        void callBack(List<ShopBean.DataBean> list);
    }
}

6.用于展示商品的适配器

package com.example.shoppingcart02.adaper;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.shoppingcart02.R;
import com.example.shoppingcart02.bean.ShopBean;
import com.example.shoppingcart02.custom.CustomView;
import java.util.List;
/**
 * 商品
 * */
public class ShopAdaper extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context mContext;
    private List<ShopBean.DataBean.ListBean> mList;

    public ShopAdaper(Context mContext, List<ShopBean.DataBean.ListBean> mList) {
        this.mContext = mContext;
        this.mList = mList;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.shop_item,viewGroup,false);
        ViewHolderShop holderShop = new ViewHolderShop(view);
        return holderShop;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, final int i) {
        ViewHolderShop holderShop = (ViewHolderShop) viewHolder;
        String url = mList.get(i).getImages().split("\\|")[0].replace("https","http");
        Glide.with(mContext).load(url).into(holderShop.shop_image);
        holderShop.shop_title.setText(mList.get(i).getTitle());
        holderShop.shop_price.setText("价格:"+mList.get(i).getPrice()+"元");
        //根据我记录的状态,改变勾选
        holderShop.shop_check.setChecked(mList.get(i).isCheck());
        //添加商品的选中的监听
        holderShop.shop_check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                //优选改变自己的状态
                mList.get(i).setCheck(isChecked);
                //回调
                if(shopCallBack!=null){
                    shopCallBack.callBack();
                }
            }
        });
        //设置自定义view里的edit
        holderShop.shop_custom.setData(this,mList,i);
        holderShop.shop_custom.setCustomCallBack(new CustomView.CustomCallBack() {
            @Override
            public void callBack() {
                if(shopCallBack!=null){
                    shopCallBack.callBack();
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class ViewHolderShop extends RecyclerView.ViewHolder{
        public final ImageView shop_image;
        public final TextView shop_title;
        public final TextView shop_price;
        public final CheckBox shop_check;
        public final CustomView shop_custom;
        public ViewHolderShop(@NonNull View itemView) {
            super(itemView);
            shop_check = itemView.findViewById(R.id.shop_check);
            shop_custom = itemView.findViewById(R.id.shop_custom);
            shop_image = itemView.findViewById(R.id.shop_image);
            shop_price = itemView.findViewById(R.id.shop_price);
            shop_title = itemView.findViewById(R.id.shop_title);
        }
    }

    /**
     * 在我们子商品的adapter中,修改子商品的全选和反选
     *
     * @param isSelectAll
     */
    public void selectOrRemoveAll(boolean isSelectAll){
        for(ShopBean.DataBean.ListBean listBean:mList){
            listBean.setCheck(isSelectAll);
        }
        notifyDataSetChanged();
    }
    private ShopCallBack shopCallBack;
    public void setShopCallBack(ShopCallBack shopCallBack){
        this.shopCallBack = shopCallBack;
    }
    //定义接口
    public interface ShopCallBack{
        void callBack();
    }
}

7.自定义加减器的view页面

package com.example.shoppingcart02.custom;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.example.shoppingcart02.adaper.ShopAdaper;
import com.example.shoppingcart02.bean.ShopBean;
import com.example.shoppingcart02.R;

import java.util.ArrayList;
import java.util.List;

/**
 * 加减数量
 */
public class CustomView extends RelativeLayout implements View.OnClickListener {

    private ImageView add;
    private ImageView jian;
    private EditText editText;

    public CustomView(Context context) {
        super(context);
        init(context);
    }

    public CustomView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public CustomView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private Context mContext;

    private void init(Context context) {
        mContext = context;
        View view = View.inflate(mContext, R.layout.custom_item, null);
        add = view.findViewById(R.id.add_car);
        jian = view.findViewById(R.id.jian_car);
        editText = view.findViewById(R.id.edit_car);
        add.setOnClickListener(this);
        jian.setOnClickListener(this);
        addView(view);
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                try {
                   num = Integer.valueOf(s.toString());
                   list.get(position).setNum(num);
                }catch (Exception e){
                    list.get(position).setNum(1);
                }
                if(customCallBack!=null){
                    customCallBack.callBack();
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    private int num;

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.add_car:
                num++;
                editText.setText(num + "");
                list.get(position).setNum(num);
                if(customCallBack!=null){
                    customCallBack.callBack();
                }
                shopAdaper.notifyItemChanged(position);
                break;
            case R.id.jian_car:
                if (num > 1) {
                    num--;
                } else {
                    Toast.makeText(mContext, "不能在减了", Toast.LENGTH_SHORT).show();
                }
                editText.setText(num + "");
                list.get(position).setNum(num);
                if(customCallBack!=null){
                    customCallBack.callBack();
                }
                shopAdaper.notifyItemChanged(position);
                break;
            default:
                break;
        }
    }

    //传递数据
    private List<ShopBean.DataBean.ListBean> list = new ArrayList<>();
    private int position;
    private ShopAdaper shopAdaper;

    public void setData(ShopAdaper shopAdaper, List<ShopBean.DataBean.ListBean> list, int i) {
        this.list = list;
        this.shopAdaper = shopAdaper;
        position = i;
        num = list.get(i).getNum();
        editText.setText(num + "");
    }

    private CustomCallBack customCallBack;

    public void setCustomCallBack(CustomCallBack customCallBack) {
        this.customCallBack = customCallBack;
    }

    //定义接口
    public interface CustomCallBack {
        void callBack();
    }
}

8.bean类(里面写一个boolean类型的值)

package com.example.shoppingcart02.bean;

import java.util.List;

public class ShopBean {


    private String msg;
    private String code;
    private List<DataBean> data;

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public List<DataBean> getData() {
        return data;
    }

    public void setData(List<DataBean> data) {
        this.data = data;
    }

    public static class DataBean {


        private String sellerName;
        private String sellerid;
        private List<ListBean> list;
        private boolean isCheck;

        public boolean isCheck() {
            return isCheck;
        }

        public void setCheck(boolean check) {
            isCheck = check;
        }

        public String getSellerName() {
            return sellerName;
        }

        public void setSellerName(String sellerName) {
            this.sellerName = sellerName;
        }

        public String getSellerid() {
            return sellerid;
        }

        public void setSellerid(String sellerid) {
            this.sellerid = sellerid;
        }

        public List<ListBean> getList() {
            return list;
        }

        public void setList(List<ListBean> list) {
            this.list = list;
        }

        public static class ListBean {

            private double bargainPrice;
            private String createtime;
            private String detailUrl;
            private String images;
            private int num;
            private int pid;
            private double price;
            private int pscid;
            private int selected;
            private int sellerid;
            private String subhead;
            private String title;
            private boolean isCheck;

            public boolean isCheck() {
                return isCheck;
            }

            public void setCheck(boolean check) {
                isCheck = check;
            }

            public double getBargainPrice() {
                return bargainPrice;
            }

            public void setBargainPrice(double bargainPrice) {
                this.bargainPrice = bargainPrice;
            }

            public String getCreatetime() {
                return createtime;
            }

            public void setCreatetime(String createtime) {
                this.createtime = createtime;
            }

            public String getDetailUrl() {
                return detailUrl;
            }

            public void setDetailUrl(String detailUrl) {
                this.detailUrl = detailUrl;
            }

            public String getImages() {
                return images;
            }

            public void setImages(String images) {
                this.images = images;
            }

            public int getNum() {
                return num;
            }

            public void setNum(int num) {
                this.num = num;
            }

            public int getPid() {
                return pid;
            }

            public void setPid(int pid) {
                this.pid = pid;
            }

            public double getPrice() {
                return price;
            }

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

            public int getPscid() {
                return pscid;
            }

            public void setPscid(int pscid) {
                this.pscid = pscid;
            }

            public int getSelected() {
                return selected;
            }

            public void setSelected(int selected) {
                this.selected = selected;
            }

            public int getSellerid() {
                return sellerid;
            }

            public void setSellerid(int sellerid) {
                this.sellerid = sellerid;
            }

            public String getSubhead() {
                return subhead;
            }

            public void setSubhead(String subhead) {
                this.subhead = subhead;
            }

            public String getTitle() {
                return title;
            }

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

9.MainActivity页面

package com.example.shoppingcart02.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;

import com.example.shoppingcart02.R;
import com.example.shoppingcart02.adaper.MearchantAdapaer;
import com.example.shoppingcart02.api.Apis;
import com.example.shoppingcart02.bean.ShopBean;
import com.example.shoppingcart02.presenter.PresenterImpl;
import com.example.shoppingcart02.view.Iview;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity implements Iview,View.OnClickListener {

    private PresenterImpl presenter;
    private List<ShopBean.DataBean> mList;
    private CheckBox all_check;
    private TextView all_price;
    private TextView sum_price_text;
    private RecyclerView recycle_view;
    private MearchantAdapaer adapaer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        presenter = new PresenterImpl(this);
        initView();
        initData();
    }

    private void initData() {
        Map<String,String> map = new HashMap<>();
        map.put("uid","71");
        presenter.startRequest(Apis.URL_SHOPP_CART,map,ShopBean.class);
    }

    private void initView() {
        //获取资源id
        all_check = findViewById(R.id.all_check);
        all_price = findViewById(R.id.all_price);
        sum_price_text = findViewById(R.id.sum_price_text);
        recycle_view = findViewById(R.id.recycle_view);
        all_check.setOnClickListener(this);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(OrientationHelper.VERTICAL);
        recycle_view.setLayoutManager(layoutManager);
        //创建适配器
        adapaer = new MearchantAdapaer(this);
        recycle_view.setAdapter(adapaer);

        adapaer.setMearchantCallBack(new MearchantAdapaer.MearchantCallBack() {
            @Override
            public void callBack(List<ShopBean.DataBean> list) {
                //在这里重新遍历已经改状态后的数据,
                // 这里不能break跳出,因为还需要计算后面点击商品的价格和数目,所以必须跑完整个循环
                double totalPrice = 0;
                //勾选商品的数量,不是该商品购买的数量
                int num = 0;
                //所有商品总数,和上面的数量做比对,如果两者相等,则说明全选
                int totalNum = 0;
                for(int a = 0;a<list.size();a++){
                    //获取商家里商品
                    List<ShopBean.DataBean.ListBean> list1 = list.get(a).getList();
                    for(int i = 0;i<list1.size();i++){
                        totalNum  = totalNum + list1.get(i).getNum();
                        //取选中的状态
                        if(list1.get(i).isCheck()){
                            totalPrice = totalPrice+(list1.get(i).getPrice()*list1.get(i).getNum());
                            num = num + list1.get(i).getNum();
                        }
                    }
                }
                if(num<totalNum){
                    //不是全部选中
                    all_check.setChecked(false);
                }else{
                    //全部选中
                    all_check.setChecked(true);
                }
                all_price.setText("合计:"+totalPrice+"元");
                sum_price_text.setText("去结算("+num+")");
            }
        });
    }

    @Override
    public void requestData(Object o) {
        if(o instanceof ShopBean){
            ShopBean bean = (ShopBean) o;
            if(bean == null){
                Toast.makeText(MainActivity.this,bean.getMsg(),Toast.LENGTH_SHORT).show();
            }else{
               mList = bean.getData();
               if(mList!=null){
                   mList.remove(0);
                   adapaer.setmData(mList);
               }
            }
        }
    }

    @Override
    public void requestFail(Object o) {
        if(o instanceof Exception){
            Exception e = (Exception) o;
            e.printStackTrace();
        }
        Toast.makeText(MainActivity.this,"网络请求错误",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.all_check:
                checkSeller(all_check.isChecked());
                adapaer.notifyDataSetChanged();
                break;
                default:
                    break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.onDetach();
    }
    /**
     * 修改选中状态,获取价格和数量
     */
    private void checkSeller(boolean b){
        double totalPrice = 0;
        int num = 0;
        for(int a = 0;a<mList.size();a++){
            //遍历商家,改变状态
            ShopBean.DataBean dataBean = mList.get(a);
            dataBean.setCheck(b);
            List<ShopBean.DataBean.ListBean> list = mList.get(a).getList();
            for(int i = 0;i<list.size();i++){
                //遍历商品,改变状态
                list.get(i).setCheck(b);
                totalPrice = totalPrice+(list.get(i).getPrice()*list.get(i).getNum());
                num = num + list.get(i).getNum();
            }
        }
        if(b){
            all_price.setText("合计:"+totalPrice+"元");
            sum_price_text.setText("去结算("+num+")");
        }else{
            all_price.setText("合计:0.00元");
            sum_price_text.setText("去结算(0)");
        }
    }
}

详细代码请访问一下链接
https://github.com/guoxinyu1995/yuekao/tree/master/shoppingcart02

猜你喜欢

转载自blog.csdn.net/guoxinyu1207/article/details/85139148
今日推荐