购物车需要的方法

application文件夹下//DashApplication

public class DashApplication extends Application {
    private static Context context;
    private static Handler handler;
    private static int mainId;
    public static boolean isLoginSuccess;//是否已经登录的状态

    @Override
    public void onCreate() {
        super.onCreate();

        //关于context----http://blog.youkuaiyun.com/lmj623565791/article/details/40481055
        context = getApplicationContext();
        //初始化handler
        handler = new Handler();
        //主线程的id
        mainId = Process.myTid();
    }

    /**
     * 对外提供了context
     * @return
     */
    public static Context getAppContext() {
        return context;
    }

    /**
     * 得到全局的handler
     * @return
     */
    public static Handler getAppHanler() {
        return handler;
    }

    /**
     * 获取主线程id
     * @return
     */
    public static int getMainThreadId() {
        return mainId;
    }
}


adapter文件夹下//MyAdapter

public class MyAdapter extends BaseExpandableListAdapter{
    private RelativeLayout relative_progress;
    private CartPresenter cartPresenter;
    private Handler handler;
    private CartBean cartBean;
    private Context context;
    private int size;
    private int childI;
    private int allSize;
    private int index;

    public MyAdapter(Context context, CartBean cartBean, Handler handler, CartPresenter cartPresenter, RelativeLayout relative_progress) {
        this.context = context;
        this.cartBean = cartBean;
        this.handler = handler;
        this.cartPresenter = cartPresenter;
        this.relative_progress = relative_progress;
    }

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

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

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

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return cartBean.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_item_layout,null);
            holder = new GroupHolder();

            holder.check_group = view.findViewById(R.id.check_group);
            holder.text_group = view.findViewById(R.id.text_group);

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

        final CartBean.DataBean dataBean = cartBean.getData().get(groupPosition);
        //赋值
        holder.check_group.setChecked(dataBean.isGroupChecked());
        holder.text_group.setText(dataBean.getSellerName());
        //组的点击事件...也要去请求更新的接口
        holder.check_group.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                relative_progress.setVisibility(View.VISIBLE);//显示

                size = dataBean.getList().size();
                childI = 0;
                updateAllInGroup(holder.check_group.isChecked(),dataBean);
            }
        });
        return view;
    }

    /**
     * 更新一组的状态
     * @param checked
     * @param dataBean
     */
    private void updateAllInGroup(final boolean checked, final CartBean.DataBean dataBean) {

        CartBean.DataBean.ListBean listBean = dataBean.getList().get(childI);//0

        //?uid=71&sellerid=1&pid=1&selected=0&num=10
        Map<String, String> params = new HashMap<>();
        params.put("uid","3690");
        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.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()){
                    childI = childI+1;//0,1,2...3
                    if (childI <size){

                        updateAllInGroup(checked,dataBean);
                    }else {
                        //所有的条目已经更新完成....再次请求查询购物车的数据
                        cartPresenter.getCartData(ApiUtil.cartUrl);
                    }
                }
            }
        });
    }

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

            holder.text_add = view.findViewById(R.id.text_add);
            holder.text_num = view.findViewById(R.id.text_num);
            holder.text_jian = view.findViewById(R.id.text_jian);
            holder.text_title = view.findViewById(R.id.text_title);
            holder.text_price = view.findViewById(R.id.text_price);
            holder.image_good = view.findViewById(R.id.image_good);
            holder.check_child = view.findViewById(R.id.check_child);
            holder.text_delete = view.findViewById(R.id.text_delete);

            view.setTag(holder);
        }else {
            holder = (ChildHolder) view.getTag();
        }
        //赋值
        final CartBean.DataBean.ListBean listBean = cartBean.getData().get(groupPosition).getList().get(childPosition);

        holder.text_num.setText(listBean.getNum()+"");//......注意
        holder.text_price.setText("¥"+listBean.getBargainPrice());
        holder.text_title.setText(listBean.getTitle());
        //listBean.getSelected().....0false,,,1true
        //设置checkBox选中状态
        holder.check_child.setChecked(listBean.getSelected()==0? false:true);

        /*implementation 'com.github.bumptech.glide:glide:4.4.0'
        annotationProcessor 'com.github.bumptech.glide:compiler:4.4.0'*/
        Glide.with(context).load(listBean.getImages().split("\\|")[0]).into(holder.image_good);

        //点击事件
        holder.check_child.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //点击的时候 更新当前条目选中的状态,,,更新完之后,请求查询购物车,重新展示数据

                updateChildChecked(listBean);
            }
        });

        //加号
        holder.text_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //请求更新的接口
                updateChildNum(listBean,true);
            }
        });

        //减号
        holder.text_jian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            if (listBean.getNum() == 1){
                return;
            }
            //更新数量,,,            updateChildNum(listBean,false);

            }
        });
        return view;
    }

    /**
     * 更新数量
     * @param listBean
     * @param
     */
    private void updateChildNum(CartBean.DataBean.ListBean listBean, boolean isAdded) {
        //一旦执行更新的操作,,,progressBar显示
        relative_progress.setVisibility(View.VISIBLE);

        //?uid=71&sellerid=1&pid=1&selected=0&num=10
        Map<String, String> params = new HashMap<>();
        params.put("uid","3690");
        params.put("sellerid", String.valueOf(listBean.getSellerid()));
        params.put("pid", String.valueOf(listBean.getPid()));
        params.put("selected", String.valueOf(listBean.getSelected()));

        if (isAdded){
            params.put("num", String.valueOf(listBean.getNum() + 1));
        }else {
            params.put("num", String.valueOf(listBean.getNum() - 1));
        }

        OkHttp3Util.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()){
                    cartPresenter.getCartData(ApiUtil.cartUrl);
                }
            }
        });
    }

    /**
     * 更新子条目 网络上的状态
     * @param listBean
     */
    private void updateChildChecked(CartBean.DataBean.ListBean listBean) {
        //一旦执行更新的操作,,,progressBar显示
        relative_progress.setVisibility(View.VISIBLE);

        //?uid=71&sellerid=1&pid=1&selected=0&num=10
        Map<String, String> params = new HashMap<>();
        params.put("uid","3690");
        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.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()){
                    cartPresenter.getCartData(ApiUtil.cartUrl);
                }
            }
        });
    }

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

    /**
     * 计算价格和数量 并发送显示
     */
    public void sendPriceAndCount() {
        double price = 0;
        int count = 0;

        //通过判断二级列表是否勾选,,,,计算价格数量
        for (int i=0;i<cartBean.getData().size();i++){

            for (int j = 0;j<cartBean.getData().get(i).getList().size();j++){
                if (cartBean.getData().get(i).getList().get(j).getSelected() == 1){
                    //价格是打折的价格...........
                    price += cartBean.getData().get(i).getList().get(j).getNum() * cartBean.getData().get(i).getList().get(j).getBargainPrice();
                    count += cartBean.getData().get(i).getList().get(j).getNum();
                }
            }
        }
        //精准的保留double的两位小数
        DecimalFormat decimalFormat = new DecimalFormat("#.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 setAllChildState(boolean checked) {
        //创建一个集合 装所有的子条目
        List<CartBean.DataBean.ListBean> allList = new ArrayList<>();

        for (int i=0;i<cartBean.getData().size();i++){
            for (int j=0;j<cartBean.getData().get(i).getList().size();j++){
                allList.add(cartBean.getData().get(i).getList().get(j));
            }
        }

        relative_progress.setVisibility(View.VISIBLE);

        allSize = allList.size();
        index = 0;

        //通过 递归 更新所有子条目的选中
        updateAllChild(allList,checked);
    }

    /**
     * 根据全选 跟新所有的子条目
     * @param allList
     * @param checked
     */
    private void updateAllChild(final List<CartBean.DataBean.ListBean> allList, final boolean checked) {
        CartBean.DataBean.ListBean listBean = allList.get(index);//0

        //跟新的操作
        //?uid=71&sellerid=1&pid=1&selected=0&num=10
        Map<String, String> params = new HashMap<>();
        params.put("uid","3690");
        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.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()){
                    index = index +1;//0,1,2......3
                    if (index < allSize){

                        updateAllChild(allList,checked);
                    }else {
                        //查询购物车
                        cartPresenter.getCartData(ApiUtil.cartUrl);
                    }
                }
            }
        });
    }


    private class GroupHolder{
        CheckBox check_group;
        TextView text_group;
    }

    private class ChildHolder{
        CheckBox check_child;
        ImageView image_good;
        TextView text_title;
        TextView text_price;
        TextView text_jian;
        TextView text_num;
        TextView text_add;
        TextView text_delete;
    }
}


custom文件夹下//CartExpanableListView

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

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

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

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

        super.onMeasure(widthMeasureSpec, height);
    }
}


bean文件夹下//CartBean

public class CartBean {
    private String msg;
    private String code;
    private List<DataBean> data;

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public List<DataBean> getData() {
        return data;
    }

    public void setData(List<DataBean> data) {
        this.data = data;
    }

    public static class DataBean {
        public boolean isGroupChecked() {
            return isGroupChecked;
        }

        public void setGroupChecked(boolean groupChecked) {
            isGroupChecked = groupChecked;
        }

        private boolean isGroupChecked;//一级列表是否选中

        private String sellerName;
        private String sellerid;
        private List<ListBean> list;

        public String getSellerName() {
            return sellerName;
        }

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

        public String getSellerid() {
            return sellerid;
        }

        public void setSellerid(String sellerid) {
            this.sellerid = sellerid;
        }

        public List<ListBean> getList() {
            return list;
        }

        public void setList(List<ListBean> list) {
            this.list = list;
        }

        public static class ListBean {
            private double bargainPrice;
            private String createtime;
            private String detailUrl;
            private String images;
            private int num;
            private int pid;
            private double price;
            private int pscid;

            private int selected;//判断二级是否选中,,,1选中,0未选中


            private int sellerid;
            private String subhead;
            private String title;

            public double getBargainPrice() {
                return bargainPrice;
            }

            public void setBargainPrice(double bargainPrice) {
                this.bargainPrice = bargainPrice;
            }

            public String getCreatetime() {
                return createtime;
            }

            public void setCreatetime(String createtime) {
                this.createtime = createtime;
            }

            public String getDetailUrl() {
                return detailUrl;
            }

            public void setDetailUrl(String detailUrl) {
                this.detailUrl = detailUrl;
            }

            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 int getPid() {
                return pid;
            }

            public void setPid(int pid) {
                this.pid = pid;
            }

            public double getPrice() {
                return price;
            }

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

            public int getPscid() {
                return pscid;
            }

            public void setPscid(int pscid) {
                this.pscid = pscid;
            }

            public int getSelected() {
                return selected;
            }

            public void setSelected(int selected) {
                this.selected = selected;
            }

            public int getSellerid() {
                return sellerid;
            }

            public void setSellerid(int sellerid) {
                this.sellerid = sellerid;
            }

            public String getSubhead() {
                return subhead;
            }

            public void setSubhead(String subhead) {
                this.subhead = subhead;
            }

            public String getTitle() {
                return title;
            }

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


//CountPriceBean

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;
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值