api
public interface ICartAPI {
@GET("product/getCarts")
Observable<CartBean> getCart(@Query("uid") int uid);
}
view
package com.example.windows.zhoukao3_lianxi.view; import com.example.windows.zhoukao3_lianxi.bean.CartBean; /** * Created by Windows on 2018/11/17. */ public interface ICartView { void success(CartBean cartBean); void failed(Exception e); }
model
public class CartModel {
public Observable<CartBean> getCart(int uid){
ICartAPI iCartAPI = RetrofitManager.getInstance().create(ICartAPI.class);
Observable<CartBean> cartBeanObservable = iCartAPI.getCart(uid);
return cartBeanObservable;
}
}
presenter
public class CartPresenter {
private ICartView iCartView;
private CartModel cartModel;
public void attach(ICartView iCartView){
this.iCartView = iCartView;
cartModel = new CartModel();
}
public void dettach(){
if (iCartView != null){
iCartView = null;
}
}
public void getCart(int uid){
cartModel.getCart(uid)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<CartBean>() {
@Override
public void accept(CartBean cartBean) throws Exception {
if (cartBean != null && "0".equals(cartBean.getCode())){
if (iCartView != null){
iCartView.success(cartBean);
return;
}
}
if (iCartView != null){
iCartView.failed(new Exception("服务器异常"));
}
}
}, new Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) throws Exception {
if (iCartView != null){
iCartView.failed(new Exception("网络异常"));
}
}
});
}
}
加减器
package com.example.windows.zhoukao3_lianxi.subtracter;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.example.windows.zhoukao3_lianxi.R;
import butterknife.BindView;
import butterknife.OnClick;
/**
* Created by Windows on 2018/11/17.
*/
public class AddDecreaseView extends RelativeLayout implements View.OnClickListener {
private TextView txtDecrease;
private TextView txtAdd;
private TextView txtNum;
private int num = 1;
public AddDecreaseView(Context context) {
this(context, null);
}
public AddDecreaseView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public AddDecreaseView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
private void init(Context context) {
View.inflate(context, R.layout.layout_add_decrease_view, this);
txtAdd = findViewById(R.id.txt_add);
txtDecrease = findViewById(R.id.txt_decrease);
txtNum = findViewById(R.id.txt_num);
txtNum.setText(num+"");
txtDecrease.setOnClickListener(this);
txtAdd.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.txt_add:
num++;
txtNum.setText(num+"");
if (listener != null){
listener.add(num);
}
break;
case R.id.txt_decrease:
if (num>1){
num--;
}
txtNum.setText(num+"");
if (listener != null){
listener.decrease(num);
}
break;
}
}
public interface OnAddDecreaseClickListener{
void add(int num);
void decrease(int num);
}
private OnAddDecreaseClickListener listener;
public void setOnAddDecreaseClickListener(OnAddDecreaseClickListener listener){
this.listener = listener;
}
}
fragment
package com.example.windows.zhoukao3_lianxi.fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
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.TextView;
import android.widget.Toast;
import com.example.windows.zhoukao3_lianxi.R;
import com.example.windows.zhoukao3_lianxi.adapter.ProductAdapter;
import com.example.windows.zhoukao3_lianxi.adapter.ShopperAdapter;
import com.example.windows.zhoukao3_lianxi.bean.CartBean;
import com.example.windows.zhoukao3_lianxi.presenter.CartPresenter;
import com.example.windows.zhoukao3_lianxi.view.ICartView;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
public class CartFragment extends Fragment implements ICartView{
@BindView(R.id.rc_shopper)
RecyclerView rcShopper;
@BindView(R.id.cb_total)
CheckBox cbTotal;
@BindView(R.id.txt_total_price)
TextView txtTotalPrice;
private List<CartBean.DataBean> shopperList;
private ShopperAdapter shopperAdapter;
private CartPresenter cartPresenter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_cart, container, false);
ButterKnife.bind(this, view);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
cartPresenter = new CartPresenter();
cartPresenter.attach(this);
cartPresenter.getCart(21241);
shopperList = new ArrayList<>();
shopperAdapter = new ShopperAdapter(getActivity(),shopperList);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
rcShopper.setLayoutManager(layoutManager);
rcShopper.setAdapter(shopperAdapter);
//商家发生变化的监听
shopperAdapter.setonShopperClickListener(new ShopperAdapter.onShopperClickListener() {
@Override
public void onShopperClick(int position, boolean isChecked) {
//如果商家不选中,全选也不选中
if (!isChecked){
cbTotal.setChecked(false);
}else {
boolean isAllShopperChecked = true;
for (CartBean.DataBean shopper : shopperList) {
if (!shopper.isChecked()){
isAllShopperChecked = false;
break;
}
}
cbTotal.setChecked(isAllShopperChecked);
}
calulatePrice();
}
});
shopperAdapter.setonAddDecreaseProductListener(new ProductAdapter.onAddDecreaseProductListener() {
@Override
public void change(int position, int num) {
calulatePrice();
}
});
cbTotal.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean isChecked = cbTotal.isChecked();
for (CartBean.DataBean shopper : shopperList) {
shopper.setChecked(isChecked);
for (CartBean.DataBean.ListBean product : shopper.getList()) {
product.setChecked(isChecked);
}
}
shopperAdapter.notifyDataSetChanged();
}
});
}
@Override
public void success(CartBean cartBean) {
Toast.makeText(getActivity(),"请求成功",Toast.LENGTH_LONG).show();
shopperList.clear();
shopperList.addAll(cartBean.getData());
shopperAdapter.notifyDataSetChanged();
}
@Override
public void failed(Exception e) {
Toast.makeText(getActivity(),"请求失败",Toast.LENGTH_LONG).show();
}
private void calulatePrice(){
float totalPrice = 0;
for (CartBean.DataBean shopper : shopperList) {
for (CartBean.DataBean.ListBean product : shopper.getList()) {
if (product.isChecked()){
totalPrice += product.getNum()*product.getPrice();
}
}
}
txtTotalPrice.setText("总结:"+totalPrice);
}
@Override
public void onDestroyView() {
super.onDestroyView();
cartPresenter.dettach();
}
}
shopperadapter
package com.example.windows.zhoukao3_lianxi.adapter;
import android.content.Context;
import android.support.annotation.NonNull;
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.windows.zhoukao3_lianxi.R;
import com.example.windows.zhoukao3_lianxi.bean.CartBean;
import java.util.List;
/**
* Created by Windows on 2018/11/17.
*/
public class ShopperAdapter extends RecyclerView.Adapter<ShopperAdapter.ViewHolder> {
private Context context;
private List<CartBean.DataBean> shopperList;
public ShopperAdapter(Context context, List<CartBean.DataBean> shopperList) {
this.context = context;
this.shopperList = shopperList;
}
//商家的监听
public interface onShopperClickListener{
void onShopperClick(int position,boolean isChecked);
}
private onShopperClickListener shopperClickListener;
public void setonShopperClickListener(onShopperClickListener shopperClickListener){
this.shopperClickListener = shopperClickListener;
}
//商品加减器的监听
private ProductAdapter.onAddDecreaseProductListener productListener;
public void setonAddDecreaseProductListener(ProductAdapter.onAddDecreaseProductListener productListener){
this.productListener = productListener;
}
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = View.inflate(context, R.layout.layout_shopper,null);
ViewHolder holder = new ViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
final CartBean.DataBean shopper = shopperList.get(position);
holder.txtShopper.setText(shopper.getSellerName());
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(context);
final ProductAdapter productAdapter = new ProductAdapter(context,shopperList.get(position).getList());
holder.rcProduct.setLayoutManager(layoutManager);
if (productListener != null){
productAdapter.setonAddDecreaseProductListener(productListener);
}
productAdapter.setonProductClickListener(new ProductAdapter.onProductClickListener() {
@Override
public void onProductClick(int position, boolean isChecked) {
//如果商品没有选中 ,商家也是未选中
if (!isChecked){
shopper.setChecked(false);
shopperClickListener.onShopperClick(position,false);
}else {
boolean isAllProductSelected = true;
for (CartBean.DataBean.ListBean product : shopper.getList()) {
if (!product.isChecked()){
isAllProductSelected = false;
}
}
shopper.setChecked(isAllProductSelected);
shopperClickListener.onShopperClick(position,true);
}
notifyDataSetChanged();
productListener.change(0,0);
}
});
holder.rcProduct.setAdapter(productAdapter);
holder.cbShopper.setOnCheckedChangeListener(null);
holder.cbShopper.setChecked(shopper.isChecked());
holder.cbShopper.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
for (CartBean.DataBean.ListBean productList : shopper.getList()) {
productList.setChecked(isChecked);
}
productAdapter.notifyDataSetChanged();
if (shopperClickListener != null){
shopperClickListener.onShopperClick(position,isChecked);
}
}
});
}
@Override
public int getItemCount() {
return shopperList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
private CheckBox cbShopper;
private TextView txtShopper;
private RecyclerView rcProduct;
public ViewHolder(View itemView) {
super(itemView);
cbShopper = itemView.findViewById(R.id.cb_shopper);
txtShopper = itemView.findViewById(R.id.txt_shopper);
rcProduct = itemView.findViewById(R.id.rc_product);
}
}
}
productadapter
package com.example.windows.zhoukao3_lianxi.adapter;
import android.content.Context;
import android.support.annotation.NonNull;
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.windows.zhoukao3_lianxi.R;
import com.example.windows.zhoukao3_lianxi.bean.CartBean;
import com.example.windows.zhoukao3_lianxi.subtracter.AddDecreaseView;
import com.facebook.drawee.view.SimpleDraweeView;
import java.util.List;
/**
* Created by Windows on 2018/11/17.
*/
public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.ViewHolder> {
private Context context;
private List<CartBean.DataBean.ListBean> productList;
public ProductAdapter(Context context, List<CartBean.DataBean.ListBean> productList) {
this.context = context;
this.productList = productList;
}
public interface onProductClickListener{
void onProductClick(int position,boolean isChecked);
}
private onProductClickListener productClickListener;
public void setonProductClickListener(onProductClickListener productClickListener){
this.productClickListener = productClickListener;
}
//加减器变化监听
public interface onAddDecreaseProductListener{
void change(int position,int num);
}
private onAddDecreaseProductListener listener;
public void setonAddDecreaseProductListener(onAddDecreaseProductListener listener){
this.listener = listener;
}
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = View.inflate(context, R.layout.layout_product,null);
ViewHolder holder = new ViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
final CartBean.DataBean.ListBean product = productList.get(position);
holder.imageProduct.setImageURI(product.getImages().split("\\|")[0]);
holder.txtTitle.setText(product.getTitle());
holder.txtPrice.setText(String.valueOf(product.getPrice()));
holder.Subracter.setOnAddDecreaseClickListener(new AddDecreaseView.OnAddDecreaseClickListener() {
@Override
public void add(int num) {
product.setNum(num);
if (listener != null){
listener.change(position,num);
}
}
@Override
public void decrease(int num) {
product.setNum(num);
listener.change(position,num);
}
});
holder.cbProduct.setOnCheckedChangeListener(null);
holder.cbProduct.setChecked(product.isChecked());
holder.cbProduct.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
product.setChecked(isChecked);
if (productClickListener != null){
productClickListener.onProductClick(position,isChecked);
}
}
});
}
@Override
public int getItemCount() {
return productList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
private CheckBox cbProduct;
private SimpleDraweeView imageProduct;
private TextView txtTitle;
private TextView txtPrice;
private AddDecreaseView Subracter;
public ViewHolder(View itemView) {
super(itemView);
cbProduct = itemView.findViewById(R.id.cb_product);
imageProduct = itemView.findViewById(R.id.image_product);
txtTitle = itemView.findViewById(R.id.txt_title);
txtPrice = itemView.findViewById(R.id.txt_price);
Subracter = itemView.findViewById(R.id.subracter);
}
}
}
cart_xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.windows.zhoukao3_lianxi.fragment.CartFragment">
<android.support.v7.widget.RecyclerView
android:id="@+id/rc_shopper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="30dp"></android.support.v7.widget.RecyclerView>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<CheckBox
android:id="@+id/cb_total"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/txt_total_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/cb_total" />
</RelativeLayout>
</RelativeLayout>
加减器.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="70dp"
android:layout_height="wrap_content">
<TextView
android:id="@+id/txt_decrease"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:background="#ff00"
android:padding="5dp"
android:text="-" />
<TextView
android:id="@+id/txt_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="#ff00"
android:padding="5dp"
android:text="+" />
<TextView
android:id="@+id/txt_num"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="@id/txt_add"
android:layout_toRightOf="@id/txt_decrease"
android:gravity="center"
android:text="1" />
</RelativeLayout>
shpper.xml
<?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:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<CheckBox
android:id="@+id/cb_shopper"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/txt_shopper"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/rc_product"
android:layout_width="match_parent"
android:layout_height="wrap_content"></android.support.v7.widget.RecyclerView>
</LinearLayout>
product.xml
<?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:orientation="horizontal"
android:paddingLeft="20dp">
<CheckBox
android:id="@+id/cb_product"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/image_product"
android:layout_width="80dp"
android:layout_height="80dp" />
<LinearLayout
android:layout_width="300dp"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/txt_title"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/txt_price"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<com.example.windows.zhoukao3_lianxi.subtracter.AddDecreaseView
android:id="@+id/subracter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></com.example.windows.zhoukao3_lianxi.subtracter.AddDecreaseView>
</LinearLayout>