cart

package com.ali.cartdemo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ExpandableListView;
import android.widget.TextView;

import com.google.gson.Gson;

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

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity {
    @BindView(R.id.el_cart)
    ExpandableListView elCart;
    @BindView(R.id.cb_cart_all_select)
    CheckBox cbCartAllSelect;
    @BindView(R.id.tv_cart_total_price)
    TextView tvCartTotalPrice;
    @BindView(R.id.btn_cart_pay)
    Button btnCartPay;
    private MyAdapter myAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initData();
    }

    private void initData() {
        String url = "https://www.zhaoapi.cn/product/getCarts";
        Map<String, String> map = new HashMap<>();
        map.put("uid", "71");
        OkhtttpUtils.getInstance().doPost(url, map, new OkhtttpUtils.OkCallback() {


            @Override
            public void onFailure(Exception e) {

            }

            @Override
            public void onResponse(String json) {
                CartInfo cartInfo = new Gson().fromJson(json, CartInfo.class);
                if ("0".equals(cartInfo.getCode())) {
                    List<CartInfo.DataBean> sellerData = cartInfo.getData();

                    //设置adapter
                    myAdapter = new MyAdapter(sellerData);
                    myAdapter.setOnCartListChangeListener(new MyAdapter.onCartListChangeListener() {
                        @Override
                        public void onSellerCheckedChange(int groupPosition) {
                            //商家被点击
                            boolean currentSellerAllProductSelected = myAdapter.isCurrentSellerAllProductSelected(groupPosition);
                            myAdapter.changeCurrentSellerAllProductsStatus(groupPosition, !currentSellerAllProductSelected);
                            myAdapter.notifyDataSetChanged();
                            //刷新底部数据
                            refreshSelectedAndTotalPriceAndTotalNumber();

                        }

                        @Override
                        public void onProductCheckedChange(int groupPosition, int childPosition) {
                            //点击商品得checkbox
                            myAdapter.changeCurrentProductStatus(groupPosition,childPosition);
                            myAdapter.notifyDataSetChanged();
                            //刷新底部数据
                            refreshSelectedAndTotalPriceAndTotalNumber();

                        }

                        @Override
                        public void onProductNumberChange(int groupPosition, int childPosition, int number) {
                            //当加减被点击
                            myAdapter.changeCurrentProductNumber(groupPosition,childPosition,number);
                            myAdapter.notifyDataSetChanged();
                            //刷新底部数据
                            refreshSelectedAndTotalPriceAndTotalNumber();

                            //联网更新网络上得商品数量
                        }
                    });
                    elCart.setAdapter(myAdapter);

                    //展开二级列表
                    for (int i=0;i<sellerData.size();i++){
                        elCart.expandGroup(i);
                    }
                    //刷新checkbox状态和总价和总数量
                    refreshSelectedAndTotalPriceAndTotalNumber();
                }
            }

        });
    }

    //刷新checkbox状态和总价和总数量
    private void refreshSelectedAndTotalPriceAndTotalNumber() {
        //去判断是否所有得商品都被选中
        boolean allProductsSelected = myAdapter.isAllProductsSelected();
        //设置给全选checkBox
        cbCartAllSelect.setChecked(allProductsSelected);

        //计算总价
        float totalPrice = myAdapter.calculateTotalPrice();
        tvCartTotalPrice.setText("总价 " + totalPrice);

        //计算总数量
        int totalNumber = myAdapter.calculateTotalNumber();
        btnCartPay.setText("去结算(" + totalNumber + ")");


    }

    @OnClick(R.id.cb_cart_all_select)
    public void onViewClicked() {
        //底部全选按钮
        //时候所有得商品都被选中
        boolean allProductsSelected = myAdapter.isAllProductsSelected();
        myAdapter.changeAllProductsStatus(!allProductsSelected);
        myAdapter.notifyDataSetChanged();
        //刷新底部数据
        refreshSelectedAndTotalPriceAndTotalNumber();
    }
}
//***************************************************************************************************************

package com.ali.cartdemo;

import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by mumu on 2018/6/25.
 */

public class MyAdapter extends BaseExpandableListAdapter {


    private List<CartInfo.DataBean> sellerData;

    public MyAdapter(List<CartInfo.DataBean> sellerData) {

        this.sellerData = sellerData;
    }

    @Override
    public int getGroupCount() {
        return sellerData == null ? 0 : sellerData.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return sellerData.get(groupPosition).getList() == null ? 0 : sellerData.get(groupPosition).getList().size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return null;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return null;
    }

    @Override
    public long getGroupId(int groupPosition) {
        return 0;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return 0;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        CartInfo.DataBean dataBean = sellerData.get(groupPosition);
        ParentViewHolder parentViewHolder;
        if (convertView == null) {
            convertView = View.inflate(parent.getContext(), R.layout.item_cart_parent, null);
            parentViewHolder = new ParentViewHolder(convertView);
            convertView.setTag(parentViewHolder);
        } else {
            parentViewHolder = (ParentViewHolder) convertView.getTag();
        }

        //商家名字
        parentViewHolder.sellerNameTv.setText(dataBean.getSellerName());
        //根据商品确定商家得checkbox是否被选中
        boolean currentSellerAllProductSelected = isCurrentSellerAllProductSelected(groupPosition);
        parentViewHolder.sellerCb.setChecked(currentSellerAllProductSelected);
        parentViewHolder.sellerCb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //点击商家得checkBox
                if (onCartListChangeListener != null) {
                    onCartListChangeListener.onSellerCheckedChange(groupPosition);
                }
            }
        });

        return convertView;
    }

    //当前商家所有商品是否被选中
    public boolean isCurrentSellerAllProductSelected(int groupPosition) {
        CartInfo.DataBean dataBean = sellerData.get(groupPosition);
        List<CartInfo.DataBean.ListBean> list = dataBean.getList();
        for (CartInfo.DataBean.ListBean listBean : list) {
            //只要有一个未选中,商家就直接未选中
            if (listBean.getSelected() == 0) {
                return false;
            }
        }
        return true;
    }

    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        CartInfo.DataBean dataBean = sellerData.get(groupPosition);
        List<CartInfo.DataBean.ListBean> list = dataBean.getList();
        //商品
        CartInfo.DataBean.ListBean listBean = list.get(childPosition);


        ChildViewHolder childViewHolder;
        if (convertView == null) {
            convertView = View.inflate(parent.getContext(), R.layout.item_cart_child, null);
            childViewHolder = new ChildViewHolder(convertView);
            convertView.setTag(childViewHolder);
        } else {
            childViewHolder = (ChildViewHolder) convertView.getTag();
        }
        //商品名字
        childViewHolder.productTitleNameTv.setText(listBean.getTitle());
        //商品单价
        childViewHolder.productPriceTv.setText(listBean.getPrice()+"");
        //商品得checkBox状态
        childViewHolder.childCb.setChecked(listBean.getSelected() == 1);
        childViewHolder.childCb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //点击商品得checkBox
                if (onCartListChangeListener != null) {
                    onCartListChangeListener.onProductCheckedChange(groupPosition, childPosition);
                }
            }
        });
        childViewHolder.addRemoveView.setNumber(listBean.getNum());
        childViewHolder.addRemoveView.setOnNumberChangeListener(new MyAddSubView.OnNumberChangeListener() {
            @Override
            public void onNumberChange(int num) {
                //拿到商品最新得数量
                if (onCartListChangeListener != null) {
                    onCartListChangeListener.onProductNumberChange(groupPosition, childPosition, num);
                }
            }
        });


        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }

    //所有商品是否被选中
    public boolean isAllProductsSelected() {
        for (int i = 0; i < sellerData.size(); i++) {
            CartInfo.DataBean dataBean = sellerData.get(i);
            List<CartInfo.DataBean.ListBean> list = dataBean.getList();
            for (int j = 0; j < list.size(); j++) {
                if (list.get(j).getSelected() == 0) {
                    return false;
                }
            }
        }
        return true;
    }

    //计算总价
    public float calculateTotalPrice() {
        float totalPrice = 0;
        for (int i = 0; i < sellerData.size(); i++) {
            CartInfo.DataBean dataBean = sellerData.get(i);
            List<CartInfo.DataBean.ListBean> list = dataBean.getList();
            for (int j = 0; j < list.size(); j++) {
                //只要是选中状态
                if (list.get(j).getSelected() == 1) {
                    float price = list.get(j).getPrice();
                    int num = list.get(j).getNum();
                    totalPrice += price * num;
                }
            }
        }
        return totalPrice;
    }

    public int calculateTotalNumber() {
        int totalNumber = 0;
        for (int i = 0; i < sellerData.size(); i++) {
            CartInfo.DataBean dataBean = sellerData.get(i);
            List<CartInfo.DataBean.ListBean> list = dataBean.getList();
            for (int j = 0; j < list.size(); j++) {
                //只要是选中状态
                if (list.get(j).getSelected() == 1) {
                    int num = list.get(j).getNum();
                    totalNumber += num;
                }
            }
        }
        return totalNumber;
    }


    //当商家得checkbox被点击得时候调用,设置当前商家得所有商品得状态
    public void changeCurrentSellerAllProductsStatus(int groupPosition, boolean isSelected) {
        CartInfo.DataBean dataBean = sellerData.get(groupPosition);
        List<CartInfo.DataBean.ListBean> listBeans = dataBean.getList();
        for (int i = 0; i < listBeans.size(); i++) {
            CartInfo.DataBean.ListBean listBean = listBeans.get(i);
            listBean.setSelected(isSelected ? 1 : 0);
        }
    }

    //当商品得checkbox被点击得时候调用,改变当前商品状态
    public void changeCurrentProductStatus(int groupPosition, int childPosition) {
        CartInfo.DataBean dataBean = sellerData.get(groupPosition);
        List<CartInfo.DataBean.ListBean> listBeans = dataBean.getList();
        CartInfo.DataBean.ListBean listBean = listBeans.get(childPosition);
        listBean.setSelected(listBean.getSelected() == 0 ? 1 : 0);
    }

    //当加减器被点击得时候调用,改变当前商品得数量
    public void changeCurrentProductNumber(int groupPosition, int childPosition, int number) {
        CartInfo.DataBean dataBean = sellerData.get(groupPosition);
        List<CartInfo.DataBean.ListBean> listBeans = dataBean.getList();
        CartInfo.DataBean.ListBean listBean = listBeans.get(childPosition);
        listBean.setNum(number);
    }

    //设置所有商品得状态
    public void changeAllProductsStatus(boolean selected) {
        for (int i = 0; i < sellerData.size(); i++) {
            CartInfo.DataBean dataBean = sellerData.get(i);
            List<CartInfo.DataBean.ListBean> list = dataBean.getList();
            for (int j = 0; j < list.size(); j++) {
                list.get(j).setSelected(selected?1:0);
            }
        }
    }


    class ParentViewHolder {
        @BindView(R.id.seller_cb)
        CheckBox sellerCb;
        @BindView(R.id.seller_name_tv)
        TextView sellerNameTv;

        ParentViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    class ChildViewHolder {
        @BindView(R.id.child_cb)
        CheckBox childCb;
        @BindView(R.id.product_icon_iv)
        ImageView productIconIv;
        @BindView(R.id.product_title_name_tv)
        TextView productTitleNameTv;
        @BindView(R.id.product_price_tv)
        TextView productPriceTv;
        @BindView(R.id.add_remove_view)
        MyAddSubView addRemoveView;

        ChildViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }


    onCartListChangeListener onCartListChangeListener;

    public void setOnCartListChangeListener(MyAdapter.onCartListChangeListener onCartListChangeListener) {
        this.onCartListChangeListener = onCartListChangeListener;
    }

    public interface onCartListChangeListener {
        void onSellerCheckedChange(int groupPosition);

        void onProductCheckedChange(int groupPosition, int childPosition);

        void onProductNumberChange(int groupPosition, int childPosition, int number);
    }
}
 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值