购物车

权限

 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

Okhttp

package com.bawei.moni.mvp.ok;

import android.os.Environment;
import android.os.Handler;

import com.google.gson.Gson;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.Cache;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/*Time:2019/4/4
 *Author:zhaozhiwei
 *Description:
 */
public class OKhttp {
    private static Handler handler = new Handler();
    private static OkHttpClient client;
    private static OKhttp instance;


    //创建烂机器
    private Interceptor getAppInterceptor(){
        Interceptor interceptor = new Interceptor() {
            @Override
            public Response intercept(Chain chain) throws IOException {
                Request request = chain.request();
                Response proceed = chain.proceed(request);
                return proceed;
            }
        };
        return interceptor;
    }
    private OKhttp(){
        File file = new File(Environment.getExternalStorageDirectory() + "cache1");
        new OkHttpClient().newBuilder()
                .readTimeout(3000,TimeUnit.SECONDS)
                .connectTimeout(3000,TimeUnit.SECONDS)
                .addInterceptor(getAppInterceptor())
                .cache(new Cache(file,10*1024))
                .build();
    }
    //单例模式
    public static OKhttp getInstance(){
        if (instance==null){
            instance = new OKhttp();
        }
        return instance;
    }
    public static OkHttpClient okHttpClient(){
        client=new OkHttpClient.Builder().build();
        return client;
    }
    public void doGet(String url,final Class clazz,final NetCallback netCallback){
        final Request request = new Request.Builder()
                .url(url)
                .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 {
                final String string = response.body().string();
                //Gson gson = new Gson();
                //final Object o = gson.fromJson(string, clazz);
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                     netCallback.onTString(string);
                    }
                });
            }
        });

    }
    public static void doPost(String url,String name,String pass,final Class clazz,final NetCallback netCallback){
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .connectTimeout(3000, TimeUnit.SECONDS)
                .readTimeout(3000,TimeUnit.SECONDS)
                .build();
       FormBody build =  new FormBody.Builder()
               .add("phone",name)
               .add("pwd",pass)
               .build();
        Request request = new Request.Builder()
                .url(url)
                .post(build)
                .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 {
                String string = response.body().string();
                Gson gson = new Gson();
                final Object o = gson.fromJson(string, clazz);
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        netCallback.onSuccess(o);
                    }
                });
            }
        });

    }


     public interface NetCallback{
         void onSuccess(Object obj);
         void onTString(String s);
    }
}

主页


/*Time:2019/4/4
 *Author:zhaozhiwei
 *Description:
 */public class Fragment01 extends Fragment implements View.OnClickListener {

    private RecyclerView mRecy;
    private ShopCarModel shopCarModel;
    private CheckBox cbCheckAll;
    private TextView tvTotalNumb;
    private ShopCarAdapter shopCarAdapter;
    private TextView tvTotalPrice;
    private ShouBean three;
    @Nullable
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment01, container, false);

        mRecy = view.findViewById(R.id.mRecy);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecy.setLayoutManager(linearLayoutManager);

        cbCheckAll = view.findViewById(R.id.cbCheckAll);
        cbCheckAll.setOnClickListener(this);
        tvTotalNumb = view.findViewById(R.id.tvTotalNumb);
        tvTotalPrice = view.findViewById(R.id.tvTotalPrice);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initDada();

    }


    private void initDada() {
        shopCarModel = new ShopCarModel();
        shopCarModel.getShopCarModel(new OKhttp.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                //  three three = (com.bawei.moni.bean.three) obj;
            }
            //15201230076
            @Override
            public void onTString(String s) {
                Gson gson = new Gson();
                three = gson.fromJson(s, ShouBean.class);
                shopCarAdapter = new ShopCarAdapter(three, getContext());
                mRecy.setAdapter(shopCarAdapter);
                shopCarAdapter.notifyDataSetChanged();
                shangjia();
            }
        });
    }

    private void shangjia() {
      shopCarAdapter.setGroupListener(new ShopCarAdapter.onGroupListener() {
          @Override
          public void onGroup(List<ShouBean.DataBean> data1) {
              Toast.makeText(getContext(), data1+"data", Toast.LENGTH_SHORT).show();
          }
          @Override
          public void onShow(int num, int price) {
              int price1=0;
              price1+=num*price;
              tvTotalNumb.setText("组价:"+price1+"");
              //刷新适配器
              new Handler().post(new Runnable() {
                  @Override
                  public void run() {

                      shopCarAdapter.notifyDataSetChanged();
                  }
              });
          }
      });
    }
    @Override
    public void onClick(View v) {
        setOntListChick(cbCheckAll.isChecked());
    }

    private void setOntListChick(boolean checked) {
        int prices = 0;//价格
        if (checked) {//如果 全选checkbox为 true的话
            for (int i = 0; i < three.getData().size(); i++) {
                three.getData().get(i).setIschecked(true);
                for (int j = 0; j < three.getData().get(i).getList().size(); j++) {//商品 循环  便利
                    //商品也设为 true
                    three.getData().get(i).getList().get(j).setChecked(true);
                    // //如果  商品为true的true的话计算价格
                    prices+=three.getData().get(i).getList().get(j).getNum()*three.getData().get(i).getList().get(j).getPrice();
                }
                }
        } else {//  如果   全选的CheckBox为false的话
            for (int i = 0; i < three.getData().size(); i++) {//遍历商家
                //遍历商家 设为 false
                three.getData().get(i).setIschecked(false);
                for (int j = 0; j < three.getData().get(i).getList().size(); j++) {
                    //遍历商品 设为 false
                    three.getData().get(i).getList().get(j).setChecked(false);
                }
            }
        }

        shopCarAdapter.notifyDataSetChanged();
        tvTotalPrice.setText("总价:"+prices + "");
    }
}

中间适配器

package com.bawei.moni.adapter;

import android.content.Context;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CheckedTextView;
import android.widget.CompoundButton;

import com.bawei.moni.R;
import com.bawei.moni.bean.ShouBean;
import com.bawei.moni.bean.ShowBean;
import com.bawei.moni.bean.Two;
import com.bawei.moni.bean.three;
import com.bawei.moni.fragment.Fragment01;

import java.util.List;


/*Time:2019/4/4
 *Author:zhaozhiwei
 *Description:
 */public class ShopCarAdapter extends RecyclerView.Adapter<ShopCarAdapter.MyViewHolder> {

     ShouBean shouBean;
     Context context;
    List<ShouBean.DataBean> data1;
    ShopCarAdapter shopCarAdapter;

    public ShopCarAdapter(ShouBean shouBean, Context context) {
        this.shouBean = shouBean;
        this.context = context;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_shop_cart,viewGroup,false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final MyViewHolder myViewHolder, final int i) {
        //显示一级列表
        String sellerName = shouBean.getData().get(i).getSellerName();
        myViewHolder.tvShopName.setText(sellerName);
        //二级列表显示
        final List<ShouBean.DataBean.ListBean> list = shouBean.getData().get(i).getList();
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        myViewHolder.recyclerViewGoods.setLayoutManager(linearLayoutManager);

        final GoodAadpter goodAadpter = new GoodAadpter(list, context);
        myViewHolder.recyclerViewGoods.setAdapter(goodAadpter);
        myViewHolder.cbShopCheck.setChecked(shouBean.getData().get(i).isIschecked());

        //商品   适配器的监听
        goodAadpter.onChildListener(new GoodAadpter.onChildListener() {
            @Override
            public void onChild() {
                if (groupListener!=null){
                    groupListener.onGroup(data1);
                }
                //声明  变量  为 true
                boolean ischecked = true;
                for (int j=0;j<list.size();j++){
                    boolean checked = list.get(j).isChecked();//获取商品的状态
                    if (!checked){
                        ischecked=false;
                        break;
                    }
                }
                //通过商品状态 来改变商家状态
                myViewHolder.cbShopCheck.setChecked(ischecked);
                // 改变商品的状态  调用 商品适配器内的  setCheckChild()方法
                shouBean.getData().get(i).setIschecked(ischecked);

            }
        });
        //商家控制 商品   shang1是   商家的checkbox
        //给商家设置点击事件
        myViewHolder.cbShopCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                //选中商家
                List<ShouBean.DataBean> list1 = shouBean.getData();
                ShouBean.DataBean dataBean = list1.get(i);
                //选中商家的商品
                List<ShouBean.DataBean.ListBean> list2 = dataBean.getList();

                dataBean.setIschecked(isChecked);
                for (int i1 = 0; i1 < list2.size(); i1++) {
                    ShouBean.DataBean.ListBean listBean = list2.get(i1);
                    listBean.setChecked(isChecked);

                    int price = listBean.getPrice();
                    int num = listBean.getNum();
                    groupListener.onShow(price,num);
                }
                //刷新适配器
                new Handler().post(new Runnable() {
                    @Override
                    public void run() {

                        notifyDataSetChanged();
                    }
                });
            }

        });

    }

    @Override
    public int getItemCount() {
        return shouBean.getData().size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        private CheckedTextView tvShopName;
        private CheckBox cbShopCheck;
        private RecyclerView recyclerViewGoods;
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            tvShopName = itemView.findViewById(R.id.tvShopName);
            cbShopCheck = itemView.findViewById(R.id.cbShopCheck);
            recyclerViewGoods = itemView.findViewById(R.id.recyclerViewGoods);
        }
    }

    public interface onGroupListener {
        void onGroup(List<ShouBean.DataBean> data1);
        void onShow(int num,int price);
    }

    public onGroupListener groupListener;

    public void setGroupListener(onGroupListener groupListener) {
        this.groupListener = groupListener;
    }

}

里面适配器

package com.bawei.moni.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 android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.bawei.moni.R;
import com.bawei.moni.bean.ShouBean;
import com.bawei.moni.bean.ShowBean;
import com.bawei.moni.zview.SubAddButton;
import com.bumptech.glide.Glide;

import java.util.List;

/*Time:2019/4/4
 *Author:zhaozhiwei
 *Description:
 */public class GoodAadpter extends RecyclerView.Adapter<GoodAadpter.MyViewHolder> {
    List<ShouBean.DataBean.ListBean> list;
    Context context;
    private boolean checked;

    public GoodAadpter(List<ShouBean.DataBean.ListBean> list, Context context) {
        this.list = list;
        this.context = context;
    }


    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_goods, viewGroup, false);
        return new GoodAadpter.MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, final int i) {
        //详情页
        myViewHolder.tvGoodsName.setText(list.get(i).getTitle());
        myViewHolder.tvGoodsPrice.setText(list.get(i).getPrice() + "");
        myViewHolder.cbGoodsCheck.setChecked(list.get(i).isChecked());
        Glide.with(context).load(list.get(i).getImages()).into(myViewHolder.ivGoodsPic);
        //自定义view;
        myViewHolder.myview.setData(this,list,i);
        myViewHolder.myview.setNumChangedListener(new SubAddButton.OnNumChangedListener() {
            @Override
            public void onResult() {
                if (childListener!=null){
                    childListener.onChild();
                }
            }
        });
        //商品状态,改变
        myViewHolder.cbGoodsCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (childListener!=null){
                    list.get(i).setChecked(isChecked);
                    childListener.onChild();
                }
            }
        });

    }
    //设置商品的状态
    public void setCheckChild(boolean checked) {
        //商家适配器调用次方法
        //通过该方法实现商家内部商品全选
        for (int i = 0; i < list.size(); i++) {
            list.get(i).setChecked(checked);
        }
        //设置完状态  刷新适配器
        notifyDataSetChanged();
    }


    @Override
    public int getItemCount() {
        return list.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        private TextView tvGoodsName, tvGoodsPrice;
        private ImageView ivGoodsPic;
        private CheckBox cbGoodsCheck;
        private SubAddButton myview;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            tvGoodsName = itemView.findViewById(R.id.tvGoodsName);
            tvGoodsPrice = itemView.findViewById(R.id.tvGoodsPrice);
            ivGoodsPic = itemView.findViewById(R.id.ivGoodsPic);
            cbGoodsCheck = itemView.findViewById(R.id.cbGoodsCheck);
            myview = itemView.findViewById(R.id.myview);

        }
    }

    public onChildListener childListener;

    public interface onChildListener{
        void onChild();
    }

    public void onChildListener(onChildListener childListener){
        this.childListener=childListener;
    }
}

请求Model

public class ShopCarModel {
     public void getShopCarModel(OKhttp.NetCallback netCallback){
         String url="http://172.17.8.100/ks/product/getCarts?uid=51";
         OKhttp.getInstance().doGet(url, ShouBean.class,netCallback);
     }
}

主页布局

<?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:orientation="vertical"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

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

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:background="#ddd"
        android:layout_height="30dp">
        <CheckBox
            android:id="@+id/cbCheckAll"
            android:textSize="18sp"
            android:textColor="@color/colorPrimaryDark"
            android:gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <TextView
            android:id="@+id/tvTotalPrice"
            android:textSize="18sp"
            android:textColor="@color/colorPrimaryDark"
            android:gravity="center"
            android:text="111"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <TextView
            android:id="@+id/tvTotalNumb"
            android:textSize="18sp"
            android:textColor="@color/colorPrimaryDark"
            android:gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </LinearLayout>


</LinearLayout>

中间适配器布局

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

<LinearLayout
    android:orientation="horizontal"
    android:gravity="center_vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <CheckBox
        android:id="@+id/cbShopCheck"
        android:textColor="@color/colorPrimary"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <CheckedTextView
        android:id="@+id/tvShopName"
        android:textColor="@color/colorPrimary"
        android:padding="10dp"
        android:textSize="35sp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>


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

里面布局

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


    <CheckBox
        android:id="@+id/cbGoodsCheck"
        android:textColor="@color/colorAccent"
        android:textSize="20sp"

        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <ImageView
        android:id="@+id/ivGoodsPic"
        android:layout_width="80dp"
        android:layout_height="80dp" />

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/tvGoodsName"
            android:textSize="20sp"
            android:gravity="center"
            android:textColor="@color/colorAccent"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <TextView
            android:id="@+id/tvGoodsPrice"
            android:textSize="20sp"
            android:gravity="center"
            android:textColor="@color/colorAccent"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <com.bawei.moni.zview.SubAddButton
            android:id="@+id/myview"
            android:layout_width="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_marginTop="40dp"
            android:layout_height="wrap_content"/>
    </LinearLayout>

</LinearLayout>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值