天天看點

購物車加訂單

implementation 'com.squareup.okhttp3:okhttp:3.9.1'
implementation 'com.google.code.gson:gson:2.8.+'
compile 'com.github.bumptech.glide:glide:3.6.1'
implementation 'com.android.support:recyclerview-v7:26.1.+'      

這些是需要用到的依賴 還有别忘了在加 網絡權限哦

首先寫OKHTTP 封裝的時候要實作一個接口

OnFinishListener 

/**
     * 定義一個接口
     */
    public interface OnFinishListener {


        void onFailed(String msg);

        void onSuccess(Object obj);


    }
      

然後寫OKHTTP的封裝類

OkHttpUtils       
import android.os.Handler;

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

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * Created by rookie on 2018/1/3.
 */

public class OkHttpUtils {

    private static OkHttpUtils OK_HTTP_UTILS = null;
    //定義一個Handler
    private static Handler handler = new Handler();

    private OkHttpUtils() {
    }


    /**
     * 得到OkHttpUtils執行個體對象
     *
     * @return
     */
    public static OkHttpUtils getInstance() {

        if (null == OK_HTTP_UTILS) {

            synchronized (OkHttpUtils.class) {

                if (null == OK_HTTP_UTILS) {
                    OK_HTTP_UTILS = new OkHttpUtils();
                }
            }

        }
        return OK_HTTP_UTILS;

    }

    /**
     * Get請求
     *
     * @param path http://www.baidu.com?key=value&key=value
     * @param onFinishListener
     */
    public void doGet(String path,Map<String,String> map,final OnFinishListener onFinishListener) {

        //Http:/www.baidu.com?key=ddd&
        StringBuffer sb = new StringBuffer();
        sb.append(path);
        //判斷path是否包含一個
        if(sb.indexOf("?") != -1){

            //判斷"?"是否在最後一個
            if(sb.indexOf("?") != sb.length() -1){
                sb.append("&");
            }

        }else{
            sb.append("?");
        }

        //周遊map集合中所有請求參數
        for (Map.Entry<String, String> entry: map.entrySet()){
            sb.append(entry.getKey())
                    .append("=")
                    .append(entry.getValue())
                    .append("&");
        }

        //判斷get請求路徑最後是否包含一個"&"
        if(sb.lastIndexOf("&") != -1){
            sb.deleteCharAt(sb.length() - 1);
        }


        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                //添加應用攔截器
                .addInterceptor(new CommonParamsInterceptor())
                //.addNetworkInterceptor() //添加網絡攔截器
                .build();

        //建構請求項
        Request request = new Request.Builder()
                .get()
                .url(sb.toString())
                .build();

        Call call = okHttpClient.newCall(request);
        //execute 子線程  enqueue //
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, final IOException e) {
                //請求失敗

                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        onFinishListener.onFailed(e.getMessage());
                    }
                });


            }

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

                //得到伺服器的響應結果
                final String result = response.body().string();

                //請求成功
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        //主線程當中執行
                        onFinishListener.onSuccess(result);
                    }
                });
            }
        });

    }

}


然後寫帶有攔截器的類        
CommonParamsInterceptor
      
import java.io.IOException;

import okhttp3.FormBody;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * 自定義一個攔截器,每次請求之前攜帶公共參數
 *
 * 應用層
 *
 */

public class CommonParamsInterceptor implements Interceptor {


    @Override
    public Response intercept(Chain chain) throws IOException {
        //得到原始的請求對象
        Request request = chain.request();
        //得到使用者所使用的請求方式
        String method = request.method();

        if("GET".equals(method)){
            //url
            //原始的請求接口
            String oldUrl = request.url().toString();

            //拼接成一個新的URL
            String newUrl = oldUrl + "&source=android";

            //重新建構請求體
            request = new Request.Builder()
                    .url(newUrl)
                    .build();
        }else if("POST".equals(method)){
            //得到原始的url
            String oldUrl = request.url().toString();
            //得到原有的請求參數
            FormBody oldBody = (FormBody) request.body();//1 2 3

            //新的建構項
            FormBody.Builder builder = new FormBody.Builder();
            for (int i =0 ; i < oldBody.size(); ++i){
                //取出相關請求參數(原有的)
                String name = oldBody.name(i);
                String value = oldBody.value(i);
                //将原始的參數拼裝到新的建構體當中
                builder.add(name,value);
            }

            //拼裝我們的公共參數
            builder.add("source","android");

            request = new Request.Builder()
                    .url(oldUrl)
                    .post(builder.build())
                    .build();


        }
        //重新發送請求
        return chain.proceed(request);
    }
}
别忘了寫一個 儲存接口的常亮類
      
/**
 * 常量類
 */
public class Constants {

    //請求的伺服器主機名
    public static final String HOST_NAME = "https://www.zhaoapi.cn";

    /**
     * 登入請求接口
     */
    public static final String LOGIN = HOST_NAME+"/user/login";

    /**
     * 擷取購物車資訊
     */
    public static final String GET_CARTS = HOST_NAME+"/product/getCarts";


    public static final String GET_DingDan = HOST_NAME+"/product/createOrder";







}      

然後我們開始寫購物車請求資料的MVP

V層的接口

import com.example.gouwu.bean.ShoppCarBean;

import java.util.List;

/**
 * Created by HaSee on 2018/1/17.
 */

public interface IMainView {
    void onSuccess(List<ShoppCarBean.DataBean> data);

    void onFailed(String msg);

}
我們還要寫一個  儲存接口的常亮類
      
/**
 * 常量類
 */
public class Constants {

    //請求的伺服器主機名
    public static final String HOST_NAME = "https://www.zhaoapi.cn";

    /**
     * 登入請求接口
     */
    public static final String LOGIN = HOST_NAME+"/user/login";

    /**
     * 擷取購物車資訊
     */
    public static final String GET_CARTS = HOST_NAME+"/product/getCarts";


    public static final String GET_DingDan = HOST_NAME+"/product/createOrder";







}      

接口寫完以後寫 V層的Activity 裡面包括一些購物車的邏輯 實作了一個 購物車MVP的接口 然後還實作了 購物車邏輯的點選事件 還有 一個建立訂單MVP 的接口

MainActivity       
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ExpandableListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.gouwu.R;
import com.example.gouwu.bean.ShoppCarBean;
import com.example.gouwu.presenter.ChengP;
import com.example.gouwu.presenter.IChengP;
import com.example.gouwu.presenter.IShooppPresenter;
import com.example.gouwu.presenter.ShooppPresenter;
import com.example.gouwu.view.adapter.MyExpandAdapter;

import java.util.List;

public class MainActivity extends AppCompatActivity implements IMainView, MyExpandAdapter.ModifyGoodsItemNumberListener, MyExpandAdapter.CheckGroupItemListener,IChuangView {

    /**
     * 傳回
     */
    private TextView mBtnBack;
    /**
     * 編輯
     */
    private TextView mBtnEditor;
    private ExpandableListView mExpandList;
    /**
     * 全選
     */
    private CheckBox mBtnCheckAll;
    /**
     * 合計:¥0.00
     */
    private TextView mTvTotalPrice;
    /**
     * 結算(0)
     */
    private TextView mBtnAmount;
    private List<ShoppCarBean.DataBean> list;
    private MyExpandAdapter adapter;
    private IShooppPresenter iShooppPrensenter;
    //購買商品的總數量
    private int totalNum = 0;
    private boolean flag;
    //購買商品的總價
    private double totalPrice= 0.00; //15.55  15  0.55億
    private IChengP chengP;
    private String zongjia;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        //隐藏actionbar
        getSupportActionBar().hide();
        //去除預設訓示器
        mExpandList.setGroupIndicator(null);
        zongjia+=totalPrice;

         chengP = new ChengP(this);
         chengP.getCarInfo("4582",zongjia);

        iShooppPrensenter= new ShooppPresenter(this);
        iShooppPrensenter.getCarInfo("4582");

        adapter = new MyExpandAdapter(this);
        mExpandList.setAdapter(adapter);
        adapter.setModifyGoodsItemNumberListener(this);
        //設定商家以及商品是否被選中的監聽
        adapter.setCheckGroupItemListener(this);
        mBtnCheckAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                isChoosedAll(((CheckBox)view).isChecked());
                //計算商品總價
                statisticsPrice();

            }
        });

        //編輯
        mBtnEditor.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if(!flag){//編輯 -> 完成\
                    flag = true;
                    mBtnEditor.setText("完成");
                    adapter.showDeleteButton(flag);
                }else{
                    flag = false;
                    mBtnEditor.setText("編輯");
                    adapter.showDeleteButton(flag);
                }

            }
        });


    }

    //二級清單預設展開
    private void defaultExpand(){

        for (int i = 0; i < adapter.getGroupCount();++i){
            mExpandList.expandGroup(i);
        }

    }
    @Override
    public void onSuccess(List<ShoppCarBean.DataBean> data) {
        this.list=data;
        adapter.setList(list);

        defaultExpand();
    }

    @Override
    public void onFailed(String msg) {

    }

    private void initView() {
        mBtnBack = (TextView) findViewById(R.id.btnBack);
        mBtnEditor = (TextView) findViewById(R.id.btnEditor);
        mExpandList = (ExpandableListView) findViewById(R.id.expandList);
        mBtnCheckAll = (CheckBox) findViewById(R.id.btnCheckAll);
        mTvTotalPrice = (TextView) findViewById(R.id.tvTotalPrice);
        mBtnAmount = (TextView) findViewById(R.id.btnAmount);
    }

    @Override
    public void checkGroupItem(int groupPosition, boolean isChecked) {
        ShoppCarBean.DataBean dataBean = list.get(groupPosition);
        dataBean.setGroupChoosed(isChecked);
        //周遊商家裡面的商品,将其置為選中狀态
        for (ShoppCarBean.DataBean.ListBean listBean : dataBean.getList()){
            listBean.setChildChoosed(isChecked);
        }

        //底部結算那個checkbox狀态(全選)
        if(isCheckAll()){
            mBtnCheckAll.setChecked(true);
        }else{
            mBtnCheckAll.setChecked(false);
        }

        //重新整理擴充卡
        adapter.notifyDataSetChanged();

        //計算價格
        statisticsPrice();
    }

    @Override
    public void checkChildItem(int groupPosition, int childPosition, boolean isChecked) {

        ShoppCarBean.DataBean dataBean = list.get(groupPosition);//商家那一層
        List<ShoppCarBean.DataBean.ListBean> listBeans = dataBean.getList();
        ShoppCarBean.DataBean.ListBean listBean = listBeans.get(childPosition);

        //你點選商家的商品條目将其選中狀态記錄
        listBean.setChildChoosed(isChecked);

        //檢測商家裡面的每一個商品是否被選中,如果被選中,傳回boolean
        boolean result = isGoodsCheckAll(groupPosition);
        if(result){
            dataBean.setGroupChoosed(result);
        }else{
            dataBean.setGroupChoosed(result);
        }

        //底部結算那個checkbox狀态(全選)
        if(isCheckAll()){
            mBtnCheckAll.setChecked(true);
        }else{
            mBtnCheckAll.setChecked(false);
        }


        //重新整理擴充卡
        adapter.notifyDataSetChanged();

        //計算總價
        statisticsPrice();
    }

    @Override
    public void doIncrease(int groupPosition, int childPosition, View view) {
        ShoppCarBean.DataBean.ListBean listBean = list.get(groupPosition).getList().get(childPosition);
        //取出目前的商品數量
        int currentNum = listBean.getNum();
        //商品++
        currentNum++;
        //将商品數量設定javabean上
        listBean.setNum(currentNum);

        //重新整理擴充卡
        adapter.notifyDataSetChanged();


        //計算商品價格
        statisticsPrice();
    }

    @Override
    public void doDecrease(int groupPosition, int childPosition, View view) {
        ShoppCarBean.DataBean.ListBean listBean = list.get(groupPosition).getList().get(childPosition);
        //取出目前的商品數量
        int currentNum = listBean.getNum();
        //直接結束這個方法
        if(currentNum == 1){
            return;
        }

        currentNum--;
        listBean.setNum(currentNum);
        //重新整理擴充卡
        adapter.notifyDataSetChanged();

        //計算商品價格
        statisticsPrice();

    }


    /**
     * 檢測某個商家的商品是否都選中,如果都選中的話,商家CheckBox應該是選中狀态
     * @param groupPosition
     * @return
     */
    private boolean isGoodsCheckAll(int groupPosition){
        List<ShoppCarBean.DataBean.ListBean> listBeans = this.list.get(groupPosition).getList();
        //周遊某一個商家的每個商品是否被選中
        for (ShoppCarBean.DataBean.ListBean listBean : listBeans){
            if(listBean.isChildChoosed()){//是選中狀态
                continue;
            }else{
                return false;
            }

        }

        return true;
    }

    //購物車商品是否全部選中
    private boolean isCheckAll(){

        for (ShoppCarBean.DataBean dataBean : list){
            if(!dataBean.isGroupChoosed()){
                return false;
            }
        }
        return true;
    }


    //全選與反選
    private void isChoosedAll(boolean isChecked){

        for (ShoppCarBean.DataBean dataBean : list){
            dataBean.setGroupChoosed(isChecked);
            for (ShoppCarBean.DataBean.ListBean listBean : dataBean.getList()){
                listBean.setChildChoosed(isChecked);
            }
        }
        //重新整理擴充卡
        adapter.notifyDataSetChanged();

    }

    /**
     * 計算你所選中的商品總價
     */
    private void statisticsPrice(){

        //初始化值
        totalNum = 0;
        totalPrice = 0.00;

        for (ShoppCarBean.DataBean dataBean : list){

            for (ShoppCarBean.DataBean.ListBean listBean : dataBean.getList()){
                if(listBean.isChildChoosed()){
                    totalNum++;
                    System.out.println("number : "+totalNum);
                    totalPrice += listBean.getNum()*listBean.getPrice();

                }
            }

        }

        //設定文本資訊 合計、結算的商品數量
        mTvTotalPrice.setText("合計:¥"+totalPrice);
        mBtnAmount.setText("結算("+totalNum+")");

    }


    @Override
    public void oncheng(final String cg) {


        mBtnAmount.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (totalPrice!=0.00) {

                    if (cg.equals("1")) {
                        Toast.makeText(MainActivity.this, "建立失敗", Toast.LENGTH_SHORT).show();

                        return;
                    } else {
                        Toast.makeText(MainActivity.this, "建立成功", Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(MainActivity.this, DingActivity.class);
                        startActivity(intent);

                    }
                }else{
                    Toast.makeText(MainActivity.this, "請選擇要購買的商品", Toast.LENGTH_SHORT).show();
                }
            }
        });


    }

    @Override
    public void onShibai(String msg) {

    }
}


然後P 層的 接口
      
import com.example.gouwu.bean.ShoppCarBean;

import java.util.List;

/**
 * Created by HaSee on 2018/1/17.
 */

public interface IShooppPresenter {
    void onSuccess(List<ShoppCarBean.DataBean> data);

    void onFailed(String msg);

    void onDestory();

    void getCarInfo(String uid);
}      

然後 P層的類

import com.example.gouwu.bean.ShoppCarBean;
import com.example.gouwu.model.IShoppCarModel;
import com.example.gouwu.model.ShoppCarModel;
import com.example.gouwu.view.IMainView;

import java.util.List;

/**
 * Created by HaSee on 2018/1/17.
 */

public class ShooppPresenter implements IShooppPresenter {
    private IMainView iMainView;
    private IShoppCarModel ishoppCarModel;

    public ShooppPresenter(IMainView iMainView) {
        this.iMainView = iMainView;
        ishoppCarModel = new ShoppCarModel();
    }

    @Override
    public void onSuccess(List<ShoppCarBean.DataBean> data) {

        if (iMainView!=null){
            iMainView.onSuccess(data);
        }
    }

    @Override
    public void onFailed(String msg) {
        if (iMainView!=null){
            iMainView.onFailed(msg);
        }
    }

    @Override
    public void onDestory() {
        if (iMainView!=null){
            iMainView=null;
        }
    }

    @Override
    public void getCarInfo(String uid) {
        ishoppCarModel.getCarInfo(uid,this);

    }
}      

然後M層的接口

package com.example.gouwu.model;

import com.example.gouwu.presenter.IShooppPresenter;

/**
 * Created by HaSee on 2018/1/17.
 */

public interface IShoppCarModel  {
    void getCarInfo(String uid,IShooppPresenter iShooppPrensenter);
}      

然後M層的類

import com.example.gouwu.bean.ShoppCarBean;
import com.example.gouwu.presenter.IShooppPresenter;
import com.example.gouwu.utils.Constants;
import com.example.gouwu.utils.OkHttpUtils;
import com.example.gouwu.utils.OnFinishListener;
import com.google.gson.Gson;

import java.util.HashMap;

/**
 * Created by HaSee on 2018/1/17.
 */

public class ShoppCarModel implements IShoppCarModel {
    @Override
    public void getCarInfo(String uid, final IShooppPresenter iShooppPrensenter) {
        HashMap<String, String> params = new HashMap<>();
        params.put("uid",uid);

        OkHttpUtils okHttpUtils = OkHttpUtils.getInstance();
        okHttpUtils.doGet(Constants.GET_CARTS,params,new OnFinishListener(){
            @Override
            public void onFailed(String msg) {
                iShooppPrensenter.onFailed(msg);
            }

            @Override
            public void onSuccess(Object obj) {
                Gson gson = new Gson();
                ShoppCarBean shoppCarBean = gson.fromJson(obj.toString(), ShoppCarBean.class);
                iShooppPrensenter.onSuccess(shoppCarBean.getData());

            }
        });
    }
}      

現在 我們的資料就已經到了V層 然後我們要寫 二級清單的 擴充卡了這裡面有點選事件還有 購物車的加減

import android.content.Context;
import android.view.LayoutInflater;
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.gouwu.R;
import com.example.gouwu.bean.ShoppCarBean;


import java.util.List;

/**
 * Created by rookie on 2018/1/9.
 */

public class MyExpandAdapter extends BaseExpandableListAdapter {


    private List<ShoppCarBean.DataBean> data;
    private Context context;
    //商品加減的一個接口變量
    private ModifyGoodsItemNumberListener modifyGoodsItemNumberListener;
    private CheckGroupItemListener checkGroupItemListener;
    //接收是否處于編輯狀态的一個boolean值
    private boolean isEditor;

    public MyExpandAdapter(Context context){
        this.context = context;
    }


    //設定資料
    public void setList(List<ShoppCarBean.DataBean> data){
        this.data = data;
        notifyDataSetChanged();
    }


    //商家以及商品是否被選中的一個監聽
    public void setCheckGroupItemListener(CheckGroupItemListener checkGroupItemListener){
        this.checkGroupItemListener = checkGroupItemListener;
    }

    //設定商品的加減監聽
    public void setModifyGoodsItemNumberListener(ModifyGoodsItemNumberListener modifyGoodsItemNumberListener){
        this.modifyGoodsItemNumberListener = modifyGoodsItemNumberListener;
    }

    //是否顯示删除按鈕
    public void showDeleteButton(boolean isEditor){
        this.isEditor = isEditor;
        //重新整理擴充卡
        notifyDataSetChanged();
    }

    @Override
    public int getGroupCount() {
        return data != null ? data.size() : 0;
    }

    @Override
    public int getChildrenCount(int i) {
        return data != null && data.get(i).getList() != null ? data.get(i).getList().size() : 0;
    }

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

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return data.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 false;
    }

    @Override
    public View getGroupView(final int groupPosition, boolean b, View convertView, ViewGroup parent) {

        if(convertView == null){

            convertView = LayoutInflater.from(context).inflate(R.layout.layout_group_item,parent,false);
        }

        //查找控件
        CheckBox ck_group_choosed = convertView.findViewById(R.id.ck_group_choosed);

        //設定商家checkbox複選框的狀态
        if(data.get(groupPosition).isGroupChoosed()){
            ck_group_choosed.setChecked(true);
        }else{
            ck_group_choosed.setChecked(false);
        }

        //ck_group_choosed.setChan
        ck_group_choosed.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                checkGroupItemListener.checkGroupItem(groupPosition,((CheckBox)view).isChecked());

            }
        });


        //指派
        ck_group_choosed.setText(data.get(groupPosition).getSellerName());


        return convertView;
    }

    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean b, View convertView, ViewGroup parent) {

        if(convertView == null){
            convertView = LayoutInflater.from(context).inflate(R.layout.layout_child_item,parent,false);
        }

        //商品選擇
        CheckBox ck_child_choosed = convertView.findViewById(R.id.ck_child_choose);
        //商品圖檔
        ImageView iv_show_pic = convertView.findViewById(R.id.iv_show_pic);
        //商品主标題
        TextView tv_commodity_name = convertView.findViewById(R.id.tv_commodity_name);
        //商品副标題
        TextView tv_commodity_attr = convertView.findViewById(R.id.tv_commodity_attr);
        //商品價格
        TextView tv_commodity_price = convertView.findViewById(R.id.tv_commodity_price);
        //商品數量
        TextView tv_commodity_num = convertView.findViewById(R.id.tv_commodity_num);
        //商品減
        TextView iv_sub = convertView.findViewById(R.id.iv_sub);
        //商品加減中的數量變化
        final TextView tv_commodity_show_num = convertView.findViewById(R.id.tv_commodity_show_num);
        //商品加
        TextView iv_add = convertView.findViewById(R.id.iv_add);
        //删除按鈕
        Button btn_commodity_delete = convertView.findViewById(R.id.btn_commodity_delete);

        //設定文本資訊
        tv_commodity_name.setText(data.get(groupPosition).getList().get(childPosition).getTitle());
        tv_commodity_attr.setText(data.get(groupPosition).getList().get(childPosition).getSubhead());
        tv_commodity_price.setText("¥"+data.get(groupPosition).getList().get(childPosition).getPrice());
        tv_commodity_num.setText("x"+data.get(groupPosition).getList().get(childPosition).getNum());
        tv_commodity_show_num.setText(data.get(groupPosition).getList().get(childPosition).getNum()+"");

        //分割圖檔位址
        String images = data.get(groupPosition).getList().get(childPosition).getImages();

        String[] urls = images.split("\\|");


        //加載商品圖檔
        Glide.with(context)
                .load(urls[0])
                .crossFade()
                .into(iv_show_pic);

        //商品加
        iv_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                modifyGoodsItemNumberListener.doIncrease(groupPosition,childPosition,tv_commodity_show_num);
            }
        });

        //設定商品加減的按鈕
        //商品減
        iv_sub.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                modifyGoodsItemNumberListener.doDecrease(groupPosition,childPosition,tv_commodity_show_num);

            }
        });

        //商品複選框是否被選中
        ck_child_choosed.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //isChecked false  true
                checkGroupItemListener.checkChildItem(groupPosition,childPosition,((CheckBox)view).isChecked());
            }
        });

        //處理商品的選中狀态
        if(data.get(groupPosition).getList().get(childPosition).isChildChoosed()){
            ck_child_choosed.setChecked(true);
        }else{
            ck_child_choosed.setChecked(false);
        }

        //控制删除按鈕的隐藏與顯示
        if(isEditor){
            btn_commodity_delete.setVisibility(View.VISIBLE);
        }else{
            btn_commodity_delete.setVisibility(View.GONE);
        }


        return convertView;
    }

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


    /**
     * 商家的複選框以及商品的複選框是否被選中的接口
     */
    /**
     * 商家複選框以及商品複選框是否被選中接口
     */
    public interface CheckGroupItemListener{
        //商家的條目的複選框監聽
        void checkGroupItem(int groupPosition, boolean isChecked);
        //商品的
        void checkChildItem(int groupPosition, int childPosition, boolean isChecked);


    }

    /**
     * 商品加減接口
     */
    public interface ModifyGoodsItemNumberListener{

        //商品添加操作
        void doIncrease(int groupPosition, int childPosition, View view);
        //商品減少操作
        void doDecrease(int groupPosition, int childPosition, View view);

    }


}


寫完擴充卡我們的購物車就已經可以點選等操作了 然後這個時候我們要進行訂單了  因為我們在購物車MVP 的V層已經進行了判斷 如果成立才跳轉 這個時候我們的判斷資料就需要在寫一個MVP來進行傳輸 下面就是 建立訂單的MVP 首先實作一個接口 然後給MainActivity 繼承一下
      
/**
 * Created by HaSee on 2018/1/17.
 */

public interface IChuangView {
    void oncheng(String cg);

    void onShibai(String msg);

}      

然後我們 開始做P層的 資料先來實作一個 接口

public interface IChengP {
    void onSuccess(String cg);

    void onFailed(String msg);

    void onDestory();

    void getCarInfo(String uid,String price);

}
      

然後寫P層的類

import com.example.gouwu.model.ChengM;
import com.example.gouwu.model.IChengM;
import com.example.gouwu.view.IChuangView;

/**
 * Created by HaSee on 2018/1/17.
 */

public class ChengP implements IChengP{
    private IChuangView iChuangView;
    private IChengM chengM;

    public ChengP(IChuangView iChuangView) {
        this.iChuangView = iChuangView;
         chengM = new ChengM();
    }

    @Override
    public void onSuccess(String cg) {
        if (iChuangView!=null){

            iChuangView.oncheng(cg);
        }

    }

    @Override
    public void onFailed(String msg) {
        if (iChuangView!=null){
            iChuangView.onShibai(msg);
        }

    }

    @Override
    public void onDestory() {
        if (iChuangView!=null){
            iChuangView=null;
        }

    }

    @Override
    public void getCarInfo(String uid,String price) {
        chengM.getCarInfo(uid,price,this);

    }
}      

然後是M層的 接口

public interface IChengM {
    void getCarInfo(String uid,String price, final IChengP iChengP);
}
      

然後是 M層的類

import java.util.HashMap;

/**
 * Created by HaSee on 2018/1/17.
 */

public class ChengM implements IChengM {
    @Override
    public void getCarInfo(String uid,String price, final IChengP iChengP) {
        HashMap<String, String> params = new HashMap<>();
        params.put("uid",uid);
        params.put("price",price);

        OkHttpUtils okHttpUtils = OkHttpUtils.getInstance();
        okHttpUtils.doGet(Constants.GET_CARTS,params,new OnFinishListener(){
            @Override
            public void onFailed(String msg) {
                iChengP.onFailed(msg);
            }

            @Override
            public void onSuccess(Object obj) {
                Gson gson = new Gson();
                ChuangJianBean chuangJianBean = gson.fromJson(obj.toString(), ChuangJianBean.class);
                String code = chuangJianBean.getCode();
                iChengP.onSuccess(code);

            }
        });
    }
}


然後資料就已經到了MainActivity裡面 進行判斷了 然後條件成立的時候就可以進行跳轉  下面我們來寫進行跳轉的DingActivity 這裡面是有一個 RecycleView 的進行訂單的展示
      
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.example.gouwu.R;
import com.example.gouwu.bean.DingBean;
import com.example.gouwu.view.adapter.MyRvAdapter;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class DingActivity extends AppCompatActivity {

    private RecyclerView mRv;
    private MyRvAdapter adapter;
    private LinearLayoutManager linearLayoutManager;
    private Handler handler=new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ding);
        mRv = (RecyclerView) findViewById(R.id.rv);

        linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        mRv.setLayoutManager(linearLayoutManager);


        OkHttpClient okHttpClient = new OkHttpClient();

        Request request=new Request.Builder()
                .get()
                .url("https://www.zhaoapi.cn/product/getOrders?uid=4582&page=1")
                .build();

        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()){
                    String str = response.body().string();
                    Gson gson = new Gson();
                    DingBean dingBean = gson.fromJson(str, DingBean.class);
                    final List<DingBean.DataBean> list = dingBean.getData();
                    handler.post(new Runnable() {
                        @Override
                        public void run() {

                            //添加分割線
                            mRv.addItemDecoration(new DividerItemDecoration(DingActivity.this, DividerItemDecoration.VERTICAL));
                            adapter = new MyRvAdapter(DingActivity.this,list);
                            mRv.setAdapter(adapter);


                        }
                    });
                }

            }
        });






然後這個就是我們上面提到的控件 的擴充卡
      
import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import android.widget.TextView;



import com.example.administrator.gouwucar.R;
import com.example.administrator.gouwucar.bean.DingBean;

import java.util.List;

/**
 * Created by HaSee on 2018/1/17.
 */

public class MyRvAdapter extends RecyclerView.Adapter<MyRvAdapter.ViewHoder>{
    private Context context;
    private List<DingBean.DataBean> list;

    public MyRvAdapter(Context context, List<DingBean.DataBean> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public MyRvAdapter.ViewHoder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(context).inflate(R.layout.dingdanitem,parent,false);
        ViewHoder viewHoder = new ViewHoder(view);


        return viewHoder;
    }


    @Override
    public void onBindViewHolder(MyRvAdapter.ViewHoder holder, final int position) {
        holder.text_orderid.setText("訂單id:"+list.get(position).getOrderid());
        holder.text_price.setText("¥:"+list.get(position).getPrice());
        holder.text_createtime.setText("建立時間:"+list.get(position).getCreatetime());
        if (list.get(position).getStatus()==0){
            holder.text_status.setText("訂單狀态:待支付");

        }else if(list.get(position).getStatus()==1){
            holder.text_status.setText("訂單狀态:已支付");
        } else if(list.get(position).getStatus()==2){
            holder.text_status.setText("訂單狀态:已取消");
        }
        holder.btn_submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                AlertDialog.Builder builder=new AlertDialog.Builder(context);
                builder.setTitle("訂單确認");
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        list.get(position).setStatus(1);
                        notifyDataSetChanged();

                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        list.get(position).setStatus(2);
                        notifyDataSetChanged();
                    }
                });
                builder.show();
            }
        });


        holder.text_title.setText(list.get(position).getTitle());


    }


    @Override
    public int getItemCount() {
        return list!=null?list.size():0;
    }

    class  ViewHoder extends RecyclerView.ViewHolder{

        private  TextView text_title;
        private  TextView text_orderid;
        private  TextView text_price;
        private  Button btn_submit;
        private  TextView text_status;
        private  TextView text_createtime;

        public ViewHoder(View itemView) {
            super(itemView);

            text_title = itemView.findViewById(R.id.text_title);
            text_orderid = itemView.findViewById(R.id.text_orderid);
            text_price = itemView.findViewById(R.id.text_price);
            btn_submit = itemView.findViewById(R.id.btn_submit);
            text_status = itemView.findViewById(R.id.text_status);
            text_createtime = itemView.findViewById(R.id.text_createtime);

        }
    }
}
      

擴充卡寫完以後 我們整個項目一共用到了 三個 GSON 的Class 其中最有用的就是 購物車的

import java.util.List;



public class ShoppCarBean {


 

    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 {
       

        private String sellerName;
        private String sellerid;
        private List<ListBean> list;
        //商家是否被選中 組條目是否被選中
        private boolean isGroupChoosed;

        public boolean isGroupChoosed() {
            return isGroupChoosed;
        }

        public void setGroupChoosed(boolean groupChoosed) {
            isGroupChoosed = groupChoosed;
        }


        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;
            private int sellerid;
            private String subhead;
            private String title;
            //子條目商品是否被選中的一個字段狀态
            private boolean isChildChoosed;

            public boolean isChildChoosed() {
                return isChildChoosed;
            }

            public void setChildChoosed(boolean childChoosed) {
                isChildChoosed = childChoosed;
            }

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



      
ChuangJianBean       
public class ChuangJianBean {

    /**
     * msg : 訂單建立成功
     * code : 0
     */

    private String msg;
    private String code;

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






      
DingBean      
import java.util.List;

/**
 * Created by Administrator on 2018/4/3 0003.
 */

public class DingBean {

    /**
     * msg : 請求成功
     * code : 0
     * data : [{"createtime":"2017-10-19T20:28:43","orderid":20,"price":100,"status":0,"title":"訂單标題測試3","uid":71},{"createtime":"2017-10-19T20:44:40","orderid":31,"price":11800,"status":2,"title":"訂單标題測試14","uid":71},{"createtime":"2017-10-19T20:44:51","orderid":32,"price":11800,"status":2,"title":"訂單标題測試15","uid":71},{"createtime":"2017-10-20T08:02:07","orderid":43,"price":11800,"status":2,"title":"訂單标題測試","uid":71},{"createtime":"2017-10-20T08:02:16","orderid":44,"price":11800,"status":2,"title":"訂單标題測試","uid":71},{"createtime":"2017-10-22T15:14:39","orderid":890,"price":11800,"status":2,"title":"","uid":71},{"createtime":"2017-11-09T09:17:20","orderid":1446,"price":99.99,"status":1,"title":"訂單标題測試","uid":71},{"createtime":"2017-11-09T09:20:58","orderid":1447,"price":567,"status":2,"title":"訂單标題測試","uid":71},{"createtime":"2017-11-09T09:20:58","orderid":1448,"price":256.99,"status":2,"title":"訂單标題測試","uid":71},{"createtime":"2017-11-09T09:20:58","orderid":1449,"price":399,"status":2,"title":"訂單标題測試","uid":71}]
     * page : 1
     */

    private String msg;
    private String code;
    private String page;
    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 String getPage() {
        return page;
    }

    public void setPage(String page) {
        this.page = page;
    }

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

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

    public static class DataBean {
        /**
         * createtime : 2017-10-19T20:28:43
         * orderid : 20
         * price : 100.0
         * status : 0
         * title : 訂單标題測試3
         * uid : 71
         */

        private String createtime;
        private int orderid;
        private double price;
        private int status;
        private String title;
        private int uid;

        public String getCreatetime() {
            return createtime;
        }

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

        public int getOrderid() {
            return orderid;
        }

        public void setOrderid(int orderid) {
            this.orderid = orderid;
        }

        public double getPrice() {
            return price;
        }

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

        public int getStatus() {
            return status;
        }

        public void setStatus(int status) {
            this.status = status;
        }

        public String getTitle() {
            return title;
        }

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

        public int getUid() {
            return uid;
        }

        public void setUid(int uid) {
            this.uid = uid;
        }
    }
}