day26
购车能的功能完善
<RelativeLayout 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:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <com.google.android.material.appbar.AppBarLayout android:id="@+id/appbar" android:layout_width="match_parent" android:layout_height="wrap_content"> <androidx.appcompat.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="@color/colorAccent" app:logo="@drawable/icon_shou" app:navigationContentDescription="" app:layout_scrollFlags="scroll|enterAlways" app:navigationIcon="@drawable/icon_back"> <TextView android:id="@+id/buy_detial_title3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="购物车" android:textColor="@color/black" android:textSize="25sp" /> </androidx.appcompat.widget.Toolbar> </com.google.android.material.appbar.AppBarLayout> <androidx.core.widget.NestedScrollView android:layout_below="@+id/appbar" android:id="@+id/nestscrollcar" android:layout_height="match_parent" android:layout_width="match_parent"> <RelativeLayout android:id="@+id/relatcar" android:layout_height="wrap_content" android:layout_width="match_parent"> <androidx.recyclerview.widget.RecyclerView android:id="@+id/recyccar" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="10dp" /> <RelativeLayout android:layout_width="match_parent" android:layout_height="60dp" android:layout_below="@+id/recyccar" android:layout_alignParentBottom="true" android:paddingTop="10dp" android:paddingBottom="10dp" android:paddingLeft="5dp" android:paddingRight="5dp" android:background="#802f4f4f" android:gravity="center_vertical" > <CheckBox android:id="@+id/checkbox_all" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:checked="true" android:text="全选" android:textSize="20sp" style="@style/customCheckbox"/> <TextView android:id="@+id/txt_total" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@+id/checkbox_all" android:layout_marginLeft="28dp" android:layout_centerVertical="true" android:textSize="24dp" android:text="合计"/> <Button android:id="@+id/btn_order" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:minHeight="60dp" android:minWidth="120dp" android:text="去结算" style="@style/bigRedButton"/> <Button android:id="@+id/btn_del" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:visibility="gone" android:minHeight="60dp" android:minWidth="120dp" android:text="删除" style="@style/bigRedButton"/> </RelativeLayout> </RelativeLayout> </androidx.core.widget.NestedScrollView> </RelativeLayout>
1、要实现每个物品的点击事件
2、实现点击全选实现全部物品的checked和如果有物品没有被选中全选键不显示
3、被选中物品价格的总计算
package Home; import android.content.Context; import android.text.Html; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.CheckBox; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView; import com.bumptech.glide.Glide; import com.example.expressdelivery.R; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import Home.Data.Goods; import Home.Data.Image; import Home.Data.ShoppingCart; import Home.Data.Zuoye; import Home.utils.CartProvider; import Home.widget.NumberAddSubView; public class CarAdapter extends RecyclerView.Adapter<CarAdapter.CarViewHolder>{ private Context mcontext; private List<Goods> goods; private List<Zuoye> Zuoyes; private List<ShoppingCart> carts=new ArrayList<>(); private CheckBox checkBox; private TextView textView; private CartProvider cartProvider; public CarAdapter(Context context, List<ShoppingCart> carts, final CheckBox checkBox, TextView textView){ this.mcontext=context; this.carts=carts; this.checkBox=checkBox; this.textView=textView; checkBox.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { checkAll_None(checkBox.isChecked()); showTotalPrice(); } }); cartProvider=new CartProvider(mcontext); showTotalPrice(); } @NonNull @Override public CarViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { return new CarViewHolder(LayoutInflater.from(mcontext).inflate(R.layout.layout_car_item,parent,false)); } @Override public void onBindViewHolder(@NonNull CarViewHolder holder, final int position) { final ShoppingCart cart=carts.get(position); Glide.with(mcontext) .load(cart.getImgUrl()) .into(holder.imageView); holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { cart.setIsChecked(!cart.isChecked()); notifyItemChanged(position); checkListen(); showTotalPrice(); } }); holder.textView1.setText(cart.getName()); holder.textView2.setText("$"+String.valueOf(cart.getPrice())); holder.checkBox.setChecked(cart.isChecked()); holder.numberAddSubView.setOnButtonClickListener(new NumberAddSubView.OnButtonClickListener() { @Override public void onButtonAddClick(View view, int value) { cart.setCount(value); Toast.makeText(mcontext,value+",快去看看吧!"+cart.getCount(),Toast.LENGTH_SHORT).show(); cartProvider.put(cart); showTotalPrice(); } @Override public void onButtonSubClick(View view, int value) { cart.setCount(value); cartProvider.put(cart); showTotalPrice(); } }); holder.numberAddSubView.setValue(cart.getCount()); } @Override public int getItemCount() { return carts.size(); } class CarViewHolder extends RecyclerView.ViewHolder{ private ImageView imageView; private TextView textView1; private TextView textView2; private CheckBox checkBox; private NumberAddSubView numberAddSubView; public CarViewHolder(@NonNull final View itemView) { super(itemView); imageView=itemView.findViewById(R.id.iv_pic); textView1=itemView.findViewById(R.id.text_title); textView2=(TextView)itemView.findViewById(R.id.text_price); checkBox=itemView.findViewById(R.id.checkbox); numberAddSubView=itemView.findViewById(R.id.num_control); } } public void showTotalPrice(){ float total = getTotalPrice(); textView.setText(Html.fromHtml("合计 ¥<span style='color:#eb4f38'>" + total + "</span>"), TextView.BufferType.SPANNABLE); } private float getTotalPrice(){ float sum=0; if(!isNull()) return sum; for (ShoppingCart cart: carts) { if(cart.isChecked()) sum += cart.getCount()*cart.getPrice(); } return sum; } private boolean isNull(){ return (carts !=null && carts.size()>0); } private void checkListen() { int count = 0; int checkNum = 0; if (carts != null) { count = carts.size(); for (ShoppingCart cart : carts) { if (!cart.isChecked()) { checkBox.setChecked(false); break; } else { checkNum = checkNum + 1; } } if (count == checkNum) { checkBox.setChecked(true); } } } public void checkAll_None(boolean isChecked) { if(!isNull()) return ; int i=0; for (ShoppingCart cart :carts) { cart.setIsChecked(isChecked); notifyItemChanged(i); i++; } } public void delCart() { if(!isNull()) return ; for(Iterator iterator = carts.iterator(); iterator.hasNext();) { ShoppingCart cart = (ShoppingCart) iterator.next(); if(cart.isChecked()) { int position = carts.indexOf(cart); cartProvider.delete(cart); iterator.remove(); notifyItemRemoved(position); } } } }