在购物车中,点击加 减号,改变购买的商品数量的监听器


        //给加号减号IamgeView添加监听
        // i是 每个购物项的position  下标
        //holder.ivm   减号的控件
        //holder.ivp   加号的控件
        holder.ivm.setOnClickListener(new ModifyNumListener(i, ModifyNumListener.BUTTON_MINUS));
        holder.ivp.setOnClickListener(new ModifyNumListener(i, ModifyNumListener.BUTTON_PLUS));
/** 在购物车中 点击加 减号,改变购买的商品数量的 监听器 * 设置点击加号 减号的监听器 */
    class ModifyNumListener implements View.OnClickListener {
        // 声明的常量 
        public static final int BUTTON_PLUS = 1;
        public static final int BUTTON_MINUS = 2;

        private int position;
        private int type;

        ModifyNumListener(int position, int type) {
            this.position = position;
            this.type = type;
        }

        @Override
        public void onClick(View view) {
            TextView tvNum = (TextView) listView.findViewWithTag("tvNum" + position);
            TextView tvCount = (TextView) listView.findViewWithTag("tvCount" + position);
            int number = Integer.parseInt(tvNum.getText().toString());
            switch (type) {
                case BUTTON_PLUS:
                    number++;
                    tvNum.setText(number+"");
                    tvCount.setText("x" + number);
                    break;
                case BUTTON_MINUS:
                    number = number==1?number:number-1;
                    tvNum.setText(number+"");
                    tvCount.setText("x" + number);
                    break;
            }
            presenter.modifyNum(getItem(position).getBook().getId(), number);
        }
    }

    public void setPresenter(ICartPresenter presenter){
        this.presenter = presenter;
    }

你可能感兴趣的:(监听器)