小小菜鸟-----购物车功能

思路浅析:

## 1.先将视图展示出来:
	在Activity里布局一个商家的RecyclerView: business_RecyclerView;
	在business_RecyclerView的布局里嵌套一个商品的RecyclerView: commodity_RecyclerView;
	在commodity_RecyclerView的布局里嵌套一个加减按钮和商品数量的自定义View: 
## 2.进行复选框间的关联:
	全选/全不选---->商家和商品复选框状态改变;
	选中商家------>商家下的复选框全选中状态;
	选中所有商品----->商家选中;
	选中所有商家------->全选/全不选的复选框选中;
	********勾选的商品总价和总数量随之改变************
## 3.加减号按钮的点击改变商品数量
	********商品勾选的情况下,随着按钮改变总价和总数量随之改变************

个人是按商品----商家----购物车的思路解析的代码。上代码:

MVP框架和OKHttp略。。。

详情请查看github…

先从加减号的自定义View开始:

自定义View的XML:

<?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="@mipmap/jian" />

<ImageView
    android:id="@+id/add_car"
    android:layout_width="20dp"
    android:layout_height="20dp"
    android:layout_centerVertical="true"
    android:layout_toRightOf="@+id/edit_shop_car"
    android:src="@mipmap/add" />

<EditText
    android:id="@+id/edit_shop_car"
    android:layout_width="50dp"
    android:layout_height="30dp"
    android:layout_centerVertical="true"
    android:layout_toRightOf="@+id/jian_car"
    android:background="@drawable/home_shop_bg"
    android:gravity="center_horizontal"
    android:inputType="number"
    android:text="1"
    android:textSize="14sp" />
</RelativeLayout>

自定义View类:

package com.bawei.myshopcar.view;

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.bawei.myshopcar.Bean.ShopBean;
import com.bawei.myshopcar.R;
import com.bawei.myshopcar.adapter.ProductsAdapter;

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

/**
 *   自定义View:
 *   加减数量
 */
public class CustomCounterView extends RelativeLayout implements View.OnClickListener {
private EditText editCar;

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

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

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

private Context context;

private void init(Context context) {
    this.context = context;
    //获取展示视图
    View view = View.inflate(context, R.layout.shop_car_price_layout, null);
    //获取资源ID
    ImageView addIamge = (ImageView) view.findViewById(R.id.add_car);
    ImageView jianIamge = (ImageView) view.findViewById(R.id.jian_car);
    editCar = (EditText) view.findViewById(R.id.edit_shop_car);
    //加号、减号的点击事件
    addIamge.setOnClickListener(this);
    jianIamge.setOnClickListener(this);
    addView(view);
    //------------------------------------------------------------------------
    //数量的改变监听
    editCar.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) {
            //TODO:改变数量
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });
}

private int num;

@Override
public void onClick(View view) {
    switch (view.getId()) {
        case R.id.add_car:
            //改变数量,设置数量,改变对象内容,回调,局部刷新
            num++;
            editCar.setText(num + "");
            list.get(position).setNum(num);
            listener.callBack();
            productsAdapter.notifyItemChanged(position);
            break;
        case R.id.jian_car:
            if (num > 1) {
                num--;
            } else {
                toast("我是有底线的啊");
            }
            editCar.setText(num + "");
            list.get(position).setNum(num);
            listener.callBack();
            productsAdapter.notifyItemChanged(position);
            break;
        default:
            break;
    }
}

private void toast(String msg) {
    Toast.makeText(context, msg, Toast.LENGTH_LONG).show();
}

//-------------------------------------------------------------------------------------
//传递的数据
private List<ShopBean.DataBean.ListBean> list = new ArrayList<>();
private int position;
private ProductsAdapter productsAdapter;

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


private CallBackListener listener;

public void setOnCallBack(CallBackListener listener) {
    this.listener = listener;
}

public interface CallBackListener {
    void callBack();
	}
}

商品的RecyclerView的Adapter对应的Xml:

<?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="100dp"
android:background="#ffffff">


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

<ImageView
    android:id="@+id/iv_product"
    android:layout_width="100dp"
    android:layout_height="80dp"
    android:layout_centerVertical="true"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="10dp"
    android:layout_toRightOf="@+id/check_product" />

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="80dp"
    android:layout_centerVertical="true"
    android:layout_toRightOf="@+id/iv_product">

    <TextView
        android:id="@+id/tv_product_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:lines="2"
        android:textColor="#222222"
        android:textSize="14sp" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_alignParentBottom="true">

        <TextView
            android:id="@+id/tv_product_price"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:textColor="@color/colorPrimary"
            android:textSize="12sp" />

        <com.bawei.myshopcar.view.CustomCounterView
            android:id="@+id/custom_product_counter"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/tv_product_price" />
    </RelativeLayout>
</RelativeLayout>

<View
    android:layout_width="match_parent"
    android:layout_height="5dp"
    android:layout_alignParentBottom="true"
    android:background="#cccccc" />
</RelativeLayout>

商品的RecyclerView对应的Adapter:

package com.bawei.myshopcar.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
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.bawei.myshopcar.Bean.ShopBean;
import com.bawei.myshopcar.R;
import com.bawei.myshopcar.view.CustomCounterView;
import com.bumptech.glide.Glide;

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

/**
* 展示商家里的商品
*/
public class ProductsAdapter extends RecyclerView.Adapter<ProductsAdapter.MyViewHolder> {
private Context mContext;
private List<ShopBean.DataBean.ListBean> mList = new ArrayList<>();

/**
 * 商品的有参构造
 * @param context
 * @param list
 */
public ProductsAdapter(Context context, List<ShopBean.DataBean.ListBean> list) {
    this.mContext = context;
    this.mList = list;
}

@NonNull
@Override
public ProductsAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    //              商品的展示视图:复选框、标题、价格、自定义View(加减号、数量)
    View view = View.inflate(mContext, R.layout.shop_car_adapter, null);
    MyViewHolder myViewHolder = new MyViewHolder(view);
    return myViewHolder;
}

@Override
public void onBindViewHolder(@NonNull ProductsAdapter.MyViewHolder myViewHolder, final int i) {
        //商品的图片
    String url = mList.get(i).getImages().split("\\|")[0].replace("https", "http");
    Glide.with(mContext).load(url).into(myViewHolder.mImage);
        //商品的标题
    myViewHolder.mTitle.setText(mList.get(i).getTitle());
        //商品的价钱
    myViewHolder.mPrice.setText(mList.get(i).getPrice() + "");
        //商品的复选框
    //根据我记录的状态,改变勾选
    myViewHolder.mCheckBox.setChecked(mList.get(i).isCheck());
    /**
     * ***************商品的复选框状态监听****************
     *
     */
    //商品的跟商家的有所不同,商品添加了选中改变的监听
    myViewHolder.mCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
            //****1.优先改变自己的状态 (运行一)
            mList.get(i).setCheck(isChecked);
            //回调,目的是告诉activity,有人选中状态被改变
            if (mShopCallBackListener != null) {
                mShopCallBackListener.callBack();
            }
        }
    });

    //------------------------------------------------------------------
    //设置自定义View里的Edit
    myViewHolder.mCustomShopCarPrice.setData(this, mList, i);
    myViewHolder.mCustomShopCarPrice.setOnCallBack(new CustomCounterView.CallBackListener() {
        @Override
        public void callBack() {
            if (mShopCallBackListener != null) {
                mShopCallBackListener.callBack();
            }
        }
    });
}

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

public class MyViewHolder extends RecyclerView.ViewHolder {
    CustomCounterView mCustomShopCarPrice;
    TextView mTitle, mPrice;
    ImageView mImage;
    CheckBox mCheckBox;

    public MyViewHolder(@NonNull View itemView) {
        super(itemView);
        mImage = (ImageView) itemView.findViewById(R.id.iv_product);
        mTitle = (TextView) itemView.findViewById(R.id.tv_product_title);
        mPrice = (TextView) itemView.findViewById(R.id.tv_product_price);
        mCheckBox = (CheckBox) itemView.findViewById(R.id.check_product);
        mCustomShopCarPrice = (CustomCounterView) itemView.findViewById(R.id.custom_product_counter);
    }
}

/**
 * ****************商品里定义个方法:**************************
 * *****************商家的所有商品状态改变**********************
 * 在我们子商品的adapter中,修改子商品的全选和反选
 * @param isSelectAll
 */
public void selectOrRemoveAll(boolean isSelectAll) {
    //循环商品
    for (ShopBean.DataBean.ListBean listBean : mList) {
        listBean.setCheck(isSelectAll);
    }
    //刷新
    notifyDataSetChanged();
}

/**
 * ***************定义接口**************************
 * ***************起过渡作用*************************
 */
private ShopCallBackListener mShopCallBackListener;

public void setListener(ShopCallBackListener listener) {
    this.mShopCallBackListener = listener;
}

public interface ShopCallBackListener {
    void callBack();
   }
}

商家的RecyclerView对应的Adapter的xml:

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

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

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

    <TextView
        android:id="@+id/tv_shop"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_alignRight="@+id/car_circle" />
</LinearLayout>

<android.support.v7.widget.RecyclerView
    android:id="@+id/recycler_shop"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/seller_name" />
</LinearLayout>

商家的RecyclerView对应的Adapter:

package com.bawei.myshopcar.adapter;

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

import com.bawei.myshopcar.Bean.ShopBean;
import com.bawei.myshopcar.R;

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

/**
* 展示商家的适配器
*/
public class ShopAdapter extends RecyclerView.Adapter<ShopAdapter.MyViewHolder> {
private List<ShopBean.DataBean> mList = new ArrayList<>();
private Context mContext;

public ShopAdapter(Context context) {
    this.mContext = context;
}

@NonNull
@Override
public ShopAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        //适配器的布局--复选框、商家名、RecyclerView(展示商家下的商品)
    View view = View.inflate(mContext, R.layout.shop_seller_car_adapter, null);
    MyViewHolder myViewHoler = new MyViewHolder(view);
    return myViewHoler;
}

@Override
public void onBindViewHolder(@NonNull final ShopAdapter.MyViewHolder myViewHolder, final int i) {
    //设置商家的名字
    myViewHolder.mSellerName.setText(mList.get(i).getSellerName());
    //展示商品的RecyclerView的适配器(不可写成全局!!!!!)
    final ProductsAdapter productsAdapter = new ProductsAdapter(mContext, mList.get(i).getList());
    //展示商品的RecyclerView的布局格式
    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
    myViewHolder.mRecyclerView.setLayoutManager(linearLayoutManager);
    myViewHolder.mRecyclerView.setAdapter(productsAdapter);

    myViewHolder.mCheck.setChecked(mList.get(i).isCheck());

    //**************回调商品的接口************************
    //*************商品全部选中,商家选中******************
    productsAdapter.setListener(new ProductsAdapter.ShopCallBackListener() {
        @Override
        public void callBack() {
        				(运行二,运行一在商品的adapter的商品状态监听)
            //从商品适配里回调回来,回给activity,activity计算价格和数量
            if(mShopCallBackListener != null) {
                mShopCallBackListener.callBack(mList);
            }
            //*****1.商品集合
            List<ShopBean.DataBean.ListBean> listBeans = mList.get(i).getList();
            //*****2.创建一个临时的标志位,用来记录现在点击的状态
            boolean isAllChecked = true;
            //*****3.遍历所有的商品
            for (ShopBean.DataBean.ListBean bean : listBeans) {
                if (!bean.isCheck()) {
                    //只要有一个商品未选中,标志位设置成false,并且跳出循环
                    isAllChecked = false;
                    break;
                }
            }

            //*****4.刷新商家的状态
            myViewHolder.mCheck.setChecked(isAllChecked);
            //商品的选中状态
            mList.get(i).setCheck(isAllChecked);
        }
    });
    //*************商家选中,商家下的商品全部选中****************

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

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

public void setList(List<ShopBean.DataBean> list) {
    this.mList = list;
    notifyDataSetChanged();
}

public class MyViewHolder extends RecyclerView.ViewHolder {
    RecyclerView mRecyclerView;
    TextView mSellerName;
    CheckBox mCheck;

    public MyViewHolder(@NonNull View itemView) {
        super(itemView);
        mSellerName = (TextView) itemView.findViewById(R.id.tv_shop);
        mCheck = itemView.findViewById(R.id.check_shop);
        mRecyclerView = (RecyclerView) itemView.findViewById(R.id.recycler_shop);
    }
}

/**
 * *********定义接口,在Activity****************
 */
private ShopCallBackListener mShopCallBackListener;

public void setListener(ShopCallBackListener listener) {
    this.mShopCallBackListener = listener;
}

public interface ShopCallBackListener {
    void callBack(List<ShopBean.DataBean> list);
	}
}

购物车对应的XML:

<?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="match_parent"
android:focusableInTouchMode="true"
android:focusable="true">

<RelativeLayout
    android:id="@+id/layout_top"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:background="@color/colorPrimary">

    <ImageView
        android:id="@+id/iv_back"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:background="@mipmap/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="#ffffff"
        android:textSize="16sp" />

</RelativeLayout>

<!--中间的显示-->
<android.support.v7.widget.RecyclerView
    android:id="@+id/recyclerview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/layout_buttom"
    android:layout_below="@+id/layout_top" />

<!--下面的全选-->
<RelativeLayout
    android:id="@+id/layout_buttom"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_alignParentBottom="true"
    android:background="#ffffff"
    android:paddingLeft="10dp">

    <RelativeLayout
        android:id="@+id/layout_all"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true">

        <CheckBox
            android:id="@+id/iv_cricle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true" />

        <TextView
            android:id="@+id/txt_all"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="5dp"
            android:layout_toRightOf="@+id/iv_cricle"
            android:text="全选/全不选" />
    </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/layout_all"
        android:layout_toLeftOf="@+id/sum_price"
        android:text="合计:0.00"
        android:textColor="#222222"
        android:textSize="16sp" />

    <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_txt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="去结算(0)"
            android:textColor="#ffffff" />
   	 	</RelativeLayout>
	 </RelativeLayout>
</RelativeLayout>

购物车:

package com.bawei.myshopcar.activity;

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

import com.bawei.myshopcar.Apis;
import com.bawei.myshopcar.Bean.ShopBean;
import com.bawei.myshopcar.Constants;
import com.bawei.myshopcar.R;
import com.bawei.myshopcar.adapter.ShopAdapter;
import com.bawei.myshopcar.presenter.IPresenterImpl;

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

public class ShopCarActivity extends AppCompatActivity implements IView, View.OnClickListener {
/**
 * 商家的适配器
 */
private ShopAdapter mShopAdapter;
/**
 * 全选/全不选按钮
  */
private CheckBox mIvCircle;
/**
 * 商家的集合
 */
private List<ShopBean.DataBean> mList = new ArrayList<>();
/**
 * 总价、总数
 */
private TextView mAllPriceTxt, nSumPrice;
/**
 * P层
 */
private IPresenterImpl mIPresenterImpl;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_product_list);
    mIPresenterImpl = new IPresenterImpl(this);
    initView();
    getData();
}

@Override
protected void onDestroy() {
    super.onDestroy();
    mIPresenterImpl.onDetach();
}

/**
 * 获取资源ID
 */
private void initView() {
    mIvCircle = (CheckBox) findViewById(R.id.iv_cricle);
    mAllPriceTxt = (TextView) findViewById(R.id.all_price);
    nSumPrice = (TextView) findViewById(R.id.sum_price_txt);
    mIvCircle.setOnClickListener(this);
    RecyclerView mRecyclerView = (RecyclerView) findViewById(R.id.recyclerview);
    //RecyclerView的布局格式
    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
    linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    mRecyclerView.setLayoutManager(linearLayoutManager);
    //设置适配器
    mShopAdapter = new ShopAdapter(this);
    mRecyclerView.setAdapter(mShopAdapter);
    /**
     * ****************选中全部商家,全选/全不选按钮选中*********************
     *               根据选中的商品数量和所有的商品数量比较判断
     */

    //*****1.回调商家适配器里的接口
    mShopAdapter.setListener(new ShopAdapter.ShopCallBackListener() {
        @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> listAll = list.get(a).getList();
                //*****2.循环商品集合

                for (int i = 0; i < listAll.size(); i++) {
                    //***3.得到所有商品的总数
                    totalNum = totalNum + listAll.get(i).getNum();
                    //****4.如果有商品选中----取选中的状态
                    if (listAll.get(i).isCheck()) {
                        //选中的商品价格
                        totalPrice = totalPrice + (listAll.get(i).getPrice() * listAll.get(i).getNum());
                        //选中商品的数量
                        num = num + listAll.get(i).getNum();
                    }
                }
            }
            //****5.如果选中商品的数量<商品的总数量
            if (num < totalNum) {
                //不是全部选中
                mIvCircle.setChecked(false);
            } else {
                //是全部选中
                mIvCircle.setChecked(true);
            }
            //*****6.将值设置
            mAllPriceTxt.setText("合计:" + totalPrice);
            nSumPrice.setText("去结算(" + num + ")");
        }
    });
}

/**
 * 将地址和Bean类交给P层
 */
 public static final String URL_GET_SHOP_CAR_INFO = "http://www.zhaoapi.cn/product/getCarts";
private void getData() {
    Map<String, String> map = new HashMap<>();
    map.put(“uid”, "71");
    mIPresenterImpl.startRequest(URL_GET_SHOP_CAR_INFO, map, ShopBean.class);
}

/**
 * 通过MVP得到的数据
 * @param data
 */
@Override
public void showResponseData(Object data) {
    if (data instanceof ShopBean) {
        ShopBean shopBean = (ShopBean) data;
        mList = shopBean.getData();
        if (mList != null) {
            mList.remove(0);
            mShopAdapter.setList(mList);
        }
    }
}

@Override
public void showResponseFail(Object data) {

}

@Override
public void onClick(View v) {
    switch (v.getId()) {
        //全选/全不选的点击事件
        case R.id.iv_cricle:
            checkSeller(mIvCircle.isChecked());
            //刷新商家适配器
            mShopAdapter.notifyDataSetChanged();
            break;
        default:

    }
}

/**
 * *******************全选/全不选复选框选中**********************
 *                   1.所有商家的复选框选中
 *                   2.所有的商品复选框选中
 * 修改选中状态,获取价格和数量
 */
private void checkSeller(boolean bool) {
    double totalPrice = 0;
    int num = 0;
    for (int a = 0; a < mList.size(); a++) {
        //****1.遍历商家,改变状态---设置商家状态为全选中
        ShopBean.DataBean dataBean = mList.get(a);
        dataBean.setCheck(bool);
                //得到所有的商品
        List<ShopBean.DataBean.ListBean> listAll = mList.get(a).getList();
        for (int i = 0; i < listAll.size(); i++) {
            //****2.遍历商品,改变状态---设置商家的商品全部选中
            listAll.get(i).setCheck(bool);
            //计算总价
            totalPrice = totalPrice + (listAll.get(i).getPrice() * listAll.get(i).getNum());
            //计算总数量
            num = num + listAll.get(i).getNum();
        }
    }

    if (bool) {
        mAllPriceTxt.setText("合计:" + totalPrice);
        nSumPrice.setText("去结算(" + num + ")");
    } else {
        mAllPriceTxt.setText("合计:0.00");
        nSumPrice.setText("去结算(0)");
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值