购物车展示与之前的商品展示和流式布局搜索可以结合起来

依赖不说了,去前面的代码里找


先来主activity.xml

主要说明一下如何粘贴。跟着步骤一步一步走就OK了,报错不要着急慢慢的一步一步解决错误。如果要结合使用请注意名字改成自己所使用的 Activity的名称。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".ShopActivity">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >


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

            <com.example.administrator.month.view.ErMyliebiao

                android:id="@+id/expanable_list_view"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

            </com.example.administrator.month.view.ErMyliebiao>

        </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:background="#d7d8d3d3"
        android:orientation="horizontal"
        android:id="@+id/linear_bottom"
        android:layout_alignParentBottom="true"
        android:layout_width="match_parent"
        android:layout_height="50dp">
        <CheckBox

            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>

主activity

package com.example.administrator.monthtwo;

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

import com.example.administrator.monthtwo.bean.ShopBean;

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

public class ShopActivity extends AppCompatActivity {

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

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



                text_buy.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {


                    }
                });
            }
        }
    };

    private int uid = 14538;

    private LinearLayout linearLayout;
    private CheckBox check_all;
    private TextView text_total;
    private TextView text_buy;
    private RelativeLayout relative_progress;

    private CountPriceBean countPriceBean;
    private ShopPresenter presenter;
    private List<ShopBean.DataBean.ListBean> listBeans;
    private ShopAdapter shopAdapter;
    private ErMyliebiao erMyliebiao;
    private String priceString;

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

    }

    private void initView() {
        erMyliebiao = findViewById(R.id.expanable_list_view);
        check_all = findViewById(R.id.check_all);
        text_total = findViewById(R.id.text_total);
        text_buy = findViewById(R.id.text_buy);
        relative_progress = findViewById(R.id.relative_progress);
        linearLayout = findViewById(R.id.linear_bottom);

        presenter = new ShopPresenter(this);


        Map<String, String> map = new HashMap<>();
        presenter.getshop("product/getCarts", uid);

        check_all.setOnClickListener(this);
    }
    //判断登录状态
    @Override
    public void onResume() {
        super.onResume();

        relative_progress.setVisibility(View.VISIBLE);
        presenter.getshop("product/getCarts",uid);
    }


    public void shop(ShopBean shopBean) {

        //获取数据成功...隐藏
        relative_progress.setVisibility(View.GONE);

        List<ShopBean.DataBean> data = shopBean.getData();
        if (shopBean != null) {
            //cBean再设置奢配器之前需不需要改变数据
            //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(this, shopBean, handler, relative_progress, presenter);

            erMyliebiao.setAdapter(shopAdapter);

            //展开所有的组...expanableListView.expandGroup()
            for (int i = 0; i < shopBean.getData().size(); i++) {
                erMyliebiao.expandGroup(i);
            }
            //3.计算总价和商品的数量
            shopAdapter.sendPriceAndCount();

        } else {
            Toast.makeText(MainActivity.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;
        }
    }

    @Override
    public void onPointerCaptureChanged(boolean hasCapture) {

    }
}
CountPriceBean
package com.example.administrator.monthtwo.bean;

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;
    }
}
IMShopPresenter
package com.example.administrator.monthtwo.Presenter;

import com.example.administrator.monthtwo.bean.ShopBean;

public interface IMShopPresenter {
    void shop(ShopBean shopBean);
}
ShopPresenter
package com.example.administrator.monthtwo.Presenter;

import com.example.administrator.monthtwo.bean.ShopBean;

public class ShopPresenter implements IMShopPresenter{
    private ShopModel shopModel;
    private IMshopview iMshopview;

    public ShopPresenter(IMshopview iMshopview) {
        this.iMshopview=iMshopview;
        shopModel = new ShopModel(this);


    }

    public void getshop(String s, int uid) {
        shopModel.getshop(s,uid);

    }

    @Override
    public void shop(ShopBean shopBean) {
        iMshopview.shop(shopBean);

    }
}
OnItemClickListener
package com.example.administrator.monthtwo.model;

public interface OnItemClickListener {
    void onItemClick(int position);
}
ShopModel
package com.example.administrator.monthtwo.model;

import com.example.administrator.monthtwo.Presenter.IMShopPresenter;
import com.example.administrator.monthtwo.bean.ShopBean;

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

public class ShopModel {
    private IMShopPresenter imShopPresenter;

    public ShopModel(IMShopPresenter imShopPresenter) {
        this.imShopPresenter=imShopPresenter;

    }


    public void getshop(final String s, int uid) {

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

        map.put("uid", String.valueOf(uid));
        map.put("source","android");
        RetrofitManager.post(s, map, new BaseObserver<ShopBean>() {
            @Override
            public void success(ShopBean shopBean) {
                imShopPresenter.shop(shopBean);


            }

            @Override
            public void failure(int code) {

            }
        });
    }
}
IMshopview
package com.example.administrator.monthtwo.view;

import com.example.administrator.monthtwo.bean.ShopBean;

public interface IMshopview {
    void shop(ShopBean shopBean);
}
RetrofitManager
package com.example.administrator.monthtwo.httputils;

import java.util.Map;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;


public class RetrofitManager {
    public static OkHttpClient client = new OkHttpClient.Builder()
            .build();


    public static  ApiService  apiService = new Retrofit.Builder()
            .baseUrl("https://www.zhaoapi.cn/")


            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .addConverterFactory(ScalarsConverterFactory.create())
            .client(client)
            .build()
            .create(ApiService.class);


    public static void get(String url, Map<String,String> map, Observer observer){

        apiService.get(url,map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);

    }


    public static void post(String url, Map<String,String> map, Observer observer){
        apiService.post(url,map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }
}
ApiService
package com.example.administrator.monthtwo.httputils;


import java.util.Map;

import io.reactivex.Observable;
import retrofit2.http.FieldMap;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.GET;
import retrofit2.http.POST;
import retrofit2.http.QueryMap;
import retrofit2.http.Url;


public interface ApiService {

    @GET
    Observable<String> get(@Url String url, @QueryMap Map<String, String> map);
    @FormUrlEncoded
    @POST
    Observable<String> post(@Url String url, @FieldMap Map<String, String> map);
}
BaseObserver
package com.example.administrator.month.http;


import com.google.gson.Gson;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.net.SocketException;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import retrofit2.HttpException;


public abstract class BaseObserver<T> implements Observer<String> {


    @Override
    public void onSubscribe(Disposable d) {

    }

    @Override
    public void onNext(String s) {


        try {
            Type genType = getClass().getGenericSuperclass();
            Type[] params = ((ParameterizedType) genType).getActualTypeArguments();
            Class entityClass = (Class) params[0];
            Gson gson = new Gson();
            T t = (T) gson.fromJson(s, entityClass);
            success(t);

        } catch (Exception e) {
            failure(1001);
            e.printStackTrace();
        }


    }

    @Override
    public void onError(Throwable e) {
        try {
            if (e != null) {
                if (e instanceof HttpException) {
                    failure(HTTP_ERROR);
                } else if (e instanceof SocketException) {
                    failure(NET_WORK_ERROR);
                } else {
                    failure(UNKNOW_ERROR);
                }
            } else {
                failure(UNKNOW_ERROR);
            }
            e.printStackTrace();
        } catch (Exception e1) {
            failure(UNKNOW_ERROR);
            e1.printStackTrace();
        }


    }

    @Override
    public void onComplete() {

    }


    //

    /**
     * code
     * 1000 UNKNOW_ERROR 未知错误
     * 1001 json 转化异常  parse error
     * 1002 当前网络不可用     java.net.SocketException: Network is unreachable  超时
     * 1003 服务器不可用 401 402 403 500 502 503 504
     *
     * @param code
     */

    public static final int UNKNOW_ERROR = 1000;
    public static final int JSON_FORMAT_ERROR = 1001;
    public static final int NET_WORK_ERROR = 1002;
    public static final int HTTP_ERROR = 1003;


    public abstract void success(T t);

    public abstract void failure(int code);
}
ShopAdapter
package com.example.administrator.monthtwo.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 android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.administrator.monthtwo.Presenter.ShopPresenter;
import com.example.administrator.monthtwo.R;
import com.example.administrator.monthtwo.bean.CountPriceBean;
import com.example.administrator.monthtwo.bean.ShopBean;
import com.example.administrator.monthtwo.httputils.BaseObserver;
import com.example.administrator.monthtwo.httputils.RetrofitManager;
import com.example.administrator.monthtwo.model.OnItemClickListener;


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

public class ShopAdapter extends BaseExpandableListAdapter {
    private ShopPresenter presenter;
    private int uid=14538;
    private int childIndex;
    private int allIndex;
    private Context context;
    private ShopBean shopBean;
    private Handler handler;
    private RelativeLayout relative_progress;
    private OnItemClickListener onItemClickListener;


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


    @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.yijiliebiao,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","14538");
        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()));

        RetrofitManager.post("product/updateCarts", params, new BaseObserver<ShopBean>() {
            @Override
            public void success(ShopBean shopBean) {


                childIndex++;
                if (childIndex < dataBean.getList().size()) {
                    //再去更新下一条
                    updateAllChildInGroup(dataBean, checked);

                } else {//全都更新完成了....重新查询购物车

                    presenter.getshop("product/getCarts", uid);

                }
            }

            @Override
            public void failure(int code) {

            }
        });
    }

    @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.erjiliebiao,null);
            holder = new ChildHolder();

            holder.checkBox = view.findViewById(R.id.child_check);
            holder.text_title = view.findViewById(R.id.child_title);
            holder.img = 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.img);

        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","14538");
                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()));

                RetrofitManager.post("product/updateCarts", params, new BaseObserver<ShopBean>() {
                    @Override
                    public void success(ShopBean shopBean) {

                        presenter.getshop("product/getCarts",uid);

                    }

                    @Override
                    public void failure(int code) {

                    }
                });

            }
        });

        //加号
        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","14538");
                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));

                RetrofitManager.post("product/updateCarts", params, new BaseObserver<ShopBean>() {
                    @Override
                    public void success(ShopBean shopBean) {


                        presenter.getshop("product/getCarts", uid);

                    }
                    @Override
                    public void failure(int code) {

                    }
                });

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

                if (num == 1){
                    Toast.makeText(context,"最小数量为1", Toast.LENGTH_SHORT).show();
                    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","14538");
                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));

                RetrofitManager.post("product/updateCarts", params, new BaseObserver<ShopBean>() {
                    @Override
                    public void success(ShopBean shopBean) {


                        presenter.getshop("product/getCarts", uid);

                    }

                    @Override
                    public void failure(int code) {


                    }
                });

            }
        });
        //删除
        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", "14538");
                params.put("pid", String.valueOf(listBean.getPid()));


                RetrofitManager.post("product/deleteCart", params, new BaseObserver<ShopBean>() {
                    @Override
                    public void success(ShopBean shopBean) {


                        presenter.getshop("product/getCarts", uid);

                    }

                    @Override
                    public void failure(int code) {

                    }

                });
            }
        });



        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);
//        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","14538");
        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()));
        RetrofitManager.post("product/updateCarts", params, new BaseObserver<ShopBean>() {
            @Override
            public void success(ShopBean shopBean) {
                allIndex ++;

                if(allIndex < allList.size()){
                    updateAllChecked(allList,checked);
                }
                else{
                    presenter.getshop("product/getCarts", uid);
                }
            }
            @Override
            public void failure(int code) {
            }
        });
    }

    private class GroupHolder{
        CheckBox checkBox;
        TextView textView;
    }


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

    public void setOnItemClickListener(OnItemClickListener onItemClickListener){
        this.onItemClickListener=onItemClickListener;
    }
}

erjiliebiao.xml

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

    <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>
biankuang

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

yijiliebiao

<?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:id="@+id/group_check"
        android:layout_gravity="center_vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:text="111"
        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>
ErMyliebiao
package com.example.administrator.monthtwo.view;

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

public class ErMyliebiao extends ExpandableListView {

    public ErMyliebiao(Context context) {
        super(context);
    }

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

    public ErMyliebiao(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);
    }
}

最后记得在主购物车方法里将自定义二级列表再敲一遍就OK了。






猜你喜欢

转载自blog.csdn.net/qq_40857831/article/details/80519354