一
<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.shopping.cart.CartFragment"> <RelativeLayout android:id="@+id/ll_bottom" android:layout_width="match_parent" android:layout_height="48dp" android:layout_alignParentBottom="true"> <CheckBox android:id="@+id/cb_total_select" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:text="全选"/> <TextView android:id="@+id/txt_total_price" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="10dp" android:layout_toRightOf="@id/cb_total_select" android:textColor="#c23636"/> </RelativeLayout> <android.support.v7.widget.RecyclerView android:id="@+id/rv_shopper" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@id/ll_bottom"></android.support.v7.widget.RecyclerView> </RelativeLayout>
二
package com.example.shopping.httputils; import java.util.concurrent.TimeUnit; import okhttp3.OkHttpClient; import retrofit2.Retrofit; import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory; import retrofit2.converter.gson.GsonConverterFactory; /** * Created by 。。。。 on 2018/11/17. */ public class HttpUtils { private static final String Url="http://120.27.23.105/"; private final Retrofit retrofit; private static final class instanc{ private static final HttpUtils instances=new HttpUtils(); } public static HttpUtils getinstance(){ return instanc.instances; } private HttpUtils(){ retrofit = new Retrofit.Builder() .baseUrl(Url) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .addConverterFactory(GsonConverterFactory.create()) .client(okHttpClient()) .build(); } private OkHttpClient okHttpClient(){ return new OkHttpClient.Builder() .writeTimeout(5000, TimeUnit.MILLISECONDS) .readTimeout(5000,TimeUnit.MILLISECONDS) .build(); } public Retrofit getRetrofit(){ return retrofit; } public <T> T create(Class<T> tClass){ return retrofit.create(tClass); } }
三
package com.example.shopping.api; import com.example.shopping.bean.CartBean; import io.reactivex.Observable; import retrofit2.http.GET; /** * Created by 。。。。 on 2018/11/17. */ public interface APIService { @GET("product/getCarts?uid=1538") Observable<CartBean> getcart(); }
四
package com.example.shopping.iview; import com.example.shopping.bean.CartBean; /** * Created by 。。。。 on 2018/11/17. */ public interface CartIVew { void OnSuccess(CartBean cartBean); void OnFailed(Throwable t); }
五
package com.example.shopping.model; import com.example.shopping.api.APIService; import com.example.shopping.bean.CartBean; import com.example.shopping.httputils.HttpUtils; import com.example.shopping.iview.CartIVew; import io.reactivex.Observable; /** * Created by 。。。。 on 2018/11/17. */ public class CartModel { public Observable<CartBean> getcart(){ APIService apiService= HttpUtils.getinstance().create(APIService.class); Observable<CartBean> cartBeanObservable=apiService.getcart(); return cartBeanObservable; } }
六
package com.example.shopping.presenter; import com.example.shopping.bean.CartBean; import com.example.shopping.iview.CartIVew; import com.example.shopping.model.CartModel; import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.functions.Consumer; import io.reactivex.schedulers.Schedulers; /** * Created by 。。。。 on 2018/11/17. */ public class Presenter { private CartIVew iv; private final CartModel model; public void attach(CartIVew iv) { this.iv = iv; } public Presenter(){ model = new CartModel(); } public void dettach(){ iv=null; } public void getcart(){ model.getcart() .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Consumer<CartBean>() { @Override public void accept(CartBean cartBean) throws Exception { iv.OnSuccess(cartBean); } }, new Consumer<Throwable>() { @Override public void accept(Throwable throwable) throws Exception { iv.OnFailed(throwable); } }); } }
七
package com.example.shopping.adapter; 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.shopping.R; import com.example.shopping.bean.CartBean; import java.util.List; /** * Created by 。。。。 on 2018/11/17. */ public class ShoppingAdapter extends RecyclerView.Adapter<ShoppingAdapter.ViewHolder> { private Context context; private List<CartBean.DataBean> list; public ShoppingAdapter(Context context, List<CartBean.DataBean> list) { this.context = context; this.list = list; } public interface OnShoppingClickListener{ void OnShoppingClick(int postion,boolean ischeked); } private OnShoppingClickListener shoppingClickListener; public void setOnShoppingClickListener(OnShoppingClickListener listener){ this.shoppingClickListener=listener; } private ProductAdapter.OnAddDecreaseProductListener productListener; public void setOnAddDecreaseProductListener(ProductAdapter.OnAddDecreaseProductListener listener){ this.productListener=listener; } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View v=View.inflate(context, R.layout.item_shopping,null); ViewHolder holder=new ViewHolder(v); return holder; } @Override public void onBindViewHolder(ViewHolder holder, final int position) { final CartBean.DataBean dataBean = list.get(position); holder.txtshoppername.setText(list.get(position).getSellerName()); RecyclerView.LayoutManager layoutManager=new LinearLayoutManager(context); holder.rvproduct.setLayoutManager(layoutManager); final ProductAdapter adapter = new ProductAdapter(context,dataBean.getList()); if(productListener!=null){ adapter.setOnAddDecreaseProductListener(productListener); } adapter.setOnProductClickListener(new ProductAdapter.OnProductClickListener() { @Override public void OnProductClickListener(int postion, boolean ischecked) { if(!ischecked){ dataBean.setChecked(false); shoppingClickListener.OnShoppingClick(postion,false); }else{ boolean isAllProductSelected=true; for (CartBean.DataBean.ListBean listBean : dataBean.getList()) { if(!listBean.isChecked()){ isAllProductSelected=false; break; } } dataBean.setChecked(isAllProductSelected); shoppingClickListener.OnShoppingClick(postion,true); } notifyDataSetChanged(); productListener.onChange(0,0); } }); holder.rvproduct.setAdapter(adapter); holder.cbshopper.setOnCheckedChangeListener(null); holder.cbshopper.setChecked(dataBean.isChecked()); holder.cbshopper.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { List<CartBean.DataBean.ListBean> productlist = dataBean.getList(); for (CartBean.DataBean.ListBean producta : productlist) { producta.setChecked(isChecked); } adapter.notifyDataSetChanged(); if(shoppingClickListener!=null){ shoppingClickListener.OnShoppingClick(position,isChecked); } } }); } @Override public int getItemCount() { return list.size(); } class ViewHolder extends RecyclerView.ViewHolder { private final CheckBox cbshopper; private final TextView txtshoppername; private final RecyclerView rvproduct; public ViewHolder(View itemView) { super(itemView); cbshopper = itemView.findViewById(R.id.cb_shopping); txtshoppername = itemView.findViewById(R.id.txt_shopping_name); rvproduct = itemView.findViewById(R.id.rv_product); } } }
八
package com.example.shopping.adapter; import android.content.Context; 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.shopping.R; import com.example.shopping.adddecrease.AddDecrease; import com.example.shopping.bean.CartBean; import com.facebook.drawee.view.SimpleDraweeView; import java.util.List; /** * Created by 。。。。 on 2018/11/17. */ public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.ViewHolder> { private Context context; private List<CartBean.DataBean.ListBean> list; public ProductAdapter(Context context, List<CartBean.DataBean.ListBean> list) { this.context = context; this.list = list; } public interface OnProductClickListener{ void OnProductClickListener(int postion,boolean ischecked); } private OnProductClickListener productClickListener; public void setOnProductClickListener(OnProductClickListener listener){ this.productClickListener=listener; } public interface OnAddDecreaseProductListener{ void onChange(int postion,int num); } private OnAddDecreaseProductListener productListener; public void setOnAddDecreaseProductListener(OnAddDecreaseProductListener listener){ this.productListener=listener; } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View v=View.inflate(context, R.layout.item_product,null); ViewHolder holder=new ViewHolder(v); return holder; } @Override public void onBindViewHolder(ViewHolder holder, final int position) { final CartBean.DataBean.ListBean listBean = list.get(position); holder.imgproduct.setImageURI(listBean.getImages().split("\\|")[0]); holder.txtproductname.setText(listBean.getTitle()); holder.txtsingleprice.setText(listBean.getPrice()+""); holder.advproduct.setNum(listBean.getNum()); holder.advproduct.setOnAddDecreaseListener(new AddDecrease.OnAddDecreaseListener() { @Override public void add(int num) { listBean.setNum(num); if(productListener!=null){ productListener.onChange(position,num); } } @Override public void decrease(int num) { listBean.setNum(num); if(productListener!=null){ productListener.onChange(position,num); } } }); holder.cbproduct.setOnCheckedChangeListener(null); holder.cbproduct.setChecked(listBean.isChecked()); holder.cbproduct.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { listBean.setChecked(isChecked); if(productClickListener!=null){ productClickListener.OnProductClickListener(position,isChecked); } } }); } @Override public int getItemCount() { return list.size(); } class ViewHolder extends RecyclerView.ViewHolder { private final CheckBox cbproduct; private final SimpleDraweeView imgproduct; private final TextView txtsingleprice; private final AddDecrease advproduct; private final TextView txtproductname; public ViewHolder(View itemView) { super(itemView); cbproduct = itemView.findViewById(R.id.cb_product); imgproduct = itemView.findViewById(R.id.img_product); txtsingleprice = itemView.findViewById(R.id.txt_single_price); advproduct = itemView.findViewById(R.id.adv_product); txtproductname = itemView.findViewById(R.id.txt_product_name); } } }
九
package com.example.shopping.cart; import android.os.Bundle; 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.RelativeLayout; import android.widget.TextView; import com.example.shopping.R; import com.example.shopping.adapter.ProductAdapter; import com.example.shopping.adapter.ShoppingAdapter; import com.example.shopping.bean.CartBean; import com.example.shopping.iview.CartIVew; import com.example.shopping.presenter.Presenter; import java.util.ArrayList; import java.util.List; import butterknife.BindView; import butterknife.ButterKnife; import butterknife.Unbinder; public class CartFragment extends Fragment implements CartIVew { @BindView(R.id.cb_total_select) CheckBox cbTotalSelect; @BindView(R.id.txt_total_price) TextView txtTotalPrice; @BindView(R.id.ll_bottom) RelativeLayout llBottom; @BindView(R.id.rv_shopper) RecyclerView rvShopper; Unbinder unbinder; private List<CartBean.DataBean> list; private ShoppingAdapter adapter; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment_cart, container, false); unbinder = ButterKnife.bind(this, v); list = new ArrayList<>(); setlistener(); adapter = new ShoppingAdapter(getActivity(), list); adapter.setOnShoppingClickListener(new ShoppingAdapter.OnShoppingClickListener() { @Override public void OnShoppingClick(int postion, boolean ischeked) { if(!ischeked){ cbTotalSelect.setChecked(false); }else{ boolean isAddShopperChecked=true; for (CartBean.DataBean listshopping : list) { if(!listshopping.isChecked()){ isAddShopperChecked=false; break; } } cbTotalSelect.setChecked(isAddShopperChecked); } calculatePrice(); } }); adapter.setOnAddDecreaseProductListener(new ProductAdapter.OnAddDecreaseProductListener() { @Override public void onChange(int postion, int num) { calculatePrice(); } }); RecyclerView.LayoutManager layoutManager=new LinearLayoutManager(getActivity()); rvShopper.setLayoutManager(layoutManager); rvShopper.setAdapter(adapter); Presenter presenter=new Presenter(); presenter.attach(this); presenter.getcart(); return v; } private void setlistener() { cbTotalSelect.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { boolean isChecked=cbTotalSelect.isChecked(); for (CartBean.DataBean listshopping : list) { listshopping.setChecked(isChecked); List<CartBean.DataBean.ListBean> product = listshopping.getList(); for (CartBean.DataBean.ListBean listBean : product) { listBean.setChecked(isChecked); } } calculatePrice(); adapter.notifyDataSetChanged(); } }); } private void calculatePrice(){ float totalPrice=0; for (CartBean.DataBean listshopping : list) { List<CartBean.DataBean.ListBean> list = listshopping.getList(); for (CartBean.DataBean.ListBean product : list) { if(product.isChecked()){ totalPrice+=product.getNum()*product.getPrice(); } } } txtTotalPrice.setText("总价"+totalPrice); } @Override public void OnSuccess(CartBean cartBean) { list.clear(); list.addAll(cartBean.getData()); adapter.notifyDataSetChanged(); } @Override public void OnFailed(Throwable t) { } @Override public void onDestroyView() { super.onDestroyView(); unbinder.unbind(); } }
十
package com.example.shopping.adddecrease; import android.content.Context; import android.util.AttributeSet; import android.view.View; import android.widget.RelativeLayout; import android.widget.TextView; import com.example.shopping.R; /** * Created by 。。。。 on 2018/11/17. */ public class AddDecrease extends RelativeLayout implements View.OnClickListener { private int num=1; private TextView txtadd; private TextView txtdecrease; private TextView txtnum; public int getNum() { return num; } public void setNum(int num) { this.num = num; txtnum.setText(num+""); } public interface OnAddDecreaseListener{ void add(int num); void decrease(int num); } private OnAddDecreaseListener listener; public void setOnAddDecreaseListener(OnAddDecreaseListener listener){ this.listener=listener; } public AddDecrease(Context context) { this(context,null); } public AddDecrease(Context context, AttributeSet attrs) { this(context, attrs,0); } public AddDecrease(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(context); } private void init(Context context) { View.inflate(context, R.layout.item_view,this); txtadd = findViewById(R.id.txt_add); txtdecrease = findViewById(R.id.txt_decrease); txtnum = findViewById(R.id.txt_num); txtnum.setText("1"); txtadd.setOnClickListener(this); txtdecrease.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; } } }
十一
package com.example.shopping.app; import android.app.Application; import com.facebook.drawee.backends.pipeline.Fresco; /** * Created by 。。。。 on 2018/11/17. */ public class App extends Application { @Override public void onCreate() { super.onCreate(); Fresco.initialize(this); } }