商品購物車整合
阿新 • • 發佈:2018-12-17
//ShopCartFragment /** * A simple {@link Fragment} subclass. */ public class ShopCartFragment extends Fragment implements ICartsView,CartAllCheckLinstener { @BindView(R.id.xRecycler_View) XRecyclerView xRecyclerView; @BindView(R.id.Allcheck_box) CheckBox AllcheckBox; @BindView(R.id.sumprice) TextView sumprice; @BindView(R.id.buy) TextView buy; Unbinder unbinder; private CartsPresent cartsPresent; private List<CartsBean.DataBean> list; private CartAdapter cartAdapter; public ShopCartFragment() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View inflate = inflater.inflate(R.layout.fragment_shop_cart, container, false); initData(); unbinder = ButterKnife.bind(this, inflate); initView(); return inflate; } private void initView() { list = new ArrayList<>(); xRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); AllcheckBox.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (AllcheckBox.isChecked()) { if (list != null && list.size() > 0) { for (int i = 0; i < list.size(); i++) { list.get(i).setSelected(true); for (int i1 = 0; i1 < list.get(i).getList().size(); i1++) { list.get(i).getList().get(i1).setSelected(true); } } } } else { if (list != null && list.size() > 0) { for (int i = 0; i < list.size(); i++) { list.get(i).setSelected(false); for (int i1 = 0; i1 < list.get(i).getList().size(); i1++) { list.get(i).getList().get(i1).setSelected(false); } } } } cartAdapter.notifyDataSetChanged(); totalPrice(); } }); } private void totalPrice() { double totalprice = 0; for (int i = 0; i < cartAdapter.getCartList().size(); i++) { for (int i1 = 0; i1 < cartAdapter.getCartList().get(i).getList().size(); i1++) { if (cartAdapter.getCartList().get(i).getList().get(i1).isSelected()) { CartsBean.DataBean.ListBean listBean = cartAdapter.getCartList().get(i).getList().get(i1); totalprice += listBean.getBargainPrice() * listBean.getTotalNum(); } } } sumprice.setText("" + totalprice); } private void initData() { cartsPresent = new CartsPresent(this); cartsPresent.getCarts("71"); } @Override public void success(CartsBean cartsBean) { if(cartsBean!=null&&cartsBean.getData()!=null){ list = cartsBean.getData(); xRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false)); cartAdapter = new CartAdapter(getActivity(),list); xRecyclerView.setAdapter(cartAdapter); }else { Toast.makeText(getActivity(),"沒有資料",Toast.LENGTH_LONG).show(); } cartAdapter.setCartAllCheckLinstener(this); } @Override public void notifyAllCheckboxStatus() { StringBuilder stringBuilder = new StringBuilder(); if (cartAdapter != null) { for (int i = 0; i < cartAdapter.getCartList().size(); i++) { stringBuilder.append(cartAdapter.getCartList().get(i).isSelected()); for (int i1 = 0; i1 < cartAdapter.getCartList().get(i).getList().size(); i1++) { stringBuilder.append(cartAdapter.getCartList().get(i).getList().get(i1).isSelected()); } } } if (stringBuilder.toString().contains("false")) { AllcheckBox.setChecked(false); } else { AllcheckBox.setChecked(true); } totalPrice(); } @Override public void Error(String msg) { } @Override public void onDestroyView() { super.onDestroyView(); unbinder.unbind(); } }
<LinearLayout 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" android:orientation="vertical" tools:context=".fragment.ShopCartFragment"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="購物車" android:textSize="25sp" android:gravity="center" android:textColor="@color/colorPrimary" /> <com.jcodecraeer.xrecyclerview.XRecyclerView android:id="@+id/xRecycler_View" android:layout_width="wrap_content" android:layout_height="0dp" android:layout_weight="1" ></com.jcodecraeer.xrecyclerview.XRecyclerView> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="center_vertical" android:padding="10dp" > <CheckBox android:id="@+id/Allcheck_box" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:textStyle="bold" android:textColor="@color/colorPrimary" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20sp" android:paddingLeft="10dp" android:text="全選" /> <LinearLayout android:padding="10dp" android:layout_marginLeft="10dp" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content"> <LinearLayout android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:textSize="20dp" android:text="總價:¥:" android:textColor="#e53e42" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/sumprice" android:layout_width="70dp" android:layout_height="25dp" android:layout_marginLeft="10dp" android:textSize="20dp" android:text="0" /> </LinearLayout> </LinearLayout> <TextView android:id="@+id/buy" android:text="去結算" android:gravity="center" android:textColor="#fff" android:background="#ff00" android:layout_width="70dp" android:layout_height="40dp" /> </LinearLayout> </LinearLayout>
public class CartAdapter extends RecyclerView.Adapter<CartViewHoder>implements CheckListener,ItemTouchHelperAdapter { Context context; List<CartsBean.DataBean> list; private CartAllCheckLinstener cartAllCheckLinstener; public CartAdapter(Context context, List<CartsBean.DataBean> list) { this.context = context; this.list = list; } public List<CartsBean.DataBean> getCartList(){ return list; } public void setCartAllCheckLinstener(CartAllCheckLinstener cartAllCheckLinstener) { this.cartAllCheckLinstener = cartAllCheckLinstener; } @NonNull @Override public CartViewHoder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { CartViewHoder hoder =new CartViewHoder(LayoutInflater.from(context).inflate(R.layout.cart_item,parent,false)); return hoder; } @Override public void onBindViewHolder(@NonNull final CartViewHoder holder, int position) { final CartsBean.DataBean bean = list.get(position); holder.shopCart.setText(list.get(position).getSellerName()); holder.shopCartSelect.setChecked(bean.isSelected()); holder.ProductRecyclerView.setLayoutManager(new LinearLayoutManager(context)); ProductApdater productApdater = new ProductApdater(bean.getList(),context); holder.ProductRecyclerView.setAdapter(productApdater); productApdater.setCheckListener(this); for (int i = 0; i < bean.getList().size(); i++) { if(!bean.getList().get(i).isSelected()){ holder.shopCartSelect.setChecked(false); break; }else{ holder.shopCartSelect.setChecked(true); } } holder.shopCartSelect.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if(holder.shopCartSelect.isChecked()){ bean.setSelected(true); for (int i = 0; i < bean.getList().size(); i++) { bean.getList().get(i).setSelected(true); } }else{ bean.setSelected(false); for (int i = 0; i < bean.getList().size(); i++) { bean.getList().get(i).setSelected(false); } } notifyDataSetChanged(); if(cartAllCheckLinstener!=null){ cartAllCheckLinstener.notifyAllCheckboxStatus(); } } }); } @Override public int getItemCount() { return list.size()==0?0:list.size(); } @Override public void notifyParpen() { notifyDataSetChanged(); if (cartAllCheckLinstener!=null){ cartAllCheckLinstener.notifyAllCheckboxStatus(); } } @Override public void onItemMove(int fromPosition, int toPosition) { Collections.swap(list,fromPosition,toPosition); notifyItemMoved(fromPosition,toPosition); } @Override public void onItemDismiss(int position) { list.remove(position); notifyItemRemoved(position); } }
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content"> <LinearLayout android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content"> <CheckBox android:id="@+id/shop_Cart_select" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/shop_cart" android:text="商家" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout android:layout_marginTop="30dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <View android:layout_width="wrap_content" android:layout_height="1dp"></View> <android.support.v7.widget.RecyclerView android:id="@+id/Product_Recycler_View" android:layout_width="wrap_content" android:layout_height="wrap_content"></android.support.v7.widget.RecyclerView> </LinearLayout> </LinearLayout>
class ProductApdater extends RecyclerView.Adapter<ProductViewHoder> { List<CartsBean.DataBean.ListBean> list; Context contex; private CheckListener checkListener; private CartAllCheckLinstener cartAllCheckLinstener; public ProductApdater(List<CartsBean.DataBean.ListBean> list, Context contex) { this.list = list; this.contex = contex; } public void setCheckListener(CheckListener checkListener) { this.checkListener = checkListener; } public void setCartAllCheckLinstener(CartAllCheckLinstener cartAllCheckLinstener) { this.cartAllCheckLinstener = cartAllCheckLinstener; } @NonNull @Override public ProductViewHoder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { ProductViewHoder hoder = new ProductViewHoder(LayoutInflater.from(contex).inflate(R.layout.product_item, parent, false)); return hoder; } @Override public void onBindViewHolder(@NonNull final ProductViewHoder holder, int position) { final CartsBean.DataBean.ListBean bean = list.get(position); holder.ProcuctCheckbox.setChecked(bean.isSelected()); holder.price.setText("優惠價:¥" + list.get(position).getBargainPrice()); holder.tv_name.setText(list.get(position).getTitle()); holder.productIcon.setImageURI(Uri.parse(list.get(position).getImages().split("\\|")[0])); String[] split = bean.getImages().split("\\|"); if (split != null && split.length > 0) { Glide.with(contex).load(split[0]).into(holder.productIcon); } else { holder.productIcon.setImageResource(R.mipmap.ic_launcher); } holder.ProcuctCheckbox.setChecked(bean.isSelected()); holder.myAddReduce.setNumEt(bean.getTotalNum()); holder.myAddReduce.setJiaJianLinstener(new My_add_reduce.JiaJianLinstener() { @Override public void getNum(int num) { bean.setTotalNum(num); if (checkListener != null) { checkListener.notifyParpen(); } } }); holder.ProcuctCheckbox.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (holder.ProcuctCheckbox.isChecked()) { bean.setSelected(true); } else { bean.setSelected(false); } if (checkListener != null) { checkListener.notifyParpen(); } //EventBus.getDefault().post(0); } }); } @Override public int getItemCount() { return list.size() == 0 ? 0 : list.size(); } }
<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:padding="10dp" android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content"> <LinearLayout android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content"> <CheckBox android:id="@+id/ProcuctCheckbox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" /> <ImageView android:id="@+id/product_icon" android:src="@mipmap/ic_launcher" android:layout_width="80dp" android:layout_height="80dp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:layout_marginLeft="10dp" > <TextView android:id="@+id/tv_product_name" android:text="商品標題" android:layout_width="match_parent" android:layout_height="wrap_content" /> <RelativeLayout android:layout_marginTop="10dp" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/price" android:layout_alignParentLeft="true" android:text="優惠價¥:99.99" android:textSize="15sp" android:textColor="#ff0000" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <com.bwie.my_demo3.mvp.view.My_add_reduce android:id="@+id/jiajianqi" android:layout_alignParentRight="true" android:layout_width="wrap_content" android:layout_height="wrap_content"></com.bwie.my_demo3.mvp.view.My_add_reduce> </RelativeLayout> </LinearLayout> </LinearLayout> <View android:layout_marginTop="10dp" android:layout_width="match_parent" android:layout_height="1px" android:background="#999999"></View> </LinearLayout>
public class My_add_reduce extends LinearLayout { private TextView jiaTv,jiantv; private EditText numEt; private int num = 1; public My_add_reduce(Context context) { this(context,null); } public My_add_reduce(Context context, @Nullable AttributeSet attrs) { this(context, attrs,0); } public My_add_reduce(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(context); } private void init(Context context) { View view = LayoutInflater.from(context).inflate(R.layout.num_layout, this, true); jiantv = view.findViewById( R.id.jian); jiaTv = view.findViewById(R.id.jia); numEt = view.findViewById(R.id.num); numEt.setText(num+""); jiaTv.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { num++; numEt.setText(num+""); if(jiaJianLinstener!=null){ jiaJianLinstener.getNum(num); } } }); jiantv.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { num--; if(num<=0){ Toast.makeText(getContext(),"數量不能小於1" , Toast.LENGTH_SHORT).show(); num=1; } numEt.setText(num+""); if(jiaJianLinstener!=null){ jiaJianLinstener.getNum(num); } } }); } public void setNumEt(int n){ numEt.setText(n+""); num = Integer.parseInt(numEt.getText().toString()); } private JiaJianLinstener jiaJianLinstener; public void setJiaJianLinstener(JiaJianLinstener jiaJianLinstener) { this.jiaJianLinstener = jiaJianLinstener; } public interface JiaJianLinstener{ void getNum(int num); }
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/jian" android:text="-" android:textSize="25dp" android:padding="5dp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <View android:layout_width="1dp" android:layout_height="match_parent" android:background="#999999"/> <EditText android:id="@+id/num" android:layout_weight="1" android:text="1" android:gravity="center" android:background="#00000000" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <View android:layout_width="1dp" android:layout_height="match_parent" android:background="#999999"/> <TextView android:id="@+id/jia" android:text="+" android:textSize="25dp" android:padding="5dp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>