仿京东 购物车

1.购物车1级

package com.example.dell.jingdong201804.model.Shopping.bean;

/**
 * Created by dell on 2018/5/1.
 */

public class ShoppingDataBean<T> {

    private String msg;
    private String code;
    private T 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 T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}

2.购物车2级

package com.example.dell.jingdong201804.model.Shopping.bean;

/**
 * Created by dell on 2018/5/1.
 */

public class ShoppingDataBean2<T> {

    private String sellerName;
    private String sellerid;
    private T list;
    Boolean oneischeck;

    public Boolean getOneischeck() {
        return oneischeck;
    }

    public void setOneischeck(Boolean oneischeck) {
        this.oneischeck = oneischeck;
    }

    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 T getList() {
        return list;
    }

    public void setList(T list) {
        this.list = list;
    }
}

3.购物车3级

package com.example.dell.jingdong201804.model.Shopping.bean;

/**
 * Created by dell on 2018/5/1.
 */

public class ShoppingDataBean3 {

    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;
    private int sellerid;
    private String subhead;
    private String title;
    Boolean twoischeck;

    public Boolean getTwoischeck() {
        return twoischeck;
    }

    public void setTwoischeck(Boolean twoischeck) {
        this.twoischeck = twoischeck;
    }

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

4.购物车M层

package com.example.dell.jingdong201804.model.Shopping;

import com.example.dell.jingdong201804.presenter.Shopping.ShoppingDataPresenter;

/**
 * Created by dell on 2018/4/28.
 */

public interface ShoppingModel {
    //请求数据的方法
    void getShoppingModelData(String url,String uid, ShoppingDataPresenter presenter);

}

5.购物车M层实现

package com.example.dell.jingdong201804.model.Shopping;

import com.example.dell.jingdong201804.model.Shopping.bean.ShoppingDataBean;
import com.example.dell.jingdong201804.model.Shopping.bean.ShoppingDataBean2;
import com.example.dell.jingdong201804.model.Shopping.bean.ShoppingDataBean3;
import com.example.dell.jingdong201804.presenter.Shopping.MyShoppingDataPresenter;
import com.example.dell.jingdong201804.presenter.Shopping.ShoppingDataPresenter;
import com.example.dell.jingdong201804.utils.RetrofitUtils;

import java.util.List;

import io.reactivex.Flowable;

/**
 * Created by dell on 2018/4/28.
 */

public class MyShoppingModel implements ShoppingModel{

    @Override
    public void getShoppingModelData(String url, String uid, ShoppingDataPresenter presenter) {
        Flowable<ShoppingDataBean<List<ShoppingDataBean2<List<ShoppingDataBean3>>>>> shopping = RetrofitUtils.getInstance(url).getApiService().getShopping(uid);

        //调用P层方法 将数据传给P        presenter.Success(shopping);
    }
}

6.购物车添加M层

package com.example.dell.jingdong201804.model.Shopping;

import com.example.dell.jingdong201804.presenter.Shopping.ShoppingAddDataPresenter;

/**
 * Created by dell on 2018/4/28.
 */

public interface ShoppingAddModel {
    void getShoppingAddModel(String url, String uid,String pid, ShoppingAddDataPresenter presenter);

}

7.购物车添加M层实现

package com.example.dell.jingdong201804.model.Shopping;

import com.example.dell.jingdong201804.model.Shopping.bean.ShoppingDataBean;
import com.example.dell.jingdong201804.model.Shopping.bean.ShoppingDataBean2;
import com.example.dell.jingdong201804.model.Shopping.bean.ShoppingDataBean3;
import com.example.dell.jingdong201804.presenter.Shopping.MyShoppingDataPresenter;
import com.example.dell.jingdong201804.presenter.Shopping.ShoppingAddDataPresenter;
import com.example.dell.jingdong201804.utils.RetrofitUtils;

import java.util.List;

import io.reactivex.Flowable;

/**
 * Created by dell on 2018/4/28.
 */

public class MyShoppingAddModel implements ShoppingAddModel{

    @Override
    public void getShoppingAddModel(String url, String uid, String pid, ShoppingAddDataPresenter presenter) {
        Flowable<ShoppingDataBean<List<ShoppingDataBean2<List<ShoppingDataBean3>>>>> shoppingAdd = RetrofitUtils.getInstance(url).getApiService().getShoppingAdd(uid, pid);
        presenter.Success(shoppingAdd);
    }
}

8.购物车P层

package com.example.dell.jingdong201804.presenter.Shopping;

import com.example.dell.jingdong201804.model.Shopping.bean.ShoppingDataBean;
import com.example.dell.jingdong201804.model.Shopping.bean.ShoppingDataBean2;
import com.example.dell.jingdong201804.model.Shopping.bean.ShoppingDataBean3;

import java.util.List;

import io.reactivex.Flowable;

/**
 * Created by dell on 2018/4/28.
 */

public interface ShoppingDataPresenter {
    void Success(Flowable<ShoppingDataBean<List<ShoppingDataBean2<List<ShoppingDataBean3>>>>> shopping);
    void Error();
}
9.购物车P层实现
package com.example.dell.jingdong201804.presenter.Shopping;

import android.util.Log;

import com.example.dell.jingdong201804.model.Shopping.MyShoppingModel;
import com.example.dell.jingdong201804.model.Shopping.bean.ShoppingDataBean;
import com.example.dell.jingdong201804.model.Shopping.bean.ShoppingDataBean2;
import com.example.dell.jingdong201804.model.Shopping.bean.ShoppingDataBean3;
import com.example.dell.jingdong201804.view.Shopping.ShoppingDataView;

import java.util.List;

import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import io.reactivex.subscribers.DefaultSubscriber;
import io.reactivex.subscribers.DisposableSubscriber;

/**
 * Created by dell on 2018/4/28.
 */

public class MyShoppingDataPresenter implements ShoppingDataPresenter {
    private MyShoppingModel model;
    ShoppingDataView dataView;
    public MyShoppingDataPresenter(ShoppingDataView dataView) {
        this.dataView=dataView;
        //实例化M层 将P层传给M        model = new MyShoppingModel();
    }


    @Override
    public void Success(Flowable<ShoppingDataBean<List<ShoppingDataBean2<List<ShoppingDataBean3>>>>> shopping) {
                shopping.subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new DisposableSubscriber<ShoppingDataBean<List<ShoppingDataBean2<List<ShoppingDataBean3>>>>>() {
                            @Override
                            public void onNext(ShoppingDataBean<List<ShoppingDataBean2<List<ShoppingDataBean3>>>> listShoppingDataBean) {
                                Log.e("++++++", "显示数据: "+listShoppingDataBean.getMsg() );
                                    if(listShoppingDataBean!=null){
                                        dataView.toBackHome(listShoppingDataBean);
                                    }
                            }

                            @Override
                            public void onError(Throwable t) {

                            }

                            @Override
                            public void onComplete() {

                            }
                        });
    }

    @Override
    public void Error() {

    }


    //创建接收url uid的方法
    public void getShopDatas(String url, String uid) {
        model.getShoppingModelData(url,uid,this);

    }

    //销毁V层 防止内存泄露
    public void daadData() {
        if(dataView!=null){
            dataView=null;
        }
    }



}

10.购物车添加P层

package com.example.dell.jingdong201804.presenter.Shopping;

import com.example.dell.jingdong201804.model.Shopping.bean.ShoppingDataBean;
import com.example.dell.jingdong201804.model.Shopping.bean.ShoppingDataBean2;
import com.example.dell.jingdong201804.model.Shopping.bean.ShoppingDataBean3;

import java.util.List;

import io.reactivex.Flowable;

/**
 * Created by dell on 2018/4/28.
 */

public interface ShoppingAddDataPresenter {
    void Success(Flowable<ShoppingDataBean<List<ShoppingDataBean2<List<ShoppingDataBean3>>>>> shoppingAdd);
    void Error();

}

11.购物车添加P层实现

package com.example.dell.jingdong201804.presenter.Shopping;

import com.example.dell.jingdong201804.model.Shopping.MyShoppingAddModel;
import com.example.dell.jingdong201804.model.Shopping.bean.ShoppingDataBean;
import com.example.dell.jingdong201804.model.Shopping.bean.ShoppingDataBean2;
import com.example.dell.jingdong201804.model.Shopping.bean.ShoppingDataBean3;
import com.example.dell.jingdong201804.view.Shopping.ShoppingAddDataView;

import java.util.List;

import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import io.reactivex.subscribers.DisposableSubscriber;

/**
 * Created by dell on 2018/4/28.
 */

public class MyShoppingAddDataPresenter implements ShoppingAddDataPresenter {
    ShoppingAddDataView dataView;
    private final MyShoppingAddModel model;

    public MyShoppingAddDataPresenter(ShoppingAddDataView dataView){
        this.dataView=dataView;

        model = new MyShoppingAddModel();
    }

    @Override
    public void Success(Flowable<ShoppingDataBean<List<ShoppingDataBean2<List<ShoppingDataBean3>>>>> shoppingAdd) {
            shoppingAdd.subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new DisposableSubscriber<ShoppingDataBean<List<ShoppingDataBean2<List<ShoppingDataBean3>>>>>() {
                        @Override
                        public void onNext(ShoppingDataBean<List<ShoppingDataBean2<List<ShoppingDataBean3>>>> listShoppingDataBean) {
                            dataView.toBackHome(listShoppingDataBean);
                        }

                        @Override
                        public void onError(Throwable t) {

                        }

                        @Override
                        public void onComplete() {

                        }
                    });
    }

    @Override
    public void Error() {

    }


    //创建接收需求值的方法
    public void getShoppingAdd(String url,String uid,String pid){
            model.getShoppingAddModel(url,uid,pid,this);
    }


}

12.购物车V层

package com.example.dell.jingdong201804.view.Shopping;

import com.example.dell.jingdong201804.model.Shopping.bean.ShoppingDataBean;
import com.example.dell.jingdong201804.model.Shopping.bean.ShoppingDataBean2;
import com.example.dell.jingdong201804.model.Shopping.bean.ShoppingDataBean3;

import java.util.List;

/**
 * Created by dell on 2018/4/28.
 */

public interface ShoppingDataView {
    void toBackHome(ShoppingDataBean<List<ShoppingDataBean2<List<ShoppingDataBean3>>>> listShoppingDataBean);
}

13.购物车V层实现

package com.example.dell.jingdong201804.view.Shopping;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ExpandableListView;
import android.widget.TextView;
import android.widget.Toast;
import com.example.dell.jingdong201804.R;
import com.example.dell.jingdong201804.adapter.Shopping.ShoppingExListAdapter;
import com.example.dell.jingdong201804.adapter.ShouYe.ShouYeTJAdapter;
import com.example.dell.jingdong201804.model.Shopping.bean.ShoppingDataBean;
import com.example.dell.jingdong201804.model.Shopping.bean.ShoppingDataBean2;
import com.example.dell.jingdong201804.model.Shopping.bean.ShoppingDataBean3;
import com.example.dell.jingdong201804.model.ShouYe.ShouYeDataBean.ShouYeDataBean;
import com.example.dell.jingdong201804.presenter.Shopping.MyShoppingDataPresenter;
import com.example.dell.jingdong201804.presenter.ShouYe.MyShouYeDataPrsenter;
import com.example.dell.jingdong201804.view.ShouYe.ShouYeDataView;
import com.example.dell.jingdong201804.view.ShouYe.ShouYeXQActivity;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import static android.content.Context.MODE_PRIVATE;


public class Fragment04 extends Fragment implements ShoppingDataView, ShouYeDataView,ShouYeTJAdapter.OnClickListener,ShoppingExListAdapter.OnClickListener {

    String url = "https://www.zhaoapi.cn/";
    @BindView(R.id.sp_elv)
    ExpandableListView sp_Elv;
    @BindView(R.id.sp_box)
    CheckBox sp_Box;
    @BindView(R.id.sp_price)
    TextView sp_Price;
    @BindView(R.id.sp_btn)
    Button sp_Btn;
    Unbinder unbinder;
    @BindView(R.id.sp_rcv)
    RecyclerView sp_Rcv;
    private SharedPreferences sharedPreferences;
    private String uid;
    private MyShoppingDataPresenter presenter;

    //创建一级列表集合
    List<ShoppingDataBean2> groupList = new ArrayList<>();
    //创建二级列表集合
    List<List<ShoppingDataBean3>> childrenList = new ArrayList<>();
    private List<ShouYeDataBean.TuijianBean.ListBean> list;
    private ShoppingExListAdapter exListAdapter;
    private List<ShoppingDataBean3> list1;
    private List<ShoppingDataBean2<List<ShoppingDataBean3>>> data;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_fragment04, container, false);
        unbinder = ButterKnife.bind(this, view);

        //得到SharedPreferences
        sharedPreferences = getActivity().getSharedPreferences("Person", MODE_PRIVATE);
        int uidd = sharedPreferences.getInt("uid", 0);

        if (uidd != 0) {
            //实例化P层 将所需值传给P            presenter = new MyShoppingDataPresenter(this);
            uid = String.valueOf(uidd);
            presenter.getShopDatas(url, uid);
        } else {
            Toast.makeText(getActivity(), "请先登录", Toast.LENGTH_SHORT).show();
        }

        //实例化首页P层 将所需值传给P        MyShouYeDataPrsenter shouYeDataPrsenter = new MyShouYeDataPrsenter(this);
        shouYeDataPrsenter.ShouYeUrl(url);

        return view;
    }


    //得到购物车信息
    @Override
    public void toBackHome(ShoppingDataBean<List<ShoppingDataBean2<List<ShoppingDataBean3>>>> listShoppingDataBean) {
        Log.e("购物车信息", "toBackHome: " + listShoppingDataBean.getData().size());

        //得到一级列表数据
        data = listShoppingDataBean.getData();

        groupList.clear();//清空原集合中数据
        groupList.addAll(data);//加入一级列表集合

        childrenList.clear();//清空原集合中数据
        //循环一级列表数据 得到第二级列表数据
        for (int i = 0; i < data.size(); i++) {
            list1 = data.get(i).getList();

            childrenList.add(list1);//加入二级列表集合
        }

        getExListAdpater();

        //将一级列表的展开标志隐藏
        sp_Elv.setGroupIndicator(null);

        //设置复选框初始全部为false
        for (int a = 0; a < data.size(); a++) {
            ShoppingDataBean2<List<ShoppingDataBean3>> listShoppingDataBean2 = data.get(a);
            listShoppingDataBean2.setOneischeck(false);
            List<ShoppingDataBean3> list = listShoppingDataBean2.getList();
            for (int i = 0; i <list.size() ; i++) {
                list.get(i).setTwoischeck(false);
            }

            //将二级列表展开
            sp_Elv.expandGroup(a);
        }

        //全选、反选
        sp_Box.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //若选中 则将一级二级复选框全部设置为选中
                if(sp_Box.isChecked()){
                    for (int i = 0; i <data.size() ; i++) {
                        data.get(i).setOneischeck(true);
                        List<ShoppingDataBean3> list = data.get(i).getList();
                        for (int j = 0; j <list.size() ; j++) {
                            list.get(j).setTwoischeck(true);
                        }
                    }

                }else{
                    //若未选中 则将一级二级复选框全部设置为未选中
                    for (int i = 0; i <data.size() ; i++) {
                        data.get(i).setOneischeck(false);
                        List<ShoppingDataBean3> list = data.get(i).getList();
                        for (int j = 0; j <list.size() ; j++) {
                            list.get(j).setTwoischeck(false);
                        }
                    }
                }
                //计算价钱
                setPrices();
            }
        });


    }

    private void getExListAdpater(){
        if(exListAdapter == null){
            //给二级列表设置适配器
            exListAdapter = new ShoppingExListAdapter(getActivity(), groupList, childrenList);
            sp_Elv.setAdapter(exListAdapter);

            //调用适配器中的方法
            exListAdapter.SetShopping(Fragment04.this);
        }else{
            exListAdapter.notifyDataSetChanged();
        }
    }

    //得到为你推荐的数据
    @Override
    public void toBackHome(ShouYeDataBean shouYeDataBean) {

        Log.e("+++购物车", "toBackHome: " + shouYeDataBean.getMsg());
        list = shouYeDataBean.getTuijian().getList();

        sp_Rcv.setLayoutManager(new GridLayoutManager(getActivity(), 2));

        //设置适配器
        ShouYeTJAdapter shouYeTJAdapter = new ShouYeTJAdapter(getActivity(), list);
        sp_Rcv.setAdapter(shouYeTJAdapter);
        //调用适配器中的点击事件
        shouYeTJAdapter.setOnClick(Fragment04.this);

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }



    //重写适配器中的点击事件
    @Override
    public void OnClickitem(int position) {
        Toast.makeText(getActivity(), "点击了" + position, Toast.LENGTH_SHORT).show();
        //实现跳转到详情页面
        Intent intent = new Intent(getActivity(), ShouYeXQActivity.class);
        //传入相应的页面pid
        int pid = list.get(position).getPid();
        intent.putExtra("pid", String.valueOf(pid));
        //跳转
        startActivity(intent);

    }


    //判断fragment的隐藏,没有隐藏则重新请求数据
    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            presenter.getShopDatas(url, uid);
        }
    }


    //    @Override
    public void addNum(int i, int i1) {
        //得到当前数量
        int num = data.get(i).getList().get(i1).getNum();
        Log.e("++++", "addNum: " + num);

        //点击时加1
        num++;
        //将加后的数量设置进集合中
        data.get(i).getList().get(i1).setNum(num);

        setPrices();

    }


    //    @Override
    public void jianNum(int i, int i1) {
        //得到当前数量
        int num = data.get(i).getList().get(i1).getNum();
        Log.e("++++", "jianNum: " + num);

        //点击时加1
        num--;
        //将加后的数量设置进集合中
        data.get(i).getList().get(i1).setNum(num);

        setPrices();



    }

    //一级复选框选中
    @Override
    public void groupCheck(int i, boolean isCheck) {
        ShoppingDataBean2<List<ShoppingDataBean3>> listShoppingDataBean2 = data.get(i);
        listShoppingDataBean2.setOneischeck(isCheck);
        List<ShoppingDataBean3> list = listShoppingDataBean2.getList();
        for (int j = 0; j <list.size() ; j++) {
          list.get(j).setTwoischeck(isCheck);

        }

        //改变总价
        setPrices();

    }

    //二级复选框选中
    @Override
    public void childCheck(int i, int i1, boolean isCheck) {
        ShoppingDataBean2<List<ShoppingDataBean3>> listShoppingDataBean2 = data.get(i);
        listShoppingDataBean2.getList().get(i1).setTwoischeck(isCheck);

        boolean b = isAllChlid(i);
        if(b){
            data.get(i).setOneischeck(true);
        }else{
            data.get(i).setOneischeck(false);
        }

        //改变总价
        setPrices();
    }


    //调用改变总价的方法
    private void setPrices() {
        //初始化总价为0
        int num = 0;
        int prices = 0;
        for (int a = 0; a < data.size(); a++) {
            //得到bean
            List<ShoppingDataBean3> list = data.get(a).getList();

            for (int b = 0; b < list.size(); b++) {

                ShoppingDataBean3 shoppingDataBean3 = list.get(b);

                //判断若选中 总价+=数量*单价
                if (shoppingDataBean3.getTwoischeck()) {
                    prices += shoppingDataBean3.getNum() * shoppingDataBean3.getPrice();
                    num += shoppingDataBean3.getNum();
                }
            }

        }

        //将最后的总价进行四舍五入 然后保留小数后2        BigDecimal bg = new BigDecimal(prices);
        double f1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();

        //将总价设置给显示控件
        sp_Price.setText("总价:" + f1);
        sp_Btn.setText("去结算("+num+")");

        getExListAdpater();
    }

    //判断二级数据是否全部选中
    private boolean isAllChlid(int i){
        List<ShoppingDataBean3> list = data.get(i).getList();
        for (int j = 0; j <list.size() ; j++) {
            ShoppingDataBean3 shoppingDataBean3 = list.get(j);
            if(!shoppingDataBean3.getTwoischeck()){
                return false;
            }
        }
        return true;
    }



}

14.自定义二级列表

package com.example.dell.jingdong201804.view.Shopping;

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

/**
 * Created by dell on 2018/5/3.
 */

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

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

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

15.购物车添加V层

package com.example.dell.jingdong201804.view.Shopping;

import com.example.dell.jingdong201804.model.Shopping.bean.ShoppingDataBean;
import com.example.dell.jingdong201804.model.Shopping.bean.ShoppingDataBean2;
import com.example.dell.jingdong201804.model.Shopping.bean.ShoppingDataBean3;

import java.util.List;

/**
 * Created by dell on 2018/5/2.
 */

public interface ShoppingAddDataView {
    void toBackHome(ShoppingDataBean<List<ShoppingDataBean2<List<ShoppingDataBean3>>>> listShoppingDataBean);
}

16.购物车朱主布局

<?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"
    tools:context="com.example.dell.jingdong201804.view.Shopping.Fragment04">


            <RelativeLayout
                android:layout_alignParentTop="true"
                android:id="@+id/sp_layout"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:background="#c0c0c0"
                android:padding="10dp">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="购物车"
                    android:textStyle="bold"
                    android:layout_centerInParent="true"/>
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="编辑"
                    android:layout_alignParentRight="true"
                    android:layout_centerVertical="true"/>
            </RelativeLayout>


        <!--ScrollView 支持上拉下拉  ScrollView只能有1个子孩子-->
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/sp_layout"
       >

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

            <com.example.dell.jingdong201804.view.Shopping.MyExpandableListView
                android:id="@+id/sp_elv"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"></com.example.dell.jingdong201804.view.Shopping.MyExpandableListView>

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/sy_tj" />

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

        </LinearLayout>


    </ScrollView>
    <RelativeLayout
        android:layout_alignParentBottom="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#c0c0c0">

        <CheckBox
            android:id="@+id/sp_box"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:text="全选" />

        <TextView
            android:id="@+id/sp_price"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="合计:520.00" />

        <Button
            android:id="@+id/sp_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:text="去结算" />

    </RelativeLayout>
</RelativeLayout>

17.购物车一级

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

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:padding="10dp">
        <CheckBox
            android:id="@+id/sp_elv_group_box"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"/>
        <TextView
            android:id="@+id/sp_elv_group_sellerName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/sp_elv_group_box"
            android:layout_centerVertical="true"
            android:layout_marginLeft="20dp"
            android:text="商家23"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:textColor="#f00"
            android:text="优惠券"/>
    </RelativeLayout>


</LinearLayout>

18.购物车二级

<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
        android:layout_height="150dp"
        android:orientation="horizontal"
        android:padding="10dp">

        <CheckBox
            android:id="@+id/sp_elv_chlid_box"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"/>
        <ImageView
            android:id="@+id/sp_elv_chlid_img"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_marginLeft="10dp"
            android:src="@mipmap/ic_launcher"/>
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:orientation="vertical">

            <TextView
                android:id="@+id/sp_elv_chlid_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="全球购 新款Apple MacBook Pro 苹果笔记本电脑 银色VP213英寸Bar i5/8G/256G"/>

            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true">
                <TextView
                    android:id="@+id/sp_elv_chlid_price"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textColor="#f00"
                    android:layout_centerVertical="true"
                    android:text="888.0"/>

                <include layout="@layout/sp_elv_chlid_tu"
                    android:layout_width="wrap_content"
                    android:layout_height="50dp"
                    android:layout_alignParentRight="true"/>

            </RelativeLayout>
        </LinearLayout>

    </LinearLayout>



</LinearLayout>

19.购物车二级引入

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="50dp">
    <Button
        android:id="@+id/sp_elv_chlid_jian"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:text=" - "/>
    <TextView
        android:id="@+id/sp_elv_chlid_num"
        android:layout_width="30dp"
        android:layout_height="50dp"
        android:layout_marginLeft="5dp"
        android:gravity="center"
        android:text="1"/>
    <Button
        android:id="@+id/sp_elv_chlid_add"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:text=" + "/>
</LinearLayout>

20.购物车的适配器

package com.example.dell.jingdong201804.adapter.Shopping;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.example.dell.jingdong201804.R;
import com.example.dell.jingdong201804.model.Shopping.bean.ShoppingDataBean2;
import com.example.dell.jingdong201804.model.Shopping.bean.ShoppingDataBean3;
import java.util.List;

/**
 * Created by dell on 2018/4/28.
 */

public class ShoppingExListAdapter extends BaseExpandableListAdapter {
    Context context;
    List<ShoppingDataBean2> groupList;
    List<List<ShoppingDataBean3>> childrenList;
    private List<ShoppingDataBean3> dataBean3s;
    private ShoppingDataBean2 dataBean2;
    private ShoppingDataBean3 dataBean3;

    public ShoppingExListAdapter(Context context, List<ShoppingDataBean2> groupList, List<List<ShoppingDataBean3>> childrenList) {
            this.context=context;
            this.childrenList=childrenList;
            this.groupList=groupList;
    }


    //  获得父项的数量
    @Override
    public int getGroupCount() {
        return groupList.size();
    }
    //  获得某个父项的子项数目
    @Override
    public int getChildrenCount(int i) {
        return childrenList.get(i).size();
    }
    //  获得某个父项
    @Override
    public Object getGroup(int i) {
        return groupList.get(i);
    }
    //  获得某个父项中的某个子项
    @Override
    public Object getChild(int i, int i1) {
        return childrenList.get(i).get(i1);
    }

    //  获得某个父项的id
    @Override
    public long getGroupId(int i) {
        return i;
    }
    //  获得某个父项的某个子项的id
    @Override
    public long getChildId(int i, int i1) {
        return i1;
    }
    //  按函数的名字来理解应该是是否具有稳定的id,这个方法目前一直都是返回false,没有去改动过
    @Override
    public boolean hasStableIds() {
        return false;
    }
    //  获得父项显示的view
    @Override
    public View getGroupView(final int i, boolean b, View view, ViewGroup viewGroup) {
        final ViewHolderGroup holderGroup;
        if(view==null){
            view = View.inflate(context, R.layout.sp_elv_group_item, null);
            holderGroup = new ViewHolderGroup();
            holderGroup.sp_elv_group_box = view.findViewById(R.id.sp_elv_group_box);
            holderGroup.sp_elv_group_sellerName = view.findViewById(R.id.sp_elv_group_sellerName);
            view.setTag(holderGroup);
        }else{
            holderGroup = (ViewHolderGroup) view.getTag();
        }
        final ShoppingDataBean2 shoppingDataBean2 = groupList.get(i);
        holderGroup.sp_elv_group_box.setChecked(shoppingDataBean2.getOneischeck());
        holderGroup.sp_elv_group_sellerName.setText(shoppingDataBean2.getSellerName());

        //设置监听 实现一级复选框选择功能
        holderGroup.sp_elv_group_box.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onClickListener.groupCheck(i,holderGroup.sp_elv_group_box.isChecked());
            }
        });

        return view;
    }
    //  获得子项显示的view
    @Override
    public View getChildView(final int i, final int i1, boolean b, View view, ViewGroup viewGroup) {
        final ViewHolderChild holderChild;
        if(view==null){
            view = View.inflate(context, R.layout.sp_elv_chlid_item, null);
            holderChild = new ViewHolderChild();
            holderChild.sp_elv_chlid_box=view.findViewById(R.id.sp_elv_chlid_box);
            holderChild.sp_elv_chlid_img=view.findViewById(R.id.sp_elv_chlid_img);
            holderChild.sp_elv_chlid_title=view.findViewById(R.id.sp_elv_chlid_title);
            holderChild.sp_elv_chlid_price=view.findViewById(R.id.sp_elv_chlid_price);
            holderChild.sp_elv_chlid_jian=view.findViewById(R.id.sp_elv_chlid_jian);
            holderChild.sp_elv_chlid_num=view.findViewById(R.id.sp_elv_chlid_num);
            holderChild.sp_elv_chlid_add=view.findViewById(R.id.sp_elv_chlid_add);
            view.setTag(holderChild);
        }else{
            holderChild = (ViewHolderChild) view.getTag();
        }
        final ShoppingDataBean3 shoppingDataBean3 = childrenList.get(i).get(i1);
        String[] split = shoppingDataBean3.getImages().split("\\|");
        Glide.with(context).load(split[0]).into(holderChild.sp_elv_chlid_img);
        holderChild.sp_elv_chlid_title.setText(shoppingDataBean3.getTitle());
        holderChild.sp_elv_chlid_price.setText(""+shoppingDataBean3.getPrice());
        holderChild.sp_elv_chlid_num.setText(""+shoppingDataBean3.getNum());
        holderChild.sp_elv_chlid_box.setChecked(shoppingDataBean3.getTwoischeck());

        //设置监听 实现加的功能
        holderChild.sp_elv_chlid_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //调用接口中的加方法
                onClickListener.addNum(i,i1);
            }
        });

        //设置监听 实现减的功能
       holderChild.sp_elv_chlid_jian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //调用接口中的减方法
                onClickListener.jianNum(i,i1);
            }
        });

        //设置监听 实现二级复选框选择功能
        holderChild.sp_elv_chlid_box.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //调用接口中的全部选中的方法
                onClickListener.childCheck(i,i1,holderChild.sp_elv_chlid_box.isChecked());
            }
        });


        return view;
    }
    //  子项是否可选中,如果需要设置子项的点击事件,需要返回true
    @Override
    public boolean isChildSelectable(int i, int i1) {
        return true;
    }

    class ViewHolderGroup {
        CheckBox sp_elv_group_box;
        TextView sp_elv_group_sellerName;
    }

    class ViewHolderChild {

        CheckBox sp_elv_chlid_box;
        ImageView sp_elv_chlid_img;
        TextView sp_elv_chlid_title;
        TextView sp_elv_chlid_price;
        Button sp_elv_chlid_jian;
        TextView sp_elv_chlid_num;
        Button sp_elv_chlid_add;

    }

    //创建方法 用于调用接口
    OnClickListener onClickListener;
    public void SetShopping(OnClickListener onClickListener){
        this.onClickListener=onClickListener;
    }

    //创建接口
    public interface OnClickListener{

        //定义实现减数量、加数量的方法
        void addNum(int i,int i1);//        void jianNum(int i,int i1);//        void groupCheck(int i,boolean isCheck);//一级复选框
        void childCheck(int i,int i1,boolean isCheck);//二级复选框
        //void childDelete(int i,int i1);//删除

    }

}

21.分类列表的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="com.example.dell.jingdong201804.view.FenLei.FenLeiLBActivity">

  <RelativeLayout
      android:layout_width="match_parent"
      android:layout_height="50dp"
      android:background="#c0c0c0">
      <TextView
          android:id="@+id/fl_lb_fh"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="返回"
          android:layout_marginLeft="10dp"
          android:layout_centerVertical="true"/>
      <TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="列表信息"
          android:textStyle="bold"
          android:layout_centerInParent="true"/>
  </RelativeLayout>

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

</LinearLayout>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值