MVP+购物车

main.xml

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

    <!-- 购物车列表 -->
    <RelativeLayout
        android:id="@+id/rl_shopcart_have"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

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

            <android.support.v7.widget.RecyclerView
                android:id="@+id/rlv_shopcart"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1" />

            <View
                android:layout_width="match_parent"
                android:layout_height="50dp" />
        </LinearLayout>

        <!-- 底部支付 -->
        <LinearLayout
            android:id="@+id/ll_pay"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:background="@color/main_white_text"
            android:gravity="center_vertical"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/tv_shopcart_addselect"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="@dimen/margin_10dp"
                android:drawableLeft="@drawable/shopcart_selected"
                android:drawablePadding="@dimen/padding_5dp"
                android:text="全选" />

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/tv_shopcart_totalprice"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:paddingLeft="@dimen/padding_10dp"
                    android:paddingTop="@dimen/padding_10dp"
                    android:text="总价:¥0"
                    android:textColor="@color/main_red_text"
                    android:textSize="@dimen/common_font_size_16" />

                <TextView
                    android:id="@+id/tv_shopcart_totalnum"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:paddingLeft="@dimen/padding_10dp"
                    android:paddingBottom="@dimen/padding_10dp"
                    android:text="共0件商品"
                    android:textSize="@dimen/common_font_size_14" />

            </LinearLayout>

            <TextView
                android:id="@+id/tv_shopcart_submit"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="@dimen/margin_10dp"
                android:background="@drawable/shop_btn"
                android:paddingLeft="@dimen/margin_30dp"
                android:paddingTop="@dimen/padding_10dp"
                android:paddingRight="@dimen/margin_30dp"
                android:paddingBottom="@dimen/padding_10dp"
                android:text="去结算"
                android:textColor="@color/main_white_text" />
        </LinearLayout>
    </RelativeLayout>
</RelativeLayout>

shop_cart_item

<?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="wrap_content"
    android:background="@color/main_white_text"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/ll_shopcart_header"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <!-- 距离占位 -->
        <View
            android:id="@+id/view"
            android:layout_width="match_parent"
            android:layout_height="@dimen/margin_10dp"
            android:background="@color/background_color"></View>

        <!-- 商铺 -->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_vertical">

            <ImageView
                android:id="@+id/iv_item_shopcart_shopselect"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="@dimen/margin_15dp"
                android:paddingTop="@dimen/margin_10dp"
                android:paddingRight="@dimen/margin_15dp"
                android:paddingBottom="@dimen/margin_10dp"
                android:src="@drawable/shopcart_selected" />

            <TextView
                android:id="@+id/tv_item_shopcart_shopname"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:drawableLeft="@drawable/shopcart_shop"
                android:drawablePadding="@dimen/padding_5dp"
                android:padding="@dimen/padding_10dp"
                android:text="宝儿家服装" />

        </LinearLayout>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical">

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

            <!-- 分隔商铺和商铺的线 -->
            <View
                android:layout_width="match_parent"
                android:layout_height="@dimen/margin_1dp"
                android:background="@color/background_color"></View>

            <!-- 商品名称 -->
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <!-- 图片占位 为了对其好看 -->
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="@dimen/margin_15dp"
                    android:layout_marginRight="@dimen/margin_15dp"
                    android:src="@drawable/shopcart_selected"
                    android:visibility="invisible" />

                <!-- 显示商品名称的 -->
                <TextView
                    android:id="@+id/tv_item_shopcart_clothname"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:paddingLeft="@dimen/padding_10dp"
                    android:paddingTop="@dimen/padding_10dp"
                    android:text="小米6" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_vertical"
                android:orientation="horizontal">

                <!-- 选中图片 -->
                <ImageView
                    android:id="@+id/tv_item_shopcart_clothselect"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="@dimen/margin_15dp"
                    android:src="@drawable/shopcart_selected" />

                <!-- 商品图片 -->
                <ImageView
                    android:id="@+id/iv_item_shopcart_cloth_pic"
                    android:layout_width="60dp"
                    android:layout_height="60dp"
                    android:layout_margin="@dimen/margin_10dp" />

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:orientation="vertical">

                    <!-- 商品价钱的TextView -->
                    <TextView
                        android:id="@+id/tv_item_shopcart_cloth_price"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="¥185"
                        android:textColor="@color/main_red_text"
                        android:textSize="@dimen/common_font_size_14" />

                    <!-- 商品的颜色和尺寸 -->
                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="@dimen/margin_5dp"
                        android:layout_marginBottom="@dimen/margin_5dp">

                        <TextView
                            android:id="@+id/tv_item_shopcart_cloth_color"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="颜色:黑色"
                            android:textSize="@dimen/common_font_size_12" />

                        <TextView
                            android:id="@+id/tv_item_shopcart_cloth_size"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginLeft="@dimen/margin_10dp"
                            android:text="尺寸:XL"
                            android:textSize="@dimen/common_font_size_12" />

                    </LinearLayout>

                    <!-- 加减键号 商品数量 -->
                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:gravity="center_vertical">

                        <ImageView
                            android:id="@+id/iv_item_shopcart_cloth_minus"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:src="@drawable/shopcart_minus_grey" />

                        <TextView
                            android:id="@+id/et_item_shopcart_cloth_num"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginLeft="@dimen/margin_5dp"
                            android:background="@drawable/shopcart_add_btn"
                            android:paddingLeft="@dimen/padding_20dp"
                            android:paddingTop="@dimen/padding_2dp"
                            android:paddingRight="@dimen/padding_20dp"
                            android:paddingBottom="@dimen/padding_2dp"
                            android:text="1" />

                        <ImageView
                            android:id="@+id/iv_item_shopcart_cloth_add"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginLeft="@dimen/margin_5dp"
                            android:src="@drawable/shopcart_add_red" />

                    </LinearLayout>


                </LinearLayout>

                <!-- 竖线 -->
                <View
                    android:layout_width="@dimen/margin_1dp"
                    android:layout_height="match_parent"
                    android:layout_marginTop="@dimen/padding_10dp"
                    android:layout_marginBottom="@dimen/padding_10dp"
                    android:background="@color/splitline_color"></View>

                <!-- 删除的图片 -->
                <ImageView
                    android:id="@+id/iv_item_shopcart_cloth_delete"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="@dimen/margin_20dp"
                    android:src="@drawable/shopcart_delete" />
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>

    <!-- 底部的线 -->
    <View
        android:layout_width="match_parent"
        android:layout_height="@dimen/margin_1dp"
        android:background="@color/background_color"></View>

</LinearLayout>

 

mvp

MainModel

package com.bwie.xiaqin.shopping0921.model;

import com.bwie.xiaqin.shopping0921.bean.ShopBean;

/**
 * Created by lenovo on 2018/9/21.
 */

public interface MainModel {
    void onSuccess(ShopBean shopBean);

    void onError(int code);
}

MyMolder

package com.bwie.xiaqin.shopping0921.model;

import com.bwie.xiaqin.shopping0921.bean.ShopBean;
import com.bwie.xiaqin.shopping0921.utils.OKHttpUtils;
import com.google.gson.Gson;

import java.io.IOException;

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

/**
 * Created by lenovo on 2018/9/21.
 */

public class MyModel {
    private String api = "https://www.zhaoapi.cn/product/getCarts?uid=71";
    public void getData(final MainModel mainModel){
        OKHttpUtils.doget(api, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                mainModel.onError(1);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String string = response.body().string();
                ShopBean shopBean = new Gson().fromJson(string, ShopBean.class);
                mainModel.onSuccess(shopBean);
            }
        });
    }
}

View

package com.bwie.xiaqin.shopping0921.View;

import com.bwie.xiaqin.shopping0921.bean.ShopBean;

/**
 * Created by lenovo on 2018/9/21.
 */

public interface MainView {
    void onSuccess(ShopBean shopBean);
    void onError(int code);
}

presenter

package com.bwie.xiaqin.shopping0921.presenter;

/**
 * Created by lenovo on 2018/9/21.
 */

public interface MainPresenter {

    void desc();
}

Mypresenter

package com.bwie.xiaqin.shopping0921.presenter;

/**
 * Created by lenovo on 2018/9/21.
 */

public interface MainPresenter {

    void desc();
}

activity

package com.bwie.xiaqin.shopping0921.activity;

import android.graphics.drawable.Drawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bwie.xiaqin.shopping0921.R;
import com.bwie.xiaqin.shopping0921.adapter.ShopCartAdapter;
import com.bwie.xiaqin.shopping0921.bean.ShopBean;
import com.bwie.xiaqin.shopping0921.event.OnResfreshListener;
import com.google.gson.Gson;

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

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

public class MainActivity extends AppCompatActivity {
    private static final String TAG = MainActivity.class.getSimpleName();
    private TextView tvShopCartSubmit,tvShopCartSelect,tvShopCartTotalNum;
    private RecyclerView rlvShopCart, rlvHotProducts;
    private ShopCartAdapter mShopCartAdapter;
    private LinearLayout llPay;
    private RelativeLayout rlHaveProduct;
    private List<ShopBean.DataBean.ListBean> mAllOrderList = new ArrayList<>();
    private ArrayList<ShopBean.DataBean.ListBean> mGoPayList = new ArrayList<>();
    private List<String> mHotProductsList = new ArrayList<>();
    private TextView tvShopCartTotalPrice;
    private int mCount, mPosition;
    private float mTotalPrice1;
    private boolean mSelect;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tvShopCartSelect = findViewById(R.id.tv_shopcart_addselect);
        tvShopCartTotalPrice = findViewById(R.id.tv_shopcart_totalprice);
        tvShopCartTotalNum = findViewById(R.id.tv_shopcart_totalnum);

        rlHaveProduct = findViewById(R.id.rl_shopcart_have);
        rlvShopCart = findViewById(R.id.rlv_shopcart);
        llPay = findViewById(R.id.ll_pay);

        tvShopCartSubmit = findViewById(R.id.tv_shopcart_submit);

        rlvShopCart.setLayoutManager(new LinearLayoutManager(this));
        mShopCartAdapter = new ShopCartAdapter(this, mAllOrderList);
        rlvShopCart.setAdapter(mShopCartAdapter);

        //实时监控全选按钮
        mShopCartAdapter.setmOnResfreshListener(new OnResfreshListener(){
            @Override
            public void onResfresh(boolean isSelect) {
                mSelect = isSelect;
                if (isSelect){
                    Drawable left = getResources().getDrawable(R.drawable.shopcart_selected);
                    tvShopCartSelect.setCompoundDrawablesWithIntrinsicBounds(left, null, null, null);
                }else {
                    Drawable left = getResources().getDrawable(R.drawable.shopcart_unselected);
                    tvShopCartSelect.setCompoundDrawablesWithIntrinsicBounds(left, null, null, null);
                }
                // 计算总价
                float mTotalPrice = 0;
                int mTotalNum = 0;
                mTotalPrice1 = 0;
                mGoPayList.clear();
                // 遍历所有商品 计算总价
                for (int i = 0;i<mAllOrderList.size();i++){
                    if (mAllOrderList.get(i).getSelected() == 0){
                        mTotalPrice += mAllOrderList.get(i).getPrice() * mAllOrderList.get(i).getNum();
                        mTotalNum += 1;
                        mGoPayList.add(mAllOrderList.get(i));
                    }
                    mTotalPrice1 = mTotalPrice;
                    tvShopCartTotalPrice.setText("总价:" + mTotalPrice);
                    tvShopCartTotalNum.setText("共" + mTotalNum + "件商品");
                }
            }
        });
        tvShopCartSelect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 对全选状态进行去反
                mSelect = !mSelect;
                if (mSelect){
                    // 全部选中
                    Drawable left = getResources().getDrawable(R.drawable.shopcart_selected);
                    tvShopCartSelect.setCompoundDrawablesWithIntrinsicBounds(left, null, null, null);
                    for (ShopBean.DataBean.ListBean listBean : mAllOrderList){
                        listBean.setSelected(0);
                        listBean.setShopSelect(true);
                    }
                }else {
                    // 全部取消
                    Drawable left = getResources().getDrawable(R.drawable.shopcart_unselected);
                    tvShopCartSelect.setCompoundDrawablesWithIntrinsicBounds(left, null, null, null);
                    for (ShopBean.DataBean.ListBean listBean :mAllOrderList){
                        listBean.setSelected(1);
                        listBean.setShopSelect(false);
                    }
                }
                mShopCartAdapter.notifyDataSetChanged();
            }
        });
        initData();

    }
        //创建并构造数据
    private void initData() {
        OkHttpClient client = new OkHttpClient();
        client.newCall(new Request.Builder().url("https://www.zhaoapi.cn/product/getCarts?uid=71").build()).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                Gson gson = new Gson();
                ShopBean shopBean = gson.fromJson(response.body().string(), ShopBean.class);
                Log.i(TAG, "shopBean:" + shopBean.getMsg());
                for (ShopBean.DataBean dataBean : shopBean.getData()){
                   for (ShopBean.DataBean.ListBean listBean : dataBean.getList()){
                       listBean.setShopName(dataBean.getSellerName());
                       mAllOrderList.add(listBean);
                   }
                }
                isSelectFirst(mAllOrderList);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mShopCartAdapter.notifyDataSetChanged();
                    }
                });
            }
        });
    }
    //判断是否是商品的第一个商品 是第一个商品 需要显示商铺

    public static void isSelectFirst(List<ShopBean.DataBean.ListBean> list) {
        // 1. 判断是否有商品 有商品 根据商品是否是第一个显示商铺
        if (list.size()>0){
            //头个商品一定属于它所在商铺的第一个位置,isFirst标记为1.
            list.get(0).setFirst(true);
            for (int i = 1; i < list.size(); i++){
                //每个商品跟它前一个商品比较,如果Shopid相同isFirst则标记为2,
                //如果Shopid不同,isFirst标记为1.
                if (list.get(i).getSellerid() == list.get(i - 1).getSellerid()){
                    list.get(i).setFirst(false);
                }else {
                    list.get(i).setFirst(true);
                }
            }
        }
    }
}

adapter

package com.bwie.xiaqin.shopping0921.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bumptech.glide.Glide;

import com.bwie.xiaqin.shopping0921.R;
import com.bwie.xiaqin.shopping0921.activity.MainActivity;
import com.bwie.xiaqin.shopping0921.bean.ShopBean;
import com.bwie.xiaqin.shopping0921.event.OnResfreshListener;
import com.bwie.xiaqin.shopping0921.holder.ShopCartHolder;

import java.util.List;

/**
 * Created by lenovo on 2018/9/21.
 */

public class ShopCartAdapter extends RecyclerView.Adapter<ShopCartHolder>{
    private Context context;
    private List<ShopBean.DataBean.ListBean> data;


    public ShopCartAdapter(Context context, List<ShopBean.DataBean.ListBean> data) {
        this.context = context;
        this.data = data;
    }

    @NonNull
    @Override
    public ShopCartHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ShopCartHolder(LayoutInflater.from(context).inflate(R.layout.shop_cart_item,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull ShopCartHolder holder, final int position) {
        //商品图片
        Glide.with(context).load(data.get(position).getImages().split("\\|")[0]).into(holder.ivShopCartClothPic);
         /* 商品的基本信息 */
        // holder.tvShopCartClothColor.setText("颜色:" + data.get(position).getColor());
        //  holder.tvShopCartClothSize.setText("尺寸:" + data.get(position).getSize());
        //  holder.tvShopCartClothName.setText(data.get(position).getProductName());
        holder.tvShopCartClothName.setText(data.get(position).getShopName());
        holder.tvShopCartClothPrice.setText("¥" +data.get(position).getPrice());
        holder.etShopCartClothNum.setText(data.get(position).getNum() + "");
        /* 显示前面的选中状态 */
        if (data.get(position).getSelected() == 0){
            holder.ivShopCartClothSel.setImageDrawable(context.getResources().getDrawable(R.drawable.shopcart_selected));
        }else {
            holder.ivShopCartClothSel.setImageDrawable(context.getResources().getDrawable(R.drawable.shopcart_unselected));
        }
        if (data.get(position).getSelected() == 0){
            holder.ivShopCartShopSel.setImageDrawable(context.getResources().getDrawable(R.drawable.shopcart_selected));
        }else {
            holder.ivShopCartShopSel.setImageDrawable(context.getResources().getDrawable(R.drawable.shopcart_unselected));
        }
         /* 判断是否显示商铺 */
         if (position>0){
                 /* 判断是否是同一个商铺的商品 */
                 if (data.get(position).getSellerid() == data.get(position - 1).getSellerid()){
                    holder.llShopCartHeader.setVisibility(View.GONE);
                 }else {
                     holder.llShopCartHeader.setVisibility(View.VISIBLE);
                 }
         }else {
             holder.llShopCartHeader.setVisibility(View.VISIBLE);
         }
        // 计算逻辑 与操作
        /* 判断是否全选并计算 */
        if (mOnResfreshListener != null){
            boolean isSelect = false;
            for (int i = 0;i<data.size();i++){
                if (data.get(i).getSelected() == 1){
                    isSelect = false;
                    break;
                }else {
                    isSelect = true;
                }
            }
            mOnResfreshListener.onResfresh(isSelect);
        }
        //商品数量加
        holder.ivShopCartClothAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                data.get(position).setNum(data.get(position).getNum()+1);
                notifyDataSetChanged();
            }
        });
        //商品数量减
        holder.ivShopCartClothMinus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (data.get(position).getNum()>1){
                    data.get(position).setNum(data.get(position).getNum()-1);
                    notifyDataSetChanged();
                }
            }
        });
        //删除操作
        holder.ivShopCartClothDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                data.remove(position);
                //重新排序,标记所有商品不同商铺第一个的商品位置
                MainActivity.isSelectFirst(data);
                notifyDataSetChanged();
            }
        });
        //单个商品 选中状态
        holder.ivShopCartClothSel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                data.get(position).setSelected(data.get(position).getSelected() == 0 ? 1 : 0);
                //通过循环找出不同商铺的第一个商品的位置
                for (int i = 0; i < data.size(); i++){
                    if (data.get(i).isFirst()){
                        //遍历去找出同一家商铺的所有商品的勾选情况
                        for (int j = 0; j < data.size(); j++){
                            //如果是同一家商铺的商品,并且其中一个商品是未选中,那么商铺的全选勾选取消
                            if (data.get(j).getSellerid() == data.get(i).getSellerid() && data.get(j).getSelected() == 1){
                                data.get(i).setShopSelect(false);
                                break;
                            }else {
                                //如果是同一家商铺的商品,并且所有商品是选中,那么商铺的选中全选勾选
                                data.get(i).setShopSelect(true);
                            }
                        }
                    }
                }
                notifyDataSetChanged();
            }
        });
        //商铺选中状态
        holder.ivShopCartShopSel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (data.get(position).isFirst()){
                    // 商铺选中状态执反
                    data.get(position).setShopSelect(!data.get(position).isShopSelect());
                    // 改变商品的选中状态和商铺一样
                    for (int i = 0; i < data.size(); i++){
                        if (data.get(i).getSellerid() == data.get(position).getSellerid()){
                            data.get(i).setSelected(data.get(position).isShopSelect() ? 0 : 1);
                        }
                    }
                    notifyDataSetChanged();
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return data.size();
    }
    //刷新的接口
    private OnResfreshListener mOnResfreshListener;

    public void setmOnResfreshListener(OnResfreshListener mOnResfreshListener) {
        this.mOnResfreshListener = mOnResfreshListener;
    }
}

event  OnResfreshListener

package com.bwie.xiaqin.shopping0921.event;

/**
 * Created by lenovo on 2018/9/21.
 */

public class OnResfreshListener {

    public void onResfresh(boolean isSelect) {

    }
}

holder

package com.bwie.xiaqin.shopping0921.holder;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bwie.xiaqin.shopping0921.R;

/**
 * Created by lenovo on 2018/9/21.
 */

public class ShopCartHolder extends RecyclerView.ViewHolder {
    public ImageView ivShopCartShopSel;
    public TextView tvShopCartShopName;
    public TextView tvShopCartClothName;
    public TextView tvShopCartClothPrice;
    public TextView etShopCartClothNum;
    public TextView tvShopCartClothColor;
    public TextView tvShopCartClothSize;
    public ImageView ivShopCartClothSel;
    public ImageView ivShopCartClothMinus;
    public ImageView ivShopCartClothAdd;
    public ImageView ivShopCartClothDelete;
    public ImageView ivShopCartClothPic;
    public LinearLayout llShopCartHeader;

    public ShopCartHolder(View itemView) {
        super(itemView);
        llShopCartHeader = itemView.findViewById(R.id.ll_shopcart_header);
        ivShopCartShopSel = itemView.findViewById(R.id.iv_item_shopcart_shopselect);
        tvShopCartShopName = itemView.findViewById(R.id.tv_item_shopcart_shopname);
        tvShopCartClothName = itemView.findViewById(R.id.tv_item_shopcart_clothname);
        tvShopCartClothPrice = itemView.findViewById(R.id.tv_item_shopcart_cloth_price);
        etShopCartClothNum = itemView.findViewById(R.id.et_item_shopcart_cloth_num);
        tvShopCartClothColor = itemView.findViewById(R.id.tv_item_shopcart_cloth_color);
        tvShopCartClothSize = itemView.findViewById(R.id.tv_item_shopcart_cloth_size);
        ivShopCartClothSel = itemView.findViewById(R.id.tv_item_shopcart_clothselect);
        ivShopCartClothMinus = itemView.findViewById(R.id.iv_item_shopcart_cloth_minus);
        ivShopCartClothAdd = itemView.findViewById(R.id.iv_item_shopcart_cloth_add);
        ivShopCartClothPic = itemView.findViewById(R.id.iv_item_shopcart_cloth_pic);
        ivShopCartClothDelete = itemView.findViewById(R.id.iv_item_shopcart_cloth_delete);
    }
}

utils

package com.bwie.xiaqin.shopping0921.utils;

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

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

/**
 * Created by lenovo on 2018/9/21.
 */

public class OKHttpUtils {
    private static OkHttpClient client = null;

    public OKHttpUtils(){

    }

    public static OkHttpClient getInstance(){
        if (client == null){
            synchronized (OKHttpUtils.class){
                if (client == null)
                    client = new OkHttpClient();
            }
        }
        return client;
    }

    public static void dogetex(String url){
        Request request = new Request.Builder()
                .url(url)
                .build();
        Call call = getInstance().newCall(request);
        try {
            call.execute();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void doget(String url, Callback callback){
        Request request = new Request.Builder()
                .url(url)
                .build();
        Call call = getInstance().newCall(request);
        call.enqueue(callback);
    }

    public static void dopost(String url, Map<String,String> map, Callback callback){
        FormBody.Builder builder = new FormBody.Builder();
        for (String key: map.keySet()){
            builder.add(key,map.get(key));
        }
        Request request = new Request.Builder()
                .url(url)
                .post(builder.build())
                .build();

        Call call = getInstance().newCall(request);
        call.enqueue(callback);
    }

    public static void dopostjson(String url,String json,Callback callback){
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"),json);
        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();
        Call call = getInstance().newCall(request);
        call.enqueue(callback);
    }

    class MyIntercepter implements Interceptor {

        @Override
        public Response intercept(Chain chain) throws IOException {
            Request request = chain.request();
            String method = request.method();
            if (method.equals("GET")){
                String url = request.url().toString();
                boolean contains = url.contains("?");
                if (contains){
                    url = url +"&source=android";
                }else {
                    url = url + "?source=android";
                }

                Request request1 = request.newBuilder().url(url).build();

                Response response = chain.proceed(request1);

                return response;
            }else  if (method.equals("POST")){
                RequestBody body = request.body();
                if (body instanceof FormBody){
                    FormBody.Builder newBuilder = new FormBody.Builder();
                    for (int i = 0; i < ((FormBody) body).size();i++){
                        String key = ((FormBody) body).name(i);
                        String value = ((FormBody) body).value(i);
                        newBuilder.add(key,value);
                    }

                    newBuilder.add("source","android");
                    FormBody newBody = newBuilder.build();

                    Request request1 = request.newBuilder().post(newBody).build();

                    Response response = chain.proceed(request1);
                    return response;
                }
            }
            return null;
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值