二级购物车代码逻辑分享

父布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="horizontal"
    android:layout_height="match_parent">

    <CheckBox
        android:layout_marginLeft="7dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:focusable="false"
        android:gravity="center"
        android:id="@+id/father_check"
        />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/father_name"
        android:textSize="22dp"
        android:gravity="center_vertical"
        />

</LinearLayout>

子布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="horizontal"
    android:layout_height="match_parent">

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_marginLeft="25dp"
        android:gravity="center"
        android:focusable="false"
        android:id="@+id/child_check"
        android:layout_height="70dp" />
    <ImageView
        android:layout_width="85dp"
        android:layout_height="85dp"
        android:id="@+id/cart_iv"
        />
    
    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:layout_height="85dp"
        >
        <TextView
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:textSize="16dp"
            android:id="@+id/cart_title"
            />

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            >
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="¥"
                android:textColor="#ff3600"
                android:textSize="12dp"
                android:id="@+id/qian"
                android:layout_alignBottom="@+id/care_money"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="#ff3600"
                android:textSize="15dp"
                android:id="@+id/care_money"
                android:layout_alignParentTop="true"
                android:layout_toRightOf="@+id/qian"
                android:layout_toEndOf="@+id/qian" />

        </RelativeLayout>
    </LinearLayout>

</LinearLayout>
主布局

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

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_alignParentTop="true"
        android:id="@+id/shop_rela"
        >

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="购物车"
            android:textSize="25dp"
            android:gravity="center"
            />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="编辑"
            android:gravity="center_vertical"
            android:layout_marginRight="15dp"
            android:layout_alignParentRight="true"
            android:textSize="18dp"
            android:id="@+id/shop_updataAll"
            />

    </RelativeLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_above="@+id/shop_showupdata"
        android:layout_below="@+id/shop_rela">

        <View
            android:layout_width="match_parent"
            android:layout_height="3dp"
            android:background="#050505"
            ></View>

        <ExpandableListView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/expandlist"
            ></ExpandableListView>

    </LinearLayout>


    <LinearLayout
        android:background="#f9f8f8"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal"
        android:id="@+id/shop_showupdata"
        >
        <CheckBox
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="3"
            android:text="全选"
            android:id="@+id/delete_checkall"
            />

        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="3"
            android:text=""
            android:textSize="5dp"
            />
        <Button
            android:layout_width="0dp"
            android:layout_weight="3"
            android:layout_height="match_parent"
            android:text="分享"
            android:textSize="12dp"
            android:gravity="center"
            />

        <Button
            android:layout_width="0dp"
            android:layout_weight="3"
            android:layout_height="match_parent"
            android:text="移入关注"
            android:textSize="12dp"
            android:gravity="center_vertical"
            android:id="@+id/delete_all"
            />

        <Button
            android:layout_width="0dp"
            android:layout_weight="3"
            android:layout_height="match_parent"
            android:text="删除"
            android:textSize="12dp"
            android:background="#ff3600"
            android:id="@+id/delete_shopcart"
            />
    </LinearLayout>

    <LinearLayout
        android:background="#f9f8f8"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal"
        android:id="@+id/shop_showmoney"
        >
        <CheckBox
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="3"
            android:text="全选"
            android:textSize="12dp"
            android:id="@+id/shop_checkAll"
            />

        <TextView
            android:layout_width="0dp"
            android:layout_weight="2"
            android:layout_height="match_parent"
            android:text="总价:"
            android:gravity="center"
            />

        <TextView
            android:layout_width="0dp"
            android:layout_weight="4"
            android:layout_height="match_parent"
            android:textSize="16dp"
            android:gravity="center_vertical"
            android:textColor="#ff3600"
            android:id="@+id/shop_moneyall"
            />

        <Button
            android:layout_width="0dp"
            android:layout_weight="3"
            android:layout_height="match_parent"
            android:text="去结算"
            android:textSize="12dp"
            android:background="#ff3600"
            android:id="@+id/suanqian"
            />
    </LinearLayout>

</RelativeLayout>
主代码

package com.example.gouwuche.View;

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

import com.example.gouwuche.Adapters.ExpandableAdapter;
import com.example.gouwuche.Beans.CartsBean;
import com.example.gouwuche.Beans.ChilderBean;
import com.example.gouwuche.Beans.FatherBean;
import com.example.gouwuche.Presenter.HttpPresenter;
import com.example.gouwuche.R;

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

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

    private List<FatherBean> fatherlist= new ArrayList<>();
    private List<List<ChilderBean>> childerlist= new ArrayList<>();
    private TextView updataAll;
    private LinearLayout showmoney;
    private LinearLayout showupdata;
    private Boolean updata=true;   //判断下方切换的值
    private ExpandableListView expandableListView;
    public CheckBox CheckAll;
    private TextView moneyall;
    private CheckBox del_CheckAll;
    private Button del_cart;
    private Button suanqian;
    private ExpandableAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //初始化组件
        updataAll = (TextView) findViewById(R.id.shop_updataAll);
        showmoney = (LinearLayout) findViewById(R.id.shop_showmoney);
        showupdata = (LinearLayout) findViewById(R.id.shop_showupdata);
        moneyall = (TextView) findViewById(R.id.shop_moneyall);

        //全选
        CheckAll = (CheckBox) findViewById(R.id.shop_checkAll);


        //删除的全选
        del_CheckAll = (CheckBox) findViewById(R.id.delete_checkall);

        del_cart= (Button) findViewById(R.id.delete_shopcart);

        //二级列表
        expandableListView = (ExpandableListView) findViewById(R.id.expandlist);

        suanqian = (Button) findViewById(R.id.suanqian);

        //点击事件
        updataAll.setOnClickListener(this);
        CheckAll.setOnClickListener(this);
        del_CheckAll.setOnClickListener(this);
        del_cart.setOnClickListener(this);
        suanqian.setOnClickListener(this);

        HttpPresenter presenter = new HttpPresenter(this);
        HashMap<String, String> map = new HashMap<>();
        map.put("uid","100");
        presenter.getMap(map,"购物车");
    }

    //数据请求成功
    @Override
    public void onSuccess(Object o, String tag) {
        if(tag.equals("购物车")){
            CartsBean cartsBean = (CartsBean) o;

            List<CartsBean.DataBean> data = cartsBean.getData();
            for(int i=0;i<data.size();i++){
                fatherlist.add(new FatherBean(false,data.get(i).getSellerName()));

                List<CartsBean.DataBean.ListBean> list = data.get(i).getList();
                List<ChilderBean> ls=new ArrayList<>();
                for(int x=0;x<list.size();x++){
                    String[] split = list.get(x).getImages().toString().split("\\|");
                    ChilderBean childerBean = new ChilderBean(false, list.get(x).getTitle(), split[0], 1, list.get(x).getBargainPrice());
                    ls.add(childerBean);
                }
                childerlist.add(ls);
            }

            //二级列表
            adapter = new ExpandableAdapter(fatherlist, childerlist, MainActivity.this);
            expandableListView.setAdapter(adapter);
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            //编辑的判断
            case R.id.shop_updataAll:
                if (updata) {
                    showmoney.setVisibility(View.INVISIBLE);
                    showupdata.setVisibility(View.VISIBLE);
                    updata = false;

                    //check所有的改为false
                    for (int i = 0; i < fatherlist.size(); i++) {
                        fatherlist.get(i).setCheck(false);
                        List<ChilderBean> childerBeen = childerlist.get(i);
                        for (int x = 0; x < childerBeen.size(); x++) {
                            childerBeen.get(x).setCheck(false);
                        }
                    }
                    CheckAll.setChecked(false);
                    del_CheckAll.setChecked(false);
                    moneyall.setText("0");
                    adapter.notifyDataSetChanged();

                } else {
                    showmoney.setVisibility(View.VISIBLE);
                    showupdata.setVisibility(View.INVISIBLE);
                    updata = true;

                    del_CheckAll.setChecked(false);

                    //适配器刷新
                    for (int i = 0; i < fatherlist.size(); i++) {
                        fatherlist.get(i).setCheck(false);
                        List<ChilderBean> childBeen = childerlist.get(i);
                        for (int x = 0; x < childBeen.size(); x++) {
                            childBeen.get(x).setCheck(false);
                        }
                    }
                    moneyall.setText("0");
                    adapter.notifyDataSetChanged();

                }

                break;


            //全选
            case R.id.shop_checkAll:

                boolean checked = CheckAll.isChecked();
                if (checked == true) {
                    for (int i = 0; i < fatherlist.size(); i++) {
                        fatherlist.get(i).setCheck(true);
                        List<ChilderBean> childBeen = childerlist.get(i);
                        for (int x = 0; x < childBeen.size(); x++) {
                            childBeen.get(x).setCheck(true);
                        }
                    }
                    adapter.notifyDataSetChanged();

                    Double money = 0.0;

                    for (int i = 0; i < childerlist.size(); i++) {
                        List<ChilderBean> childBeen = childerlist.get(i);
                        for (int x = 0; x < childBeen.size(); x++) {
                            Boolean childCheck = childBeen.get(x).getCheck();
                            if (childCheck) {
                                Double price = childBeen.get(x).getMoney();
                                int num = childBeen.get(x).getNum();
                                money = money + price * num;
                            }
                        }
                    }
                    moneyall.setText(money + "");
                } else if (checked == false) {
                    for (int i = 0; i < fatherlist.size(); i++) {
                        fatherlist.get(i).setCheck(false);
                        List<ChilderBean> childBeen = childerlist.get(i);
                        for (int x = 0; x < childBeen.size(); x++) {
                            childBeen.get(x).setCheck(false);
                        }
                    }
                    adapter.notifyDataSetChanged();
                    moneyall.setText(0 + "");
                }

                break;

            //删除的全选
            case R.id.delete_checkall:
                boolean checked1 = del_CheckAll.isChecked();
                if (checked1) {
                    for (int i = 0; i < fatherlist.size(); i++) {
                        fatherlist.get(i).setCheck(true);
                        List<ChilderBean> childBeen = childerlist.get(i);
                        for (int x = 0; x < childBeen.size(); x++) {
                            childBeen.get(x).setCheck(true);
                        }
                    }
                    adapter.notifyDataSetChanged();

                } else {
                    for (int i = 0; i < fatherlist.size(); i++) {
                        fatherlist.get(i).setCheck(false);
                        List<ChilderBean> childBeen = childerlist.get(i);
                        for (int x = 0; x < childBeen.size(); x++) {
                            childBeen.get(x).setCheck(false);
                        }
                    }
                    adapter.notifyDataSetChanged();
                }
                break;

            //删除的点击事件
            case R.id.delete_shopcart:

                //删除
                for (int i = 0; i < childerlist.size(); i++) {
                    List<ChilderBean> childBeen = childerlist.get(i);
                    for (int x = 0; x < childBeen.size(); x++) {
                        Boolean childCheck = childBeen.get(x).getCheck();
                        if (childCheck) {
//                            HashMap<String, String> map = new HashMap<>();
//                            map.put("uid", userID);
//                            map.put("pid", childBeen.get(x).getPid() + "");
//                            okHttpPresenter.post("http://120.27.23.105/product/deleteCart", map, "删除", AddShopBean.class);

                    }
                    }
                }
//                secondAdapter.notifyDataSetChanged();
                break;
    }
    }
}
适配器代码

package com.example.gouwuche.Adapters;

import android.content.Context;
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 com.bumptech.glide.Glide;
import com.example.gouwuche.Beans.ChilderBean;
import com.example.gouwuche.Beans.FatherBean;
import com.example.gouwuche.R;
import com.example.gouwuche.View.MainActivity;

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

/**
 * Created by xsj on 2017/12/16.
 */

public class ExpandableAdapter extends BaseExpandableListAdapter {

    private List<FatherBean> fatherlist;
    private List<List<ChilderBean>> childerlist;
    private Context context;

    public ExpandableAdapter(List<FatherBean> fatherlist, List<List<ChilderBean>> childerlist, Context context) {
        this.fatherlist = fatherlist;
        this.childerlist = childerlist;
        this.context = context;
    }

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

    @Override
    public int getChildrenCount(int i) {
        return childerlist.get(i).size();
    }

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

    @Override
    public Object getChild(int i, int i1) {
        return null;
    }

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

    @Override
    public long getChildId(int i, int i1) {
        return 0;
    }

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

    @Override
    public View getGroupView(final int i, boolean b, View view, ViewGroup viewGroup) {
        View inflate = View.inflate(context, R.layout.fatheritem, null);
        final CheckBox check=inflate.findViewById(R.id.father_check);
        TextView name=inflate.findViewById(R.id.father_name);

        name.setText(fatherlist.get(i).getName());
        check.setChecked(fatherlist.get(i).getCheck());

        //父的check事件
        check.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


                if(check.isChecked()==true){

                    List<ChilderBean> childBeen = childerlist.get(i);
                    for(int x=0;x<childBeen.size();x++){
                        childBeen.get(x).setCheck(true);
                    }
                    fatherlist.get(i).setCheck(true);
                    notifyDataSetChanged();

                }else if(check.isChecked()==false){
                    //子级别的全设置false
                    List<ChilderBean> childBeen = childerlist.get(i);
                    for(int x=0;x<childBeen.size();x++){
                        childBeen.get(x).setCheck(false);
                    }

                    fatherlist.get(i).setCheck(false);
                    notifyDataSetChanged();
                }

                //全选按钮 写点击改变父类按钮值事件下面
                MainActivity tag=(MainActivity)context;
                CheckBox check = (CheckBox) tag.findViewById(R.id.shop_checkAll);


                Boolean A = true;
                for(int c=0;c<fatherlist.size();c++){
                    Boolean fatherCheck = fatherlist.get(c).getCheck();
                    if(fatherCheck==false){
                        A=false;
                        break;
                    }else{
                        A=true;
                    }
                }
                check.setChecked(A);

                //接口回调
//                checks.isCheck(childlist);

            }
        });
        return inflate;
    }

    @Override
    public View getChildView(final int i, final int i1, boolean b, View view, ViewGroup viewGroup) {
        View inflate = View.inflate(context, R.layout.childitem, null);
        CheckBox check=inflate.findViewById(R.id.child_check);
        TextView name=inflate.findViewById(R.id.cart_title);
        TextView qian=inflate.findViewById(R.id.qian);
        TextView num=inflate.findViewById(R.id.care_money);
        ImageView im=inflate.findViewById(R.id.cart_iv);

        check.setChecked(childerlist.get(i).get(i1).getCheck());
        Glide.with(context).load(childerlist.get(i).get(i1).getIm()).into(im);
        qian.setText(childerlist.get(i).get(i1).getMoney()+"");
        name.setText(childerlist.get(i).get(i1).getName());
        num.setText(childerlist.get(i).get(i1).getNum()+"");

        //子类的check点击事件
        check.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                //判断点击事件
                Boolean childCheck = childerlist.get(i).get(i1).getCheck();
                if(childCheck){
                    childerlist.get(i).get(i1).setCheck(false);
                }else{
                    childerlist.get(i).get(i1).setCheck(true);
                }

                //设置初始默认值
                Boolean ischeckedall = true;
                //判断是否有哪个没选择,有一个就false
                List<ChilderBean> childBeen = childerlist.get(i);
                for(int x=0;x<childBeen.size();x++){
                    if(!childBeen.get(x).getCheck()){
                        ischeckedall=false;
                    }
                }
                //设置父类check显示
                if(ischeckedall==true){
                    fatherlist.get(i).setCheck(true);
                    notifyDataSetChanged();
                }else{
                    fatherlist.get(i).setCheck(false);
                    notifyDataSetChanged();
                }

                //接口回调
//                checks.isCheck(childlist);
            }
        });

        //自定义加减号的点击回调事件
        //设置加减号
//        adv = inflate.findViewById(R.id.text_view);
//        adv.setNumber(childlist.get(i).get(i1).getNum());
//        adv.setOnAddDelCilckListener(new AddDeleteNum.OnAddDelCilckListener() {
//            @Override
//            public void OnAddClick(View v) {
//                //View的值==自定义View的值 得到对应的对象
//                AddDeleteNum temp = (AddDeleteNum) v;
//                int origin = temp.getNumber();
//                origin++;
//                temp.setNumber(origin);
//
//                //设置为选中状态
//                childlist.get(i).get(i1).setChildCheck(true);
//                childlist.get(i).get(i1).setNum(origin);
//                notifyDataSetChanged();
//
//                //设置初始默认值
//                Boolean ischeckedall = true;
//                //判断是否有哪个没选择,有一个就false
//                List<ChildBean> childBeen = childlist.get(i);
//                for(int x=0;x<childBeen.size();x++){
//                    if(!childBeen.get(x).getChildCheck()){
//                        ischeckedall=false;
//                    }
//                }
//                //设置父类check显示
//                if(ischeckedall==true){
//                    grouplist.get(i).setFatherCheck(true);
//                    notifyDataSetChanged();
//                }else{
//                    grouplist.get(i).setFatherCheck(false);
//                    notifyDataSetChanged();
//                }
//
//                //接口套接口进入主界面进行回调
//                checks.isCheck(childlist);
//            }
//
//            @Override
//            public void OnDelClick(View v) {
//                AddDeleteNum temp = (AddDeleteNum) v;
//                int origin = temp.getNumber();
//                if(origin>1){
//                    origin--;
//                }else{
//                    origin=1;
//                }
//
//                temp.setNumber(origin);
//                childlist.get(i).get(i1).setChildCheck(true);
//                childlist.get(i).get(i1).setNum(origin);
//                notifyDataSetChanged();
//
//
//                //设置为选中状态
//                childlist.get(i).get(i1).setChildCheck(true);
//                childlist.get(i).get(i1).setNum(origin);
//                notifyDataSetChanged();
//
//                //设置初始默认值
//                Boolean ischeckedall = true;
//                //判断是否有哪个没选择,有一个就false
//                List<ChildBean> childBeen = childlist.get(i);
//                for(int x=0;x<childBeen.size();x++){
//                    if(!childBeen.get(x).getChildCheck()){
//                        ischeckedall=false;
//                    }
//                }
//                //设置父类check显示
//                if(ischeckedall==true){
//                    grouplist.get(i).setFatherCheck(true);
//                    notifyDataSetChanged();
//                }else{
//                    grouplist.get(i).setFatherCheck(false);
//                    notifyDataSetChanged();
//                }
//
//                //接口套接口
//                checks.isCheck(childlist);
//            }
//
//        });
        return inflate;
    }

    @Override
    public boolean isChildSelectable(int i, int i1) {
        return true;
    }
}
Bean包

package com.example.gouwuche.Beans;

/**
 * Created by xsj on 2017/12/16.
 */

public class FatherBean  {
    private Boolean isCheck;
    private String name;

    public FatherBean(Boolean isCheck, String name) {
        this.isCheck = isCheck;
        this.name = name;
    }
}
子Bean包

public class ChilderBean {

    private Boolean isCheck;
    private String name;
    private String im;
    private int num;
    private Double money;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值