购物车

cart主页面      activity_cart

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

    <ExpandableListView
        android:id="@+id/cart_expand"
        android:layout_marginTop="25dp"
        android:layout_width="match_parent"
        android:layout_marginBottom="80dp"
        android:layout_height="match_parent"></ExpandableListView>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_height="80dp">
        <CheckBox
            android:id="@+id/cart_quanxuan"
            android:layout_width="wrap_content"
            android:layout_height="79dp" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="79dp"
            android:text="全选" />

        <TextView
            android:id="@+id/cart_he"
            android:layout_width="wrap_content"
            android:layout_height="79dp"
            android:layout_marginLeft="20dp"
            android:layout_weight="1"
            android:text="合计:¥0.0" />

        <Button
            android:id="@+id/btn_buy"
            android:layout_width="100dp"
            android:layout_height="79dp"
            android:layout_marginLeft="170dp"
            android:text="去结算()" />


    </LinearLayout>
</RelativeLayout>

自定义加减器    car_add

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="60sp"
    android:layout_height="30dp"
    android:layout_gravity="center_vertical"
    android:gravity="center_vertical"
    android:layout_marginLeft="15dp"
    >


    <TextView
        android:id="@+id/jian"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="-"
        android:background="#fff"
        android:gravity="center"
        android:layout_weight="1"
        />
    <TextView
        android:id="@+id/product_num"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="1"
        android:layout_weight="1"
        android:background="#fff"
        android:gravity="center"
        />

    <TextView
        android:id="@+id/jia"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="#fff"
        android:text="+"
        />
</LinearLayout>

子布局   cart_child

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

       <CheckBox
           android:id="@+id/child_cb"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content" />
         <com.facebook.drawee.view.SimpleDraweeView
             android:id="@+id/product_img"
             android:layout_width="80dp"
             android:background="@mipmap/ic_launcher"
             android:layout_height="80dp" />
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
             <TextView
                 android:id="@+id/product_title_name_tv"
                 android:layout_width="wrap_content"
                 android:text="商品标题"
                 android:layout_height="wrap_content" />
            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                 <TextView
                     android:id="@+id/child_price"
                     android:text="商品价格"
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content" />
                <Button
                    android:id="@+id/cart_delete"
                    android:text="删除"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
            </LinearLayout>
        <com.example.yuekao_moni.mvp.cart.view.activity.MyAddSub
            android:id="@+id/my_sub_add"
            android:layout_width="wrap_content"
            android:layout_marginLeft="15dp"
            android:layout_height="wrap_content"></com.example.yuekao_moni.mvp.cart.view.activity.MyAddSub>
        </LinearLayout>
</LinearLayout>

父布局  cart_parent

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="60sp"
    android:layout_gravity="center_vertical"
    >


    <CheckBox
        android:id="@+id/seller_cb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        />


    <TextView
        android:id="@+id/seller_name_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:text="文字"
        />


</LinearLayout>

m 层

public class CartModel {
     public Observable<CartBean> getcart(int uid){
         return RetrofitManager.getDefaule().create(MainApi.class).cart(uid);
     }
}

p 层

public class CartPersenter extends BasePresenter<CartView> {
    private CartModel cartModel;
    public CartPersenter(CartView view) {
        super(view);
    }

    @Override
    protected void initModel() {
        cartModel=new CartModel();
    }
    public void cart(int uid){
        cartModel.getcart(uid)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<CartBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(CartBean cartBean) {
                    if(view!=null){
                        view.onSuccess(cartBean);
                    }
                    }

                    @Override
                    public void onError(Throwable e) {
                        if(view!=null){
                            view.onError(e.toString());
                        }
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}

v 层

public interface CartView extends IView{
    void onSuccess(CartBean cartBean);
    void onError(String error);
}

自定义加减器的activity    MyAddSub 

public class MyAddSub extends LinearLayout {
    @BindView(R.id.jian)
    TextView jian;
    @BindView(R.id.product_num)
    TextView productNum;
    @BindView(R.id.jia)
    TextView jia;
    int num = 1;


    public MyAddSub(Context context) {
        this(context, null);
    }

    public MyAddSub(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        View view = inflate(context, R.layout.cart_add, this);

        //初始化
        ButterKnife.bind(view);
    }

    @OnClick({R.id.jian, R.id.product_num, R.id.jia})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.jian:
                if(num>1){
                    --num;
                    productNum.setText(num+"");
                    if(onNumberChangeListener!=null){
                        onNumberChangeListener.onSubNumberChange(num);

                    }
                }
                else{
                    Toast.makeText(getContext(),"数量不能小于1",Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.jia:
                ++num;
                productNum.setText(num+"");
                if(onNumberChangeListener!=null){
                    onNumberChangeListener.onAddNumberChange(num);
                }
                break;
        }
    }
    public int getNum() {
        return num;
    }

    public void setNumr(int num) {
        this.num = num;
        productNum.setText(num + "");
    }
    private onNumberChangeListener onNumberChangeListener;

    public void setOnNumberChangeListener(MyAddSub.onNumberChangeListener onNumberChangeListener) {
        this.onNumberChangeListener = onNumberChangeListener;
    }

    public interface onNumberChangeListener {
        void onAddNumberChange(int product_num);
        void onSubNumberChange(int product_num);
    }
}

购物车适配器   ExpandViewAdapter

public class ExpandViewAdapter extends BaseExpandableListAdapter {
    private List<CartBean.DataBean> seller;

    public ExpandViewAdapter(List<CartBean.DataBean> seller) {
        this.seller = seller;
    }

    @Override
    public int getGroupCount() {
        return seller.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return seller.get(groupPosition).getList().size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return seller.get(groupPosition);
    }

    @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) {
        CartBean.DataBean dataBean = seller.get(groupPosition);
        ParentHolder parentHolder;
        if (convertView == null) {
            convertView = View.inflate(parent.getContext(), R.layout.cart_parent, null);
            parentHolder = new ParentHolder(convertView);
            convertView.setTag(parentHolder);
        } else {
            parentHolder = (ParentHolder) convertView.getTag();
        }
        //商家姓名
        parentHolder.sellerNameTv.setText(dataBean.getSellerName());
        boolean b = productStatus(groupPosition);
        parentHolder.sellerCb.setChecked(b);
        parentHolder.sellerCb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onCartListChangeListener != null) {
                    onCartListChangeListener.SellerCheckChange(groupPosition);
                }
            }
        });
        return convertView;
    }

    //子类
    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, final ViewGroup parent) {
        CartBean.DataBean.ListBean listBean = seller.get(groupPosition).getList().get(childPosition);
        ChildHolder childHolder;
        if (convertView == null) {
            convertView = View.inflate(parent.getContext(), R.layout.cart_child, null);

            childHolder = new ChildHolder(convertView);
            convertView.setTag(childHolder);
        } else {
            childHolder = (ChildHolder) convertView.getTag();
        }
        //赋值子类名称
        childHolder.productTitleNameTv.setText(listBean.getTitle());

        childHolder.childPrice.setText(listBean.getBargainPrice() + "");
        String[] pic = listBean.getImages().split("\\|");
        Uri uri = Uri.parse(pic[0]);
        childHolder.productImg.setImageURI(uri);
        boolean flag = listBean.getSelected() == 1 ? true : false;
        childHolder.childCb.setChecked(flag);
        childHolder.childCb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (onCartListChangeListener != null) {
                    onCartListChangeListener.onProductCheckedChange(groupPosition, childPosition);
                    Toast.makeText(parent.getContext(), "kkkkk", Toast.LENGTH_SHORT).show();
                }
            }
        });
        childHolder.mySubAdd.setNumr(listBean.getNum());
        childHolder.mySubAdd.setOnNumberChangeListener(new MyAddSub.onNumberChangeListener() {
            @Override
            public void onAddNumberChange(int product_num) {
                if (onCartListChangeListener != null) {
                    onCartListChangeListener.onProductNumberChange(groupPosition, childPosition, product_num);
                }
            }

            @Override
            public void onSubNumberChange(int product_num) {
                if (onCartListChangeListener != null) {
                    onCartListChangeListener.onProductNumberChange(groupPosition, childPosition, product_num);
                }
            }

        });
        return convertView;
    }

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

    //关于商家按钮
    public boolean productStatus(int groupPosition) {

        List<CartBean.DataBean.ListBean> list = seller.get(groupPosition).getList();
        for (CartBean.DataBean.ListBean listBean : list) {
            if (listBean.getSelected() == 0) {
                return false;
            }
        }
        return true;
    }

    //子类按钮跟着改变
    public void noProductStatus(int groupPosition, boolean b) {

        List<CartBean.DataBean.ListBean> list = seller.get(groupPosition).getList();
        for (int i = 0; i < list.size(); i++) {
            list.get(i).setSelected(b ? 1 : 0);
        }
    }

    //判断所有商品是否选中
    public boolean isAllProductSelected() {
        for (int i = 0; i < seller.size(); i++) {
            List<CartBean.DataBean.ListBean> list = seller.get(i).getList();
            for (int j = 0; j < list.size(); j++) {
                if (list.get(j).getSelected() == 0) {
                    return false;
                }
            }
        }
        return true;
    }

    //关于底部全选按钮
    public void changeAllProductStatus(boolean selected) {

        for (int i = 0; i < seller.size(); i++) {
            List<CartBean.DataBean.ListBean> list = seller.get(i).getList();
            for (int j = 0; j < list.size(); j++) {
                list.get(j).setSelected(selected ? 1 : 0);
            }
        }
    }

    //关于子类按钮
    public void changeCurrentProductStatus(int groupPosition, int childPosition) {

        List<CartBean.DataBean.ListBean> list = seller.get(groupPosition).getList();
        CartBean.DataBean.ListBean listBean = list.get(childPosition);
        listBean.setSelected(listBean.getSelected() == 0 ? 1 : 0);

    }

    //总价格
    public float calcuteTotalPrice() {
        float totlaPrice = 0;
        for (int i = 0; i < seller.size(); i++) {
            List<CartBean.DataBean.ListBean> list = seller.get(i).getList();
            for (int j = 0; j < list.size(); j++) {
                if (list.get(j).getSelected() == 1) {
                    float price = (float) list.get(j).getBargainPrice();
                    int num = list.get(j).getNum();
                    totlaPrice += num * price;
                }

            }
        }
        return totlaPrice;
    }

    //总数量
    public int cacuteTotalNum() {
        int totalNum = 0;
        for (int i = 0; i < seller.size(); i++) {
            List<CartBean.DataBean.ListBean> list = seller.get(i).getList();
            for (int j = 0; j < list.size(); j++) {
                if (list.get(j).getSelected() == 1) {
                    int num = list.get(j).getNum();
                    totalNum += num;
                }

            }
        }
        return totalNum;
    }

    //设置加减按钮
    public void changeCurrentNumberProduct(int groupPosition, int childPosition, int number) {
        List<CartBean.DataBean.ListBean> list = seller.get(groupPosition).getList();

        CartBean.DataBean.ListBean listBean = list.get(childPosition);

        listBean.setNum(number);
    }

    //父类   资源id
    static class ParentHolder {
        @BindView(R.id.seller_cb)
        CheckBox sellerCb;
        @BindView(R.id.seller_name_tv)
        TextView sellerNameTv;

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

    //子类
    onCartListChangeListener onCartListChangeListener;

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

    //接口回调
    public interface onCartListChangeListener {
        void SellerCheckChange(int groupPosition);

        void onProductCheckedChange(int groupPosition, int childPosition);

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

        void onRemoveCartListener(int groupPosition, int childPosition);
    }

    private onClickListener onClickListener;

    public void setOnClickListener(ExpandViewAdapter.onClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    public interface onClickListener {
        void onClick(View view, int position);
    }

    static class ChildHolder {
        @BindView(R.id.child_cb)
        CheckBox childCb;
        @BindView(R.id.product_img)
        SimpleDraweeView productImg;
        @BindView(R.id.product_title_name_tv)
        TextView productTitleNameTv;
        @BindView(R.id.child_price)
        TextView childPrice;
        @BindView(R.id.cart_delete)
        Button cartDelete;
        @BindView(R.id.my_sub_add)
        MyAddSub mySubAdd;

        ChildHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
CartFragment
public class CartFragment extends BaseFragment<CartPersenter> implements CartView,View.OnClickListener{
    private ExpandableListView cart_expand;
    private CheckBox cart_quanxuan;
    private TextView cart_he;
    private Button btn_buy;
    private ExpandViewAdapter adapter;
//    private Button delete;
    private float totalPrice;
    private int uid;
    @Override
    public Context context() {
        return getContext();
    }

    @Override
    public void onSuccess(CartBean cartBean) {
        final List<CartBean.DataBean> data = cartBean.getData();
        Log.e("tag", "onSuccess----------: " + data.size());
        //创建适配器
        adapter = new ExpandViewAdapter(data);
        cart_expand.setAdapter(adapter);
        reFreshSelectedAndToTalPriceAndTotalAllNumber();
        adapter.setOnCartListChangeListener(new ExpandViewAdapter.onCartListChangeListener() {
            @Override
            public void SellerCheckChange(int groupPosition) {
                //设置商家
                boolean b = adapter.productStatus(groupPosition);
                //子类按钮跟着改变
                adapter.noProductStatus(groupPosition, !b);
                //刷新适配器
                adapter.notifyDataSetChanged();
                reFreshSelectedAndToTalPriceAndTotalAllNumber();
            }
            @Override
            public void onProductCheckedChange(int groupPosition, int childPosition) {
                adapter.changeCurrentProductStatus(groupPosition, childPosition);
                //刷新适配器
                adapter.notifyDataSetChanged();
                reFreshSelectedAndToTalPriceAndTotalAllNumber();
            }
            @Override
            public void onProductNumberChange(int groupPosition, int childPosition, int number) {
                //设置加减按钮
                adapter.changeCurrentNumberProduct(groupPosition, childPosition, number);
                //刷新适配器
                adapter.notifyDataSetChanged();
                reFreshSelectedAndToTalPriceAndTotalAllNumber();
            }

            @Override
            public void onRemoveCartListener(int groupPosition, int childPosition) {

            }
        });
        //展开二级列表
        for (int i = 0; i < data.size(); i++) {
            cart_expand.expandGroup(i);
        }
    }

    @Override
    public void onError(String error) {

    }

    @Override
    protected void initViews(View view) {
        cart_expand= view.findViewById(R.id.cart_expand);
        cart_he=  view.findViewById(R.id.cart_he);
        cart_quanxuan= view.findViewById(R.id.cart_quanxuan);
        btn_buy= view.findViewById(R.id.btn_buy);


    }

    @Override
    protected void initData() {

        presenter.cart(14789);
    }

    @Override
    protected void initListener() {

    }

    @Override
    protected CartPersenter providePresenter() {
        return new CartPersenter(this);
    }

    @Override
    protected int privideLayoutId() {
        return R.layout.activity_cart;
    }
    public void reFreshSelectedAndToTalPriceAndTotalAllNumber() {
        //判断是否全部选中
        boolean allProductSelected = adapter.isAllProductSelected();
        cart_quanxuan.setChecked(allProductSelected);

        //设置总价格
        totalPrice = adapter.calcuteTotalPrice();
        cart_he.setText("总价格(" + totalPrice + ")");
        //总数量
        int totalNum = adapter.cacuteTotalNum();
        btn_buy.setText("总数量" + totalNum);
    }

    @Override
    public void onClick(View v) {
        //底部全选按钮
        boolean allProductSelected = adapter.isAllProductSelected();
        adapter.changeAllProductStatus(!allProductSelected);
        //刷新适配器
        adapter.notifyDataSetChanged();
        reFreshSelectedAndToTalPriceAndTotalAllNumber();

    }
}

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值