模仿京东购物车(二级列表,mvp请求数据)

//加入依赖

compile 'com.squareup.okhttp3:okhttp:3.9.0'
compile 'com.google.code.gson:gson:2.8.2'
compile 'com.android.support:recyclerview-v7:26.0.0-alpha1'
compile 'com.github.bumptech.glide:glide:3.7.0'

//加入请求网络的权限

<uses-permission android:name="android.permission.INTERNET"/>

//主布局

<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:id="@+id/top_bar"
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:background="#f7f7f7"
        android:orientation="vertical" >
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:background="@android:color/transparent"
            android:orientation="vertical" >
            <ImageView
                android:id="@+id/back"
                android:layout_width="48dp"
                android:layout_height="48dp"
                android:layout_alignParentLeft="true"
                android:layout_gravity="center_vertical"
                android:padding="12dp"
                android:src="@drawable/back" />
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:minHeight="48dp"
                android:text="购物车"
                android:textColor="#1a1a1a"
                android:textSize="16sp" />
            <TextView
                android:id="@+id/edit"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_marginRight="40dp"
                android:gravity="center"
                android:minHeight="48dp"
                android:text="编辑"
                android:textColor="#1a1a1a"
                android:textSize="14sp"
                android:visibility="visible" />
        </RelativeLayout>
    </LinearLayout>
    <ExpandableListView
        android:id="@+id/exListView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:childIndicator="@null"
        android:groupIndicator="@null" >
    </ExpandableListView>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:gravity="center_vertical"
        android:orientation="horizontal" >

        <CheckBox
            android:id="@+id/all_chekbox"
            android:layout_marginLeft="20dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="全选"/>
        <LinearLayout
            android:id="@+id/ll_info"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="4"
            >
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:layout_marginRight="20dp"
                android:layout_weight="1"
                >
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"
                    android:gravity="right"
                    >
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="5dp"
                        android:text="合计:"
                        android:textSize="18sp"
                        android:textStyle="bold" />
                    <TextView
                        android:id="@+id/total_price"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="¥0.00"
                        android:textColor="#f23232"
                        android:textSize="16sp"
                        android:textStyle="bold" />
                </LinearLayout>
                <TextView
                    android:id="@+id/total_number"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="共有商品:0件"
                    android:gravity="right"
                    android:textSize="16sp"
                    android:textStyle="bold" />
            </LinearLayout>
            <TextView
                android:id="@+id/tv_go_to_pay"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="3"
                android:background="#fd7a05"
                android:clickable="true"
                android:gravity="center"
                android:text="结算"
                android:textColor="#FAFAFA" />
        </LinearLayout>

    </LinearLayout>
</LinearLayout>

//一级类表布局ex_group_item.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#cfc3c3"
    android:orientation="horizontal">
    <CheckBox
        android:id="@+id/group_checkbox"
        android:layout_marginLeft="20dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:focusable="false"/>
    <TextView
        android:id="@+id/shop_name"
        android:layout_marginLeft="20dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="16dp" />
</LinearLayout>

 
//二级类表布局ex_child_item.xml 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="230dp"
    android:orientation="horizontal">
    <CheckBox
        android:id="@+id/child_checkbox"
        android:layout_marginTop="50dp"
        android:layout_marginLeft="20dp"
        android:layout_marginBottom="50dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="20dp">
        <TextView
            android:id="@+id/shop_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="17dp"
            android:layout_marginStart="17dp"
            android:text="TextView" />
        <ImageView
            android:id="@+id/shop_img"
            android:layout_width="90dp"
            android:layout_height="90dp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/shop_name"
            android:layout_marginTop="30dp"
            app:srcCompat="@mipmap/ic_launcher" />
        <TextView
            android:id="@+id/shop_price"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignTop="@+id/shop_img"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="10dp"
            android:text="¥20"
            android:textColor="#f23232" />
        <chendemin.bwei.com.twoshoppingcars.view.AddDeleteView
            android:id="@+id/adv"
            android:layout_width="160dp"
            android:layout_height="30dp"
            android:layout_below="@+id/shop_price"
            android:layout_marginLeft="140dp"
            android:layout_marginTop="30dp"
            android:focusable="false"
            app:left_text="-"
            app:middle_text="1"
            app:right_text="+"/>
        <Button
            android:id="@+id/shop_delete"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:text="删除"
            android:visibility="invisible" />
    </RelativeLayout>
</LinearLayout>


//View绘制的布局文件layout_add_delete.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:weightSum="1">
    <TextView
        android:id="@+id/txt_delete"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:text="-"
        android:gravity="center"
        android:background="#8b948b"/>
    <EditText
        android:id="@+id/et_number"
        android:layout_marginTop="2dp"
        android:layout_width="50dp"
        android:layout_height="30dp"
        android:background="@drawable/edit"
        android:layout_weight="0.00"
        android:gravity="center"
        android:text="1"/>
    <TextView
        android:id="@+id/txt_add"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:text="+"
        android:gravity="center"
        android:background="#8b948b"/>
</LinearLayout>

//在value下面创建attrs.xml,按钮文件

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="AddDeleteViewStyle">
        <attr name="left_text" format="string"></attr>
        <attr name="right_text" format="string"></attr>
        <attr name="middle_text" format="string"></attr>
        <attr name="left_text_color" format="color"></attr>
    </declare-styleable>
</resources>

//在drawable下面添加"<"这个样子的图片,在创建edit.xml

//edit.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#FFFFFF" />
<!--<corners android:radius="3dip"/>-->
<stroke
    android:width="1dip"
    android:color="#BDC7D8" />
</shape>

//view自定义的加减数量按钮

public class AddDeleteView extends LinearLayout{
    private OnAddDelClickListener listener;
    private EditText etNumber;
    //对外提供一个点击的回调接口
    public interface OnAddDelClickListener{
        void onAddClick(View v);
        void onDelClick(View v);
    }
    public void setOnAddDelClickListener(OnAddDelClickListener listener){
        if(listener!=null){
            this.listener=listener;
        }
    }
    public AddDeleteView(Context context) {
        this(context,null);
    }

    public AddDeleteView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public AddDeleteView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs, defStyleAttr);
    }

    private void initView(Context context, AttributeSet attrs, int defStyleAttr) {
        View.inflate(context, R.layout.layout_add_delete,this);
        //获取控件
        TextView txtDelete=findViewById(R.id.txt_delete);
        TextView txtAdd=findViewById(R.id.txt_add);
        etNumber = findViewById(R.id.et_number);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.AddDeleteViewStyle);
        String leftText = typedArray.getString(R.styleable.AddDeleteViewStyle_left_text);
        String rightText = typedArray.getString(R.styleable.AddDeleteViewStyle_right_text);
        String middleText = typedArray.getString(R.styleable.AddDeleteViewStyle_middle_text);
        int color = typedArray.getColor(R.styleable.AddDeleteViewStyle_left_text_color, Color.BLACK);
        txtDelete.setText(leftText);
        txtAdd.setText(rightText);
        etNumber.setText(middleText);
        txtDelete.setTextColor(color);
        //回收
        typedArray.recycle();
        txtDelete.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                listener.onDelClick(view);
            }
        });
        txtAdd.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                listener.onAddClick(view);
            }
        });
    }
    //对外提供一个修改数字的方法
    public void setNumber(int number){
        if(number>0){
            etNumber.setText(number+"");
        }
    }
    //对外提供一个获取当前数字的方法
    public int getNumber(){
        String string = etNumber.getText().toString();
        int i = Integer.parseInt(string);
        return i;
    }
}

//bean类

//一级bean类

public class goodBean {
    private String sellerName;
    private boolean groupCb;
    public goodBean(String sellerName, boolean groupCb) {
        this.sellerName = sellerName;
        this.groupCb = groupCb;
    }
    public String getSellerName() {
        return sellerName;
    }

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

    public boolean isGroupCb() {
        return groupCb;
    }

    public void setGroupCb(boolean groupCb) {
        this.groupCb = groupCb;
    }

    @Override
    public String toString() {
        return "goodBean{" +
                "sellerName='" + sellerName + '\'' +
                ", groupCb=" + groupCb +
                '}';
    }
}
//二级bean类
public class goodsBean {
    private double price;
    private String title;
    private String images;
    private int num;
    private boolean childCb;
    private boolean btn;

    public goodsBean(double price, String title, String images, int num, boolean childCb, boolean btn) {
        this.price = price;
        this.title = title;
        this.images = images;
        this.num = num;
        this.childCb = childCb;
        this.btn = btn;
    }

    public double getPrice() {
        return price;
    }

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

    public String getTitle() {
        return title;
    }

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

    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 boolean isChildCb() {
        return childCb;
    }

    public void setChildCb(boolean childCb) {
        this.childCb = childCb;
    }

    public boolean isBtn() {
        return btn;
    }

    public void setBtn(boolean btn) {
        this.btn = btn;
    }

    @Override
    public String toString() {
        return "goodsBean{" +
                "price=" + price +
                ", title='" + title + '\'' +
                ", images='" + images + '\'' +
                ", num=" + num +
                ", childCb=" + childCb +
                ", btn=" + btn +
                '}';
    }
}

//m层

public class HttpUtils {
    private static volatile HttpUtils instance;
    private Handler handler=new Handler();
    //单例模式
    public static HttpUtils getInstance() {
        if (instance == null) {
            synchronized (HttpUtils.class) {
                if (instance == null) {
                    instance = new HttpUtils();
                }
            }
        }
        return instance;
    }
    public void get(String url, Map<String, String> map, final CallBack callback, final Class c) {
        StringBuffer sb = new StringBuffer();
        sb.append(url);
        // 如果存在?
        if (sb.indexOf("?") != -1) {
            // 如果?不在最后一位
            if (sb.indexOf("?") != sb.length() - 1) {
                sb.append("&");
            }
        } else {
            sb.append("?");
        }
        for (Map.Entry<String, String> entry : map.entrySet()) {
            sb.append(entry.getKey())
                    .append("=")
                    .append(entry.getValue())
                    .append("&");
        }
        if (sb.indexOf("&") != -1) {
            sb.deleteCharAt(sb.lastIndexOf("&"));
        }
        Log.i(TAG, "get url: " + sb);
        //1:创建OkHttpClient对象
        OkHttpClient okHttpClient = new OkHttpClient();
        //2:创建Request对象
        final Request request = new Request.Builder()
                .get()
                .url(sb.toString())
                .build();
        //3:创建Call对象
        Call call = okHttpClient.newCall(request);
        //4:请求网络
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, final IOException e) {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        callback.onFailed(e);
                    }
                });
            }
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String result = response.body().string();
                //拿到数据解析
                final Object o = new Gson().fromJson(result, c);
                //当前是在子线程,回到主线程中
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        //回调
                        callback.onSuccess(o);
                    }
                });
            }
        });
    }
    //post请求
    public void post(String url, Map<String,String> map, final CallBack callBack, final Class c){
        //1:创建OkHttpClient对象
        OkHttpClient okHttpClient = new OkHttpClient();
        //2:提供post请求需要的body对象
        FormBody.Builder builder = new FormBody.Builder();
        for(Map.Entry<String,String> entry:map.entrySet()){
            builder.add(entry.getKey(),entry.getValue());
        }
        FormBody body = builder.build();
        //3:创建Request对象
        final Request request = new Request.Builder()
                .post(body)
                .url(url)
                .build();
        //4:创建Call对象
        Call call = okHttpClient.newCall(request);
        //5:请求网络
        call.enqueue(new Callback() {
            //请求失败
            @Override
            public void onFailure(Call call, final IOException e) {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        callBack.onFailed(e);
                    }
                });
            }
            //请求成功
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String result = response.body().string();
                //拿到数据解析
                final Object o = new Gson().fromJson(result, c);
                //当前是在子线程,回到主线程中
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        //回调
                        callBack.onSuccess(o);
                    }
                });
            }
        });
    }
}

//callback接口

public interface CallBack {
     void onSuccess(Object o);
     void onFailed(Exception e);
}
//v层

public interface iView {
    void success(List<goodBean> list, List<List<goodsBean>> lists);
    void failed(Exception e);
}

//p层

public class Presenter {
    private  Context context;
    private iView iv;
    private List<goodBean> list=new ArrayList<>();
    private List<List<goodsBean>> lists=new ArrayList<>();
    public Presenter(Context context, iView iv) {
        this.context=context;
        this.iv=iv;
    }
    public void get(){
        Map<String, String> map = new HashMap<>();
        map.put("uid","100");
        HttpUtils.getInstance().get("http://120.27.23.105/product/getCarts", map, new CallBack() {
            @Override
            public void onSuccess(Object o) {
                listBean bean = (listBean) o;
                List<listBean.DataBean> data = bean.getData();
                for (int i = 0; i < data.size(); i++) {
                    list.add(new goodBean(data.get(i).getSellerName(),false));
                    List<listBean.DataBean.ListBean> beanList = data.get(i).getList();
                    List<goodsBean> ls=new ArrayList<>();
                    for (int j = 0; j < beanList.size(); j++) {
                        String images = beanList.get(j).getImages();
                        String[] split = images.split("!");
                        ls.add(new goodsBean(beanList.get(j).getPrice(),beanList.get(j).getTitle(),split[0],1,false,false));
                    }
                    lists.add(ls);
                }
                iv.success(list,lists);
            }
            @Override
            public void onFailed(Exception e) {
                Toast.makeText(context,e.getMessage()+"",Toast.LENGTH_SHORT).show();
            }
        },listBean.class);
    }
}

//MainActivity

public class MainActivity extends AppCompatActivity implements iView{
    private ExpandableListView exListView;
    public CheckBox allCheckbox;
    private TextView totalPrice;
    private TextView totalnumber;
    private TextView edit;
    private ExpandableAdapter expandableAdapter;
    private boolean flagedit=true;//自己定义的一个值
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化控件
        initView();
        Presenter presenter = new Presenter(this, this);
        presenter.get();
    }
   //初始化控件
    private void initView() {
        exListView = (ExpandableListView) findViewById(R.id.exListView);
        allCheckbox = (CheckBox)findViewById(R.id.all_chekbox);
        totalPrice = (TextView)findViewById(R.id.total_price);
        totalnumber = (TextView)findViewById(R.id.total_number);
        edit = (TextView) findViewById(R.id.edit);
    }

    @Override
    public void success(List<goodBean> list, List<List<goodsBean>> lists) {
        //获取二级列表适配器
        expandableAdapter = new ExpandableAdapter(MainActivity.this, list, lists);
        exListView.setAdapter(expandableAdapter);
        initDate(list,lists);
        for(int i = 0; i < expandableAdapter.getGroupCount(); i++){
            exListView.expandGroup(i);
        }
        expandableAdapter.notifyDataSetChanged();
    }

    private void initDate(final List<goodBean> list, final List<List<goodsBean>> lists) {
        //父级点击事件
        exListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView expandableListView, View view, int i, long l) {
                return true;
            }
        });
        //全选监听
        allCheckbox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                boolean checked = allCheckbox.isChecked();
                //改变一级item复选框
                for (goodBean i:list) {
                    i.setGroupCb(checked);
                }
                //改变二级item复选框
                for (List<goodsBean> i1:lists) {
                    for(int r=0;r<i1.size();r++) {
                        i1.get(r).setChildCb(checked);
                    }
                }
                expandableAdapter.notifyDataSetChanged();
                changesum(lists);
            }
        });
        //点击编辑事件
        edit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                for (List<goodsBean> i1:lists){
                    for(int r=0;r<i1.size();r++) {
                        i1.get(r).setBtn(flagedit);
                    }
                }
                flagedit=!flagedit;
                expandableAdapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    public void failed(Exception e) {
    }
    //计算和数量总价
    public void changesum(List<List<goodsBean>> lists){
        int count=0;
        double sum=0;
        for (List<goodsBean>i1:lists){
            for(int r=0;r<i1.size();r++) {
                boolean childCb1 = i1.get(r).isChildCb();
                if(childCb1){
                    double price = i1.get(r).getPrice();
                    int num = i1.get(r).getNum();
                    sum+=price*num;
                    count++;
                }
            }
        }
        totalPrice.setText("¥"+sum);
        totalnumber.setText("共有商品:"+count+"件");
    }
}

//二级类表适配器adater

public class ExpandableAdapter extends BaseExpandableListAdapter {
    private Context context;
    private List<goodBean> list;
    private List<List<goodsBean>> lists;

    public ExpandableAdapter(Context context, List<goodBean> list, List<List<goodsBean>> lists) {
        this.context = context;
        this.list = list;
        this.lists = lists;
    }

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

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

    @Override
    public Object getGroup(int i) {
        return list.get(i).getSellerName();
    }

    @Override
    public Object getChild(int i, int i1) {
        return lists.get(i).get(i1).getTitle();
    }

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

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

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

    @Override
    public View getGroupView(final int i, boolean b, View view, ViewGroup viewGroup) {
        //获取子布局
        view=View.inflate(context, R.layout.ex_group_item,null);
        //获取id值
        final CheckBox groupCb= view.findViewById(R.id.group_checkbox);
        TextView shopName= view.findViewById(R.id.shop_name);
        //赋值
        shopName.setText(list.get(i).getSellerName());
        groupCb.setChecked(list.get(i).isGroupCb());
        //复选按钮
        groupCb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //选中状态
                boolean checked = groupCb.isChecked();
                list.get(i).setGroupCb(checked);
                //获取上下文
                MainActivity main = (MainActivity)context;
                //增强for
                for (goodBean i:list) {
                    //一级复选框
                    boolean iGroupCb = i.isGroupCb();
                    //判断一级复选框没有选中状态
                    if(!iGroupCb){
                        //全选也没有选中状态
                        main.allCheckbox.setChecked(false);
                        break;
                    }else{//全选选中状态
                        main.allCheckbox.setChecked(true);
                    }
                }
                //拿到子级长度
                int size = lists.get(i).size();
                //如果父级全部选中
                if(checked){
                    for (int j = 0; j <size; j++) {
                        lists.get(i).get(j).setChildCb(true);
                    }
                }else{
                    for (int j = 0; j <size; j++) {
                        lists.get(i).get(j).setChildCb(false);
                    }
                }
                notifyDataSetChanged();
                main.changesum(lists);
            }
        });
        return view;
    }

    @Override
    public View getChildView(final int i, final int i1, boolean b, View v, ViewGroup viewGroup) {
        //加载视图
        v=View.inflate(context, R.layout.ex_child_item ,null);
        //获取id值
        final CheckBox childCb = v.findViewById(R.id.child_checkbox);
        TextView shopTitle= v.findViewById(R.id.shop_title);
        TextView shopPrice= v.findViewById(R.id.shop_price);
        ImageView shopImg=v.findViewById(R.id.shop_img);
        final AddDeleteView adv = v.findViewById(R.id.adv);
        Button shop_delete=v.findViewById(R.id.shop_delete);
        //添加值
        childCb.setChecked(lists.get(i).get(i1).isChildCb());
        Glide.with(context).load(lists.get(i).get(i1).getImages()).into(shopImg);
        shopTitle.setText(lists.get(i).get(i1).getTitle());
        shopPrice.setText(lists.get(i).get(i1).getPrice()+"");
        adv.setNumber(lists.get(i).get(i1).getNum());
        final MainActivity main= (MainActivity) context;
        //控制删除按钮的显隐
        if(lists.get(i).get(i1).isBtn()){
            shop_delete.setVisibility(View.VISIBLE);
        }else{
            shop_delete.setVisibility(View.INVISIBLE);
        }
        //删除按钮监听
        shop_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int size = lists.get(i).size();
                if (size == 1) {
                    lists.remove(i);
                    list.remove(i);
                } else {
                    lists.get(i).remove(i1);
                }
                //点击删除后隐藏所有删除按钮
                for (List<goodsBean> i1 : lists) {
                    for (int r = 0; r < i1.size(); r++) {
                        i1.get(r).setBtn(false);
                    }
                }
                notifyDataSetChanged();
                main.changesum(lists);
            }
        });
        //加减器逻辑
        adv.setOnAddDelClickListener(new AddDeleteView.OnAddDelClickListener() {
            @Override
            public void onAddClick(View v) {
                int number = adv.getNumber();
                number++;
                adv.setNumber(number);
                lists.get(i).get(i1).setNum(number);
                main.changesum(lists);
            }
            @Override
            public void onDelClick(View v) {
                int number = adv.getNumber();
                number--;
                adv.setNumber(number);
                lists.get(i).get(i1).setNum(number);
                main.changesum(lists);
            }
        });
        //二级组的复选框监听
        childCb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                boolean flag=false;
                boolean cchecked = childCb.isChecked();
                lists.get(i).get(i1).setChildCb(cchecked);
                MainActivity main= (MainActivity) context;
                //二级复选框
                for (List<goodsBean> i1:lists){
                    for(int r=0;r<i1.size();r++) {
                        boolean childCb1 = i1.get(r).isChildCb();
                        //判断二级复选框没有选中状态
                        if(!childCb1){
                            main.allCheckbox.setChecked(false);  //全选没有选中状态
                            list.get(i).setGroupCb(false);//一级复选框也没有选中
                            flag=true;
                            break;
                        }else{
                            main.allCheckbox.setChecked(true);//全选选中状态
                            list.get(i).setGroupCb(true);//一级复选框选中
                        }
                    }
                    if(flag){
                        break;
                    }
                }
                //拿到子级的长度
                int size = lists.get(i).size();
                //遍历子级
                for(int x=0;x<size;x++) {
                    //拿到子级复选框的状态
                    boolean childCb1 = lists.get(i).get(x).isChildCb();
                    if(!childCb1){//没有选中
                        list.get(i).setGroupCb(false);//父级就没有选中
                        break;
                    }else{//否则
                        list.get(i).setGroupCb(true);//父级就选中
                    }
                }
                notifyDataSetChanged();
                main.changesum(lists);
            }
        });
        return v;
    }

    @Override
    public boolean isChildSelectable(int i, int i1) {
        return false;
    }
}




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值