Silicon Valley Mall 5 -- shopping cart product selection and total price calculation, 5 -- shopping cart
1_total commodity price calculation
① Set in the GovaffairPager class
adapter = new GovaffairPagerAdapter(context,datas,checkbox_all,tv_total_price);
② GovaffairPagerAdapter
/*** Display the total commodity price */private void showTotalPrice () {TV _total_price.setText ("total $" + getTotalPrice ());} /*** calculate the total architecture of the selected item in the shopping cart * @ return */private double getTotalPrice () {double num = 0; if (datas! = Null & datas. size ()> 0) {for (int I = 0; I <datas. size (); I ++) {ShoppingCart cart = datas. get (I); // determines whether the selected item if (cart. isChecked () {num = num + cart. getPrice () * cart. getCount () ;}} return num ;}// call public GovaffairPagerAdapter (final Context context, final List <ShopingCart> datas) {this In the constructor. context = context; this. datas = datas; cartProvider = new CartProvider (context); showTotalPrice ();}
2 _ calculate the total price when adding or reducing products
@Overridepublic void onBindViewHolder(ViewHolder holder, int position) { final ShoppingCart cart = datas.get(position); ...... holder.numberAddSubView.setOnButtonClickListenter(new NumberAddSubView.OnButtonClickListenter() { @Override public void onButtonAddClick(View view, int value) { cart.setCount(value); cartProvider.update(cart); showTotalPrice(); } @Override public void onButtonSubClick(View view, int value) { cart.setCount(value); cartProvider.update(cart); showTotalPrice(); } });}
3 _ set to click an item
1 _ define interfaces and calls first
// Set the listener for clicking an item public interface OnItemClickListener {void onItemClick (View view, int position);} private OnItemClickListener onItemClickListener; /*** set a listener * @ param onItemClickListener */public void setOnItemClickListener (OnItemClickListener onItemClickListener) {this. onItemClickListener = onItemClickListener ;}
2 _ call the interface
class ViewHolder extends RecyclerView.ViewHolder { private CheckBox checkbox; ................. public ViewHolder(View itemView) { super(itemView); checkbox = (CheckBox) itemView.findViewById(R.id.checkbox); iv_icon = (ImageView) itemView.findViewById(R.id.iv_icon); ...... itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(onItemClickListener != null){ onItemClickListener.onItemClick(v,getLayoutPosition()); } } }); }}
3 _ set the listener in the constructor
Public GovaffairPagerAdapter (final Context context, final List <ShopingCart> datas) {this. context = context; this. datas = datas; cartProvider = new CartProvider (context); showTotalPrice (); setOnItemClickListener (new OnItemClickListener () {@ Override public void onItemClick (View view, int position) {// 1. set ShoppingCart = datas. get (position); shopingCart. setIsChecked (! ShopingCart. isChecked (); policyitemchanged (position); // 2. set all and all checkListener (); // 3. show total price showTotalPrice (); // Toast. makeText (context, "selected" + position, Toast. LENGTH_SHORT ). show () ;}}) ;}/ *** select all listeners */private void checkListener () {int num = 0; if (datas! = Null & datas. size ()> 0) {for (int I = 0; I <datas. size (); I ++) {ShoppingCart cart = datas. get (I); // if one of them is not selected, set all to if (! Cart. isChecked () {checkbox_all.setChecked (false) ;}else {num + = 1 ;}} if (datas. size () = num) {checkbox_all.setChecked (true );}}}
4_select all and reselect
// Pass the CheckBox and TextView into the adapter private final CheckBox checkbox_all; private final TextView TV _total_price; private CartProvider cartProvider; public GovaffairPagerAdapter (final Context context, final List <ShopingCart> datas, final CheckBox checkbox_all, TextView TV _total_price) {this. context = context; this. datas = datas; this. checkbox_all = checkbox_all; this. TV _total_price = TV _total_price ;.... ...................... Checkbox_all.setOnClickListener (new View. onClickListener () {@ Override public void onClick (View v) {// select all and invert checkAll_none (checkbox_all.isChecked (); showTotalPrice ();}});} /*** select all and reselect * @ param ischeck */private void checkAll_none (boolean ischeck) {if (datas! = Null & datas. size ()> 0) {for (int I = 0; I <datas. size (); I ++) {ShoppingCart cart = datas. get (I); cart. setIsChecked (ischeck); policyitemchanged (I) ;}}// set adapter = new GovaffairPagerAdapter (context, datas, checkbox_all, TV _total_price) IN THE GovaffairPager class );