MVP+Retrofit+Cart

Main

import android.os.Bundle;

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 android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.lenovo.cart03.adapter.CartAdapter;
import com.example.lenovo.cart03.adapter.ChildAdapter;
import com.example.lenovo.cart03.bean.CartBean;
import com.example.lenovo.cart03.cart.presenter.CartPresenter;
import com.example.lenovo.cart03.cart.view.IView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;



public class MainActivity extends AppCompatActivity implements IView{


    @BindView(R.id.rv_top)
    RelativeLayout rvTop;
    @BindView(R.id.cb_total_select)
    CheckBox cbTotalSelect;
    @BindView(R.id.txt_total_price)
    TextView txtTotalPrice;
    @BindView(R.id.btn_calu)
    Button btnCalu;
    @BindView(R.id.rl_bottom)
    RelativeLayout rlBottom;
    @BindView(R.id.rv_shopper)
    RecyclerView rvShopper;





    private List list;
    private CartPresenter presenter;
    private CartAdapter cartAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        initData();

    }

    private void initData() {

        presenter = new CartPresenter();
        presenter.attach(this);
        presenter.getCart(71);

        list = new ArrayList<>();

        cartAdapter = new CartAdapter(this,list);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
        rvShopper.setLayoutManager(layoutManager);

        rvShopper.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));

        cartAdapter.setCartClickListener(new CartAdapter.onCartClickListener() {
            @Override
            public void onCartListener(int position, boolean isChecked) {

                if (!isChecked){
                    cbTotalSelect.setChecked(false);
                }else {
                    boolean isAllCartSelected = true;
                    for (CartBean.DataBean dataBean : list) {
                        if (!dataBean.isChecked()){
                            isAllCartSelected = false;
                            break;
                        }
                    }
                    cbTotalSelect.setChecked(isAllCartSelected);
                    AllSum();
                }

            }


        });

        cartAdapter.setAddCreateClickListener(new ChildAdapter.onAddCreateClickListener() {
            @Override
            public void onChange(int position, int num) {
                AllSum();
            }
        });

        rvShopper.setAdapter(cartAdapter);
        cbTotalSelect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                boolean checked = cbTotalSelect.isChecked();
                for (CartBean.DataBean dataBean : list) {

                    dataBean.setChecked(checked);
                    for (CartBean.DataBean.ListBean listBean : dataBean.getList()) {
                        listBean.setChecked(checked);
                    }

                }
                AllSum();
                cartAdapter.notifyDataSetChanged();

            }
        });


    }


    public void AllSum(){
        float sum = 0;
        for (CartBean.DataBean dataBean : list) {

            for (CartBean.DataBean.ListBean listBean : dataBean.getList()) {

                if (listBean.isChecked()){
                    sum+=listBean.getPrice()*listBean.getNum();
                }
            }

        }
       txtTotalPrice.setText("总价"+sum);

    }

    @Override
    public void getCart(List dataBeans) {

        Toast.makeText(this,"sdgsdf"+dataBeans,Toast.LENGTH_LONG).show();

        if (dataBeans!= null){
            list.clear();
            list.addAll(dataBeans);
          cartAdapter.notifyDataSetChanged();
        }

    }

    @Override
    public void faild(Throwable t) {

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.detach();
    }
}

 

 

 

//Utils

CartApi

public interface CartApi {


    //购物车
    @GET("product/getCarts")
    Observable cart1(@Query("uid") int uid);

}

APP

public class CartApp extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        Fresco.initialize(this);
    }
}

ImgUtils

public class ImgUtils {

    public static String getImg(String url){
        String replace = url.replace("https","http");
        String[] split = replace.split("\\|");
        return split[0];
    }

}

 

RetrofitManager

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 *
 */

public class RetrofitManager {

    private static final String BASE_URL = "http://www.zhaoapi.cn/";

    private Retrofit retrofit;

    private static final class SINGLE{
        private static RetrofitManager INSTANCE = new RetrofitManager();
    }

    public static RetrofitManager getInstance(){
        return SINGLE.INSTANCE;
    }

    private RetrofitManager(){
        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .client(buildOkhttpClient())
                .build();
    }


    private OkHttpClient buildOkhttpClient(){
        return new OkHttpClient.Builder()
                .readTimeout(5000, TimeUnit.MILLISECONDS)
                .writeTimeout(5000,TimeUnit.MILLISECONDS)
                .build();
    }


    public Retrofit getRetrofit(){
        return retrofit;
    }

    public  T create(Class clazz){
        return retrofit.create(clazz);
    }

}

 

AddDecreaseView类

package com.example.lenovo.cart03.inter;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.lenovo.cart03.R;

import butterknife.BindView;

/**
 * 

 */

public class AddDecreaseView extends LinearLayout implements View.OnClickListener {
    @BindView(R.id.txt_decrease)
    TextView txtDecrease;
    @BindView(R.id.txt_add)
    TextView txtAdd;
    @BindView(R.id.txt_num)
    TextView txtNum;
    private int num;

    public void setNum(int num) {
        this.num = num;
        txtNum.setText(num+"");
    }

    public int getNum(int num){
        return num;
    }




    //接口回调
    public interface OnAddDecreaseClickListener{
        void add(int num);

        void decrease(int num);

    }

    private OnAddDecreaseClickListener clickListener;

    public void setClickListener(OnAddDecreaseClickListener clickListener){
        this.clickListener = clickListener;
    }


    public AddDecreaseView(Context context) {
        this(context, null);
    }

    public AddDecreaseView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public AddDecreaseView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {

        View.inflate(context, R.layout.item_add, this);
        txtAdd = findViewById(R.id.txt_add);
        txtDecrease = findViewById(R.id.txt_decrease);
        txtNum = findViewById(R.id.txt_num);
        txtAdd.setOnClickListener(this);
        txtDecrease.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.txt_add:
                num++;
                txtNum.setText(num+"");
                if (clickListener != null){
                    clickListener.add(num);
                }
                break;


            case R.id.txt_decrease:
                if (num>1){
                    num--;
                }
                txtNum.setText(num+"");
                if (clickListener!=null){
                    clickListener.decrease(num);
                }
                break;
        }
    }

}

 

MVP

View

public interface IView {

    void getCart(List list);

    void faild(Throwable t);

}

Model

public class CartModel {

    public Observable getcart(int uid){
        CartApi cartApi = RetrofitManager.getInstance().create(CartApi.class);
        Observable cartBeanObservable = cartApi.cart1(uid);

        return cartBeanObservable;
    }

}

 

Presenter

package com.example.lenovo.cart03.cart.presenter;

import com.example.lenovo.cart03.bean.CartBean;
import com.example.lenovo.cart03.cart.model.CartModel;
import com.example.lenovo.cart03.cart.view.IView;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * 
 */

public class CartPresenter {

    private CartModel model;
    private IView iv;

    public void attach(IView iv){
        this.iv = iv;
        model = new CartModel();

    }


    public void detach(){
        if (iv!=null){
            iv=null;
        }
    }

   public void getCart(int uid){

        model.getcart(uid)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer() {
                    @Override
                    public void accept(CartBean cartBean) throws Exception {
                        if (cartBean != null & "0".equals(cartBean.getCode())) {
                            if (iv != null) {
                                iv.getCart(cartBean.getData());
                            }
                            return;
                        }
                        if (iv != null) {
                            iv.faild(new Throwable("" + cartBean.getMsg()));
                        }
                    }
                }, new Consumer() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        if (iv!=null){
                            iv.faild(throwable);
                        }
                    }
                });

   }


}

 

CartAdapter

 

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.example.lenovo.cart03.R;
import com.example.lenovo.cart03.bean.CartBean;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 
 */

public class CartAdapter extends RecyclerView.Adapter {


    //接口回调一级列表商家
    public interface onCartClickListener{
        void onCartListener(int position,boolean isChecked);
    }

    private onCartClickListener cartClickListener;

    public void setCartClickListener(onCartClickListener cartClickListener){
        this.cartClickListener= cartClickListener;
    }


    // 二级列表的加减器监听
    private ChildAdapter.onAddCreateClickListener addCreateClickListener;

    public void setAddCreateClickListener(ChildAdapter.onAddCreateClickListener addCreateClickListener){
        this.addCreateClickListener = addCreateClickListener;
    }

    private Context context;
    private List list;

    public CartAdapter(Context context, List list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {

        View v = View.inflate(context, R.layout.item_cart, null);

        return new Holder(v);
    }

    @Override
    public void onBindViewHolder(Holder holder, final int position) {

        final CartBean.DataBean dataBean = list.get(position);

        holder.txtShopperName.setText(list.get(position).getSellerName());


        //产品列表RecyclerView适配器+布局管理器
        final RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(context);
        final ChildAdapter childAdapter = new ChildAdapter(context,dataBean.getList());
        holder.rvProduct.setLayoutManager(layoutManager);


        if (addCreateClickListener!=null){
            childAdapter.setAddCreateClickListener(addCreateClickListener);
        }


        childAdapter.setChildClickListener(new ChildAdapter.onChildClickListener() {
            @Override
            public void onChildListener(int position, boolean isChecked) {
                if (!isChecked){
                    dataBean.setChecked(false);
                    cartClickListener.onCartListener(position,false);
                }else {
                    boolean isAllChildSelected = true;
                    for (CartBean.DataBean.ListBean listBean : dataBean.getList()) {
                        if (!listBean.isChecked()) {
                            isAllChildSelected = false;
                            break;
                        }
                    }
                    dataBean.setChecked(isAllChildSelected);
                    cartClickListener.onCartListener(position, true);

                }
                notifyDataSetChanged();

            }
        });

        holder.rvProduct.setAdapter(childAdapter);

        holder.cbShopperBox.setOnCheckedChangeListener(null);
        holder.cbShopperBox.setChecked(dataBean.isChecked());

        holder.cbShopperBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {

                dataBean.setChecked(b);
                for (CartBean.DataBean.ListBean listBean : dataBean.getList()) {
                    listBean.setChecked(b);
                }

                childAdapter.notifyDataSetChanged();

                if (cartClickListener != null){
                    cartClickListener.onCartListener(position,b);
                }


            }
        });

    }

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

    class Holder extends RecyclerView.ViewHolder {
        @BindView(R.id.cb_shopper_box)
        CheckBox cbShopperBox;
        @BindView(R.id.txt_shopper_name)
        TextView txtShopperName;
        @BindView(R.id.rv_product)
        RecyclerView rvProduct;
        public Holder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }

}

 

ChildAdapter

package com.example.lenovo.cart03.adapter;

import android.content.Context;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.example.lenovo.cart03.R;
import com.example.lenovo.cart03.bean.CartBean;
import com.example.lenovo.cart03.inter.AddDecreaseView;
import com.example.lenovo.cart03.utils.ImgUtils;
import com.facebook.drawee.view.SimpleDraweeView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 *
 */

public class ChildAdapter extends RecyclerView.Adapter {

    //二级列表
    public interface onChildClickListener {
        void onChildListener(int position, boolean isChecked);
    }

    private onChildClickListener childClickListener;

    public void setChildClickListener(onChildClickListener childClickListener) {
        this.childClickListener = childClickListener;
    }


    // 加减器发生变化的监听
    public interface onAddCreateClickListener {
        void onChange(int position, int num);
    }

    private onAddCreateClickListener addCreateClickListener;

    public void setAddCreateClickListener(onAddCreateClickListener addCreateClickListener) {
        this.addCreateClickListener = addCreateClickListener;
    }


    private Context context;
    private List list;

    public ChildAdapter(Context context, List list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {

        View v = View.inflate(context, R.layout.item_cart_child, null);
        return new Holder(v);
    }

    @Override
    public void onBindViewHolder(Holder holder, final int position) {

        final CartBean.DataBean.ListBean listBean = list.get(position);

        holder.txtProductName.setText(listBean.getTitle());
        holder.txtSinglePrice.setText(listBean.getPrice() + "");
        holder.imgProduct.setImageURI(Uri.parse(ImgUtils.getImg(listBean.getImages())));
        holder.advProduct.setNum(listBean.getNum());

        //复选框
        holder.cbProduct.setOnCheckedChangeListener(null);
        holder.cbProduct.setChecked(listBean.isChecked());
        holder.cbProduct.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {

                listBean.setChecked(b);
                if (childClickListener != null) {
                    childClickListener.onChildListener(position, b);
                }

            }
        });

        //加减器事件
        holder.advProduct.setClickListener(new AddDecreaseView.OnAddDecreaseClickListener() {
            @Override
            public void add(int num) {
                listBean.setNum(num);
                if (addCreateClickListener != null) {
                    addCreateClickListener.onChange(position, num);
                }
            }

            @Override
            public void decrease(int num) {
                listBean.setNum(num);
                if (addCreateClickListener != null) {
                    addCreateClickListener.onChange(position, num);
                }
            }
        });


    }

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

    class Holder extends RecyclerView.ViewHolder {
        @BindView(R.id.cb_product)
        CheckBox cbProduct;
        @BindView(R.id.img_product)
        SimpleDraweeView imgProduct;
        @BindView(R.id.adv_product)
        AddDecreaseView advProduct;
        @BindView(R.id.txt_product_name)
        TextView txtProductName;
        @BindView(R.id.txt_single_price)
        TextView txtSinglePrice;
        public Holder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }
}

 

 

MainLayout










    

    




    

        



        


        

 

AddLayout





    


    

    

 

CartLayout





    

        

        

    


    

 

ChildLayout





    


    



    


    

        

        
    


 

你可能感兴趣的:(MVP+Retrofit+Cart)