你的位置:首页 > 操作系统

[操作系统]硅谷商城5


1_商品总价格计算

①在GovaffairPager类中设置

adapter = new GovaffairPagerAdapter(context,datas,checkbox_all,tv_total_price);

②GovaffairPagerAdapter  

/** * 显示总商品价格 */private void showTotalPrice(){  tv_total_price.setText("合计¥"+getTotalPrice());}/** * 计算购物车里面所选商品总架构 * @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);      //判断是否选中的商品      if(cart.isChecked()){        num = num + cart.getPrice()*cart.getCount();      }    }  }  return num;}//在构造方法中调用 public GovaffairPagerAdapter(final Context context, final List<ShopingCart> datas) {    this.context = context;    this.datas = datas;    cartProvider = new CartProvider(context);    showTotalPrice();}

 

2_增加商品或者减少商品的时候计算总价格

@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_设置点击某一条item

1_先定义接口和调用

//设置点击某个item的监听public interface OnItemClickListener{  void onItemClick(View view,int position);}private OnItemClickListener onItemClickListener;/** * 设置某条的监听 * @param onItemClickListener */public void setOnItemClickListener(OnItemClickListener onItemClickListener) {  this.onItemClickListener = onItemClickListener;}

2_调用接口

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_在构造方法中设置监听

 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.设置点击item的状态        ShoppingCart ShoppingCart = datas.get(position);        shopingCart.setIsChecked(!shopingCart.isChecked());        notifyItemChanged(position);        //2.设置全选和非全选        checkListener();        //3.显示总价格        showTotalPrice();        // Toast.makeText(context,"选中了"+position,Toast.LENGTH_SHORT).show();      }    });  } /** * 全选的监听 */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(!cart.isChecked()){        checkbox_all.setChecked(false);      }else{        num += 1;      }    }    if(datas.size()==num){      checkbox_all.setChecked(true);    }  }}

4_全选和反选

  //把CheckBox 和 TextView 传入到适配器中  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) {        //全选和反选        checkAll_none(checkbox_all.isChecked());        showTotalPrice();      }    });  } /** * 全选和反选 * @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);      notifyItemChanged(i);    }  }}//在GovaffairPager类中设置adapter = new GovaffairPagerAdapter(context,datas,checkbox_all,tv_total_price);