防京东购物车页面

//自定义二级列表

package com.example.huoxuebin.myapplication;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.ExpandableListView;

/**
 * Created by huoxuebin on 2018/4/7.
 */

public class Goucard extends ExpandableListView {
    public Goucard(Context context) {
        super(context);
    }

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

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

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int i = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, i);
    }
}


//Activity代码


package com.example.month1.activity;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.month1.Adapter.ShopAdapter;
import com.example.month1.Bean.CountPriceBean;
import com.example.month1.Bean.ShopBean;
import com.example.month1.R;
import com.example.month1.activity.IM.IMainshopactivity;
import com.example.month1.coutes.MyExpanableListView;
import com.example.month1.presenter.Mainshoppresenter;
import com.example.month1.util.Apiutil;

import java.util.List;

public class Mainshopping extends AppCompatActivity implements IMainshopactivity,View.OnClickListener {


    private MyExpanableListView expanableListView;
    private LinearLayout linearLayout;
    private CheckBox check_all;
    private TextView text_total;
    private TextView text_buy;
    private RelativeLayout relative_progress;
    private Mainshoppresenter mainshoppresenter;
    private CountPriceBean countPriceBean;
    private List<ShopBean.DataBean.ListBean> listBeans;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 0) {
                CountPriceBean countPriceBean = (CountPriceBean) msg.obj;

                //设置价格和数量
                text_total.setText("合计:¥" + countPriceBean.getPriceString());
                text_buy.setText("去结算(" + countPriceBean.getCount() + ")");
            }
        }
    };
    private ShopAdapter shopAdapter;

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

        //获取控件
        expanableListView =(MyExpanableListView) findViewById(R.id.expanable_list_view);
        check_all =(CheckBox) findViewById(R.id.check_all);
        text_total =(TextView) findViewById(R.id.text_total);
        text_buy =(TextView) findViewById(R.id.text_buy);
        relative_progress =(RelativeLayout) findViewById(R.id.relative_progress);
        linearLayout =(LinearLayout) findViewById(R.id.linear_bottom);


        //全选的点击事件
        check_all.setOnClickListener(this);
        //去掉默认的指示器
        expanableListView.setGroupIndicator(null);


        //创建中间者

        mainshoppresenter = new Mainshoppresenter(this);

        mainshoppresenter.getshop(Apiutil.selectCartUrl);

        //跳转订单
        text_buy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(Mainshopping.this,Maindingdan.class));
            }
        });


    }

    @Override
    protected void onResume() {
        super.onResume();

        //显示进度条
        relative_progress.setVisibility(View.VISIBLE);
        mainshoppresenter.getshop(Apiutil.selectCartUrl);
    }

    //接收model层数据
    @Override
    public void select(final ShopBean shopBean) {

        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                //获取数据成功...隐藏
                relative_progress.setVisibility(View.GONE);
                if (shopBean != null){
                    //cartBean再设置奢配器之前需不需要改变数据
                    //1.在bean类中添加商家是否选中的字段....默认值的false,初始值是根据该组下面所有孩子的状态进行改变的
                    for (int i = 0;i<shopBean.getData().size();i++){
                        //当前组中所有孩子的数据
                        listBeans = shopBean.getData().get(i).getList();
                        //设置组的初始选中状态,,,,根据所有孩子的状态
                        shopBean.getData().get(i).setGroup_check(isAllChildInGroupChecked(listBeans));
                    }

                    //2.根据所有商家选中的状态,改变全选的状态
                    check_all.setChecked(isAllGroupChecked(shopBean));

                    //设置适配器
                    //   myAdapter = new MyAdapter(MainActivity.this, cartBean,handler,relative_progress,mainPresenter);
                    shopAdapter = new ShopAdapter(Mainshopping.this,shopBean,handler,relative_progress,mainshoppresenter);

                    expanableListView.setAdapter(shopAdapter);

                    //展开所有的组...expanableListView.expandGroup()
                    for (int i = 0;i<shopBean.getData().size();i++){
                        expanableListView.expandGroup(i);
                    }

                    //3.计算总价和商品的数量
                    shopAdapter.sendPriceAndCount();


                }else   {
                    Toast.makeText(Mainshopping.this,"购物车空,请添加购物车",Toast.LENGTH_SHORT).show();
                }
            }
        });




    }


    /**
     * 所有的商家是否选中
     * @param cartBean
     * @return
     */
    private boolean isAllGroupChecked(ShopBean cartBean) {

        for (int i=0;i<cartBean.getData().size();i++){
            //只要有一个组未选中 返回false
            if (! cartBean.getData().get(i).isGroup_check()){
                return false;
            }
        }

        return true;
    }

    /**
     * 当前组中所有的孩子是否选中
     * @param listBeans 当前组中所有的孩子的数据
     * @return
     */
    private boolean isAllChildInGroupChecked(List<ShopBean.DataBean.ListBean> listBeans) {

        for (int i=0;i<listBeans.size();i++){
            if (listBeans.get(i).getSelected() == 0){
                return false;
            }
        }

        return true;
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.check_all:
                //根据全选的状态更新所有商品的状态...check_all.isChecked() true...1;;;;false---0
                shopAdapter.setAllChildsChecked(check_all.isChecked());

                break;
        }
    }
}


//多选框选择器
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_checked="true" android:drawable="@drawable/shopping_cart_checked"></item>
    <item android:state_checked="false" android:drawable="@drawable/shopping_cart_none_check"></item>
    <item  android:drawable="@drawable/shopping_cart_none_check"></item>


</selector>


//改变数量边框

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">

    <solid android:color="#ffffff"/>

    <stroke android:color="#000000" android:width="0.1dp"/>

</shape>
//activity布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout

    xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.month1.activity.Mainshopping">

    <ScrollView
        android:id="@+id/scro"
        android:layout_above="@+id/linear_bottom"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <!--二级列表-->
            <com.example.month1.coutes.MyExpanableListView
                android:id="@+id/expanable_list_view"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

            </com.example.month1.coutes.MyExpanableListView>>

            <!--recyclerView展示为你推荐-->
            <TextView
                android:textSize="25dp"
                android:gravity="center"
                android:text="为你推荐"
                android:layout_marginTop="10dp"
                android:layout_gravity="center_horizontal"
                android:layout_width="match_parent"
                android:layout_height="30dp" />

            <android.support.v7.widget.RecyclerView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/f4re"></android.support.v7.widget.RecyclerView>


        </LinearLayout>

    </ScrollView>

    <RelativeLayout
        android:id="@+id/relative_progress"
        android:visibility="gone"
        android:layout_above="@+id/linear_bottom"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ProgressBar
            android:layout_centerInParent="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </RelativeLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:id="@+id/linear_bottom"
        android:layout_alignParentBottom="true"
        android:layout_width="match_parent"
        android:layout_height="50dp">

        <CheckBox
            android:button="@null"
            android:background="@drawable/select_check"
            android:layout_marginLeft="10dp"
            android:id="@+id/check_all"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/text_total"
            android:text="合计:¥0.00"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="10dp"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/text_buy"
            android:background="#ff0000"
            android:textColor="#ffffff"
            android:gravity="center"
            android:text="去结算(0)"
            android:layout_width="100dp"
            android:layout_height="match_parent" />



    </LinearLayout>

</RelativeLayout>
//二级列表布局


<?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="wrap_content"
    android:padding="10dp">

    <CheckBox
        android:id="@+id/child_check"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:background="@drawable/select_check"
        android:button="@null" />

    <ImageView
        android:id="@+id/child_image"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_marginLeft="10dp"
        android:layout_toRightOf="@+id/child_check" />

    <TextView
        android:id="@+id/child_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_toLeftOf="@+id/text_delete"
        android:layout_toRightOf="@+id/child_image"
        android:maxLines="2"
        android:minLines="2" />

    <TextView
        android:id="@+id/child_price"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/child_image"
        android:layout_marginLeft="5dp"
        android:layout_toRightOf="@+id/child_image"
        android:text="¥0.00"
        android:textColor="#ff0000" />


    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/child_image"
        android:layout_toLeftOf="@+id/text_delete"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/text_jian"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/bian_kuang"
            android:padding="5dp"
            android:text="-" />

        <TextView
            android:id="@+id/text_num"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/bian_kuang"
            android:paddingBottom="5dp"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:paddingTop="5dp"
            android:text="1" />

        <TextView
            android:id="@+id/text_add"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/bian_kuang"
            android:padding="5dp"
            android:text="+" />

    </LinearLayout>

    <TextView
        android:id="@+id/text_delete"
        android:layout_width="40dp"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/linearLayout"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="5dp"
        android:background="#ff0000"

        android:gravity="center"
        android:text="删除"
        android:textColor="#ffffff" />


</RelativeLayout>
//一级列表布局

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

    <CheckBox
        android:button="@null"
        android:background="@drawable/select_check"
        android:id="@+id/group_check"
        android:layout_gravity="center_vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/group_text"
        android:layout_marginLeft="10dp"
        android:layout_gravity="center_vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>
//mvp model层数据

package com.example.month1.model;

import android.util.Log;

import com.example.month1.Bean.ShopBean;
import com.example.month1.presenter.IM.IMainshoppersenter;
import com.example.month1.presenter.Mainshoppresenter;
import com.example.month1.util.OkHttp3Util_3;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * Created by huoxuebin on 2018/1/16.
 */

public class Mainshopmodel {

    private  IMainshoppersenter iMainshoppersenter;

    public Mainshopmodel(IMainshoppersenter  iMainshoppersenter) {


         this.iMainshoppersenter = iMainshoppersenter;

    }

    public void getpost(String selectCartUrl) {

        Map<String, String> params = new HashMap<>();
        params.put("uid","10190");
        OkHttp3Util_3.doPost(selectCartUrl, params, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

                if(response.isSuccessful()){
                    String json = response.body().string();

                        ShopBean shopBean = new Gson().fromJson(json, ShopBean.class);

                    Log.i("TAG","++++"+shopBean);

                    iMainshoppersenter.select(shopBean);


                }

            }
        });




    }
}

//presenter层 
package com.example.month1.presenter;

import com.example.month1.Bean.ShopBean;
import com.example.month1.activity.IM.IMainshopactivity;
import com.example.month1.activity.Mainshopping;
import com.example.month1.model.Mainshopmodel;
import com.example.month1.presenter.IM.IMainshoppersenter;

/**
 * Created by huoxuebin on 2018/1/16.
 */

public class Mainshoppresenter implements IMainshoppersenter {

    private  Mainshopmodel mainshopmodel;
    private  IMainshopactivity iMainshopactivity;

    public Mainshoppresenter(IMainshopactivity iMainshopactivity) {
        this.iMainshopactivity=iMainshopactivity;

        mainshopmodel = new Mainshopmodel(this);

    }

    public void getshop(String selectCartUrl) {

        mainshopmodel.getpost(selectCartUrl);


    }

    @Override
    public void select(ShopBean shopBean) {
        iMainshopactivity.select(shopBean);

    }
}
//bean包需要添加的代码

private boolean group_check;

public boolean isGroup_check() {
    return group_check;
}

public void setGroup_check(boolean group_check) {
    this.group_check = group_check;
}
//创建结算bean
package com.example.month1.Bean;

/**
 * Created by Dash on 2018/1/4.
 */
public class CountPriceBean {
    private String priceString;
    private int count;

    public CountPriceBean(String priceString, int count) {
        this.priceString = priceString;
        this.count = count;
    }

    public String getPriceString() {
        return priceString;
    }

    public void setPriceString(String priceString) {
        this.priceString = priceString;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }
}
//适配器

package com.example.month1.Adapter;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import com.example.month1.Bean.CountPriceBean;
import com.example.month1.Bean.ShopBean;
import com.example.month1.R;
import com.example.month1.presenter.Mainshoppresenter;
import com.example.month1.util.Apiutil;
import com.example.month1.util.OkHttp3Util_3;

import java.io.IOException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * Created by huoxuebin on 2018/1/15.
 */

public class ShopAdapter extends BaseExpandableListAdapter {


    private  ShopBean shopBean;
    private  Mainshoppresenter mainshoppresenter;
    private RelativeLayout relative_progress;
    private Handler handler;

    private Context context;
    private int childIndex;
    private int allIndex;

    public ShopAdapter(Context context, ShopBean shopBean, Handler handler, RelativeLayout relative_progress, Mainshoppresenter mainshoppresenter) {
        this.context = context;
        this.shopBean = shopBean;
        this.handler = handler;
        this.relative_progress = relative_progress;
        this.mainshoppresenter = mainshoppresenter;
    }

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

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

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

    @Override
    public Object getChild(int groupPosition, int childPosition) {

        return shopBean.getData().get(groupPosition).getList().get(childPosition);
    }

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

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

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

    @Override
    public View getGroupView(int groupPosition, boolean b, View view, ViewGroup viewGroup) {
        final GroupHolder holder;
        if (view == null){
            view = View.inflate(context, R.layout.group_layout,null);
            holder = new GroupHolder();

           holder.checkBox=view.findViewById(R.id.group_check);
           holder.textView= view.findViewById(R.id.group_text);

            view.setTag(holder);
        }else {
            holder = (GroupHolder) view.getTag();
        }

        final ShopBean.DataBean dataBean = shopBean.getData().get(groupPosition);

        //赋值
        holder.textView.setText(dataBean.getSellerName());
        holder.checkBox.setChecked(dataBean.isGroup_check());

        //商家的点击事件
        holder.checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //显示progress
                relative_progress.setVisibility(View.VISIBLE);

                //根据商家的状态holder.checkbox.ischecked(),改变下面所有子条目的状态,,,10,,改变十次,更新完成一个之后再去执行下一个...递归
                childIndex = 0;
                updateAllChildInGroup(dataBean,holder.checkBox.isChecked());

            }
        });


        return view;
    }

    /**
     * 根据商家状态使用递归改变所有的子条目
     * @param dataBean
     * @param checked
     */
    private void updateAllChildInGroup(final ShopBean.DataBean dataBean, final boolean checked) {

        ShopBean.DataBean.ListBean listBean = dataBean.getList().get(childIndex);

        Map<String, String> params = new HashMap<>();

        //?uid=71&sellerid=1&pid=1&selected=0&num=10
        params.put("uid","10190");
        params.put("sellerid", String.valueOf(listBean.getSellerid()));
        params.put("pid", String.valueOf(listBean.getPid()));

        params.put("selected", String.valueOf(checked ? 1:0));
        params.put("num", String.valueOf(listBean.getNum()));

        OkHttp3Util_3.doPost(Apiutil.updateCartUrl, params, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                //更新成功一条
                if (response.isSuccessful()){
                    //索引增加
                    childIndex ++;
                    if (childIndex < dataBean.getList().size()){
                        //再去更新下一条
                        updateAllChildInGroup(dataBean,checked);

                    }else {//全都更新完成了....重新查询购物车
                        mainshoppresenter.getshop(Apiutil.selectCartUrl);
                    }
                }

            }
        });

    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean b, View view, ViewGroup viewGroup) {
        ChildHolder holder;
        if (view == null){
            view = View.inflate(context, R.layout.child_layout,null);
            holder = new ChildHolder();

            holder.checkBox = view.findViewById(R.id.child_check);
            holder.text_title = view.findViewById(R.id.child_title);
            holder.imageView = view.findViewById(R.id.child_image);
            holder.text_price = view.findViewById(R.id.child_price);
            holder.text_jian = view.findViewById(R.id.text_jian);
            holder.text_num = view.findViewById(R.id.text_num);
            holder.text_add = view.findViewById(R.id.text_add);
            holder.text_delete = view.findViewById(R.id.text_delete);

            view.setTag(holder);
        }else {
            holder = (ChildHolder) view.getTag();
        }

        final ShopBean.DataBean.ListBean listBean = shopBean.getData().get(groupPosition).getList().get(childPosition);

        //赋值
        holder.text_title.setText(listBean.getTitle());
        holder.text_price.setText("¥"+listBean.getBargainPrice());

        String[] strings = listBean.getImages().split("\\|");
        Glide.with(context).load(strings[0]).into(holder.imageView);

        holder.checkBox.setChecked(listBean.getSelected() == 0? false:true);//根据0,1进行设置是否选中
        //setText()我们使用一定是设置字符串
        holder.text_num.setText(listBean.getNum()+"");

        //点击事件
        holder.checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //此时需要显示进度条
                relative_progress.setVisibility(View.VISIBLE);
                //更新购物车,,,,需要改变是否选中,,,如果现在显示的是0,改成1;;;1--->0

                Map<String, String> params = new HashMap<>();

                //?uid=71&sellerid=1&pid=1&selected=0&num=10
                params.put("uid","10190");
                params.put("sellerid", String.valueOf(listBean.getSellerid()));
                params.put("pid", String.valueOf(listBean.getPid()));

                params.put("selected", String.valueOf(listBean.getSelected() == 0? 1:0));
                params.put("num", String.valueOf(listBean.getNum()));

                OkHttp3Util_3.doPost(Apiutil.updateCartUrl, params, new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {

                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        //更新成功之后,,,,再次查询购物车的数据进行展示
                        if (response.isSuccessful()){
                            mainshoppresenter.getshop(Apiutil.selectCartUrl);
                        }
                    }
                });

            }
        });

        //加号
        holder.text_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //此时需要显示进度条
                relative_progress.setVisibility(View.VISIBLE);
                //更新购物车,,,,需要改变是数量,,,,需要加1

                Map<String, String> params = new HashMap<>();

                //?uid=71&sellerid=1&pid=1&selected=0&num=10
                params.put("uid","10190");
                params.put("sellerid", String.valueOf(listBean.getSellerid()));
                params.put("pid", String.valueOf(listBean.getPid()));
                params.put("selected", String.valueOf(listBean.getSelected()));

                params.put("num", String.valueOf(listBean.getNum()+1));

                OkHttp3Util_3.doPost(Apiutil.updateCartUrl, params, new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {

                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        //更新成功之后,,,,再次查询购物车的数据进行展示
                        if (response.isSuccessful()){
                            mainshoppresenter.getshop(Apiutil.selectCartUrl);
                        }
                    }
                });

            }
        });
        //减号
        holder.text_jian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int num = listBean.getNum();

                if (num == 1){
                    return;
                }

                //此时需要显示进度条
                relative_progress.setVisibility(View.VISIBLE);
                //更新购物车,,,,需要改变是数量,,,,需要加1

                Map<String, String> params = new HashMap<>();

                //?uid=71&sellerid=1&pid=1&selected=0&num=10
                params.put("uid","10190");
                params.put("sellerid", String.valueOf(listBean.getSellerid()));
                params.put("pid", String.valueOf(listBean.getPid()));
                params.put("selected", String.valueOf(listBean.getSelected()));

                params.put("num", String.valueOf(num-1));

                OkHttp3Util_3.doPost(Apiutil.updateCartUrl, params, new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {

                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        //更新成功之后,,,,再次查询购物车的数据进行展示
                        if (response.isSuccessful()){
                            mainshoppresenter.getshop(Apiutil.selectCartUrl);
                        }
                    }
                });
            }
        });
        //删除
        holder.text_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //显示进度条
                relative_progress.setVisibility(View.VISIBLE);
                //调用删除的接口
                Map<String, String> params = new HashMap<>();

                //uid=72&pid=1
                params.put("uid","10190");
                params.put("pid", String.valueOf(listBean.getPid()));

                OkHttp3Util_3.doPost(Apiutil.deleteCartUrl, params, new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {

                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        if (response.isSuccessful()){
                            //再次请求购物车的数据
                            mainshoppresenter.getshop(Apiutil.selectCartUrl);
                        }
                    }
                });

            }
        });


        return view;
    }

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

    /**
     * 计算总价和数量,,,并且发送给activity进行显示
     */
    public void sendPriceAndCount() {

        double price = 0;
        int count = 0;

        for (int i = 0;i<shopBean.getData().size();i++){
            List<ShopBean.DataBean.ListBean> listBeans = shopBean.getData().get(i).getList();
            for (int j = 0; j< listBeans.size(); j++){

                if (listBeans.get(j).getSelected() == 1){
                    price += listBeans.get(j).getBargainPrice() * listBeans.get(j).getNum();
                    count += listBeans.get(j).getNum();
                }
            }
        }

        //double高精度,,,计算的时候可能会出现一串数字...保留两位
        DecimalFormat decimalFormat = new DecimalFormat("0.00");
        String priceString = decimalFormat.format(price);
        CountPriceBean countPriceBean = new CountPriceBean(priceString, count);

        //发送到主页面进行显示
        Message msg = Message.obtain();

        msg.what = 0;
        msg.obj = countPriceBean;
        handler.sendMessage(msg);
    }

    /**
     * 根据全选的状态更新所有商品的状态
     * @param checked
     */
    public void setAllChildsChecked(boolean checked) {

        //创建一个大的结合,,,存放所有商品的数据
        List<ShopBean.DataBean.ListBean> allList = new ArrayList<>();
        for (int i= 0;i<shopBean.getData().size();i++){
            List<ShopBean.DataBean.ListBean> listBeans = shopBean.getData().get(i).getList();
            for (int j=0;j<listBeans.size();j++){
                allList.add(listBeans.get(j));
            }
        }

        //显示progress
        relative_progress.setVisibility(View.VISIBLE);

        //递归更新....
        allIndex = 0;
        updateAllChecked(allList,checked);

    }

    /**
     * 更新所有的商品
     * @param allList
     * @param checked
     */
    private void updateAllChecked(final List<ShopBean.DataBean.ListBean> allList, final boolean checked) {

        ShopBean.DataBean.ListBean listBean = allList.get(allIndex);
        Map<String, String> params = new HashMap<>();

        //?uid=71&sellerid=1&pid=1&selected=0&num=10
        params.put("uid","10190");
        params.put("sellerid", String.valueOf(listBean.getSellerid()));
        params.put("pid", String.valueOf(listBean.getPid()));

        params.put("selected", String.valueOf(checked ? 1:0));
        params.put("num", String.valueOf(listBean.getNum()));

        OkHttp3Util_3.doPost(Apiutil.updateCartUrl, params, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                //更新一条成功
                if (response.isSuccessful()){
                    allIndex ++;

                    if (allIndex < allList.size()){
                        //继续更新下一条
                        updateAllChecked(allList,checked);
                    }else {
                        //重新查询
                        mainshoppresenter.getshop(Apiutil.selectCartUrl);
                    }

                }
            }
        });
    }


    private class GroupHolder{
        CheckBox checkBox;
        TextView textView;
    }

    private class ChildHolder{
        CheckBox checkBox;
        ImageView imageView;
        TextView text_title;
        TextView text_price;
        TextView text_num;
        TextView text_jian;
        TextView text_add;
        TextView text_delete;
    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值