实现购物车商品数量+1、-1按钮的效果

需求:楼主最近在做一个商城类的APP,购物页面和购物车中都要实现一个+1、-1按钮的功能,用于动态来指定商品的数量


废话不多说,开始撸码:

1.自定义一个AddSubView继承于线性布局,因为布局文件中要使用就实现两个参数的构造方法

package com.atguigu.add_sub_view;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * Created by xpf on 2016/11/22 :)
 * Wechat:18091383534
 * Function:自定义购物车的增加删除按钮
 */

public class addSubView extends LinearLayout implements View.OnClickListener {

    private Context mContext;
    private ImageView iv_sub;
    private ImageView iv_add;
    private TextView tv_number;
    private int currentValue = 1; // 默认为1
    private int minValue = 1;
    private int maxValue = 10; // 实际情况为最大库存

    public addSubView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;

        // 把布局实例化并且把当前的view加入到当前视图中
        // 把布局文件实例化,并且加载到AddSubView类中
        View.inflate(context, R.layout.add_sub_view, this);
        iv_sub = (ImageView) findViewById(R.id.iv_sub);
        iv_add = (ImageView) findViewById(R.id.iv_add);
        tv_number = (TextView) findViewById(R.id.tv_number);

        // 注意此处不能写成tv_number.setText(getCurrentValue()),但可以这样写tv_number.setText(getCurrentValue() + "");
        setCurrentValue(getCurrentValue());

        iv_sub.setOnClickListener(this);
        iv_add.setOnClickListener(this);
    }

    public int getCurrentValue() {

        String strValue = tv_number.getText().toString();
        if (!TextUtils.isEmpty(strValue)) {
            currentValue = Integer.parseInt(strValue);
        }
        return currentValue;
    }

    public void setCurrentValue(int currentValue) {
        this.currentValue = currentValue;
        tv_number.setText(currentValue + "");
    }

    public int getMinValue() {
        return minValue;
    }

    public void setMinValue(int minValue) {
        this.minValue = minValue;
    }

    public int getMaxValue() {
        return maxValue;
    }

    public void setMaxValue(int maxValue) {
        this.maxValue = maxValue;
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_sub:
                subNumber();
                break;
            case R.id.iv_add:
                addNumber();
                break;
        }
//        Toast.makeText(mContext, "当前的值为:" + currentValue, Toast.LENGTH_SHORT).show();
    }

    private void subNumber() {
        if (currentValue > minValue) {
            currentValue--;
        }
        setCurrentValue(currentValue);
        if (onAddSubClickListener != null) {
            onAddSubClickListener.onNumberChange(currentValue);
        }
    }

    private void addNumber() {
        if (currentValue < maxValue) {
            currentValue++;
        }
        setCurrentValue(currentValue);
        if (onAddSubClickListener != null) {
            onAddSubClickListener.onNumberChange(currentValue);
        }
    }


    private OnAddSubClickListener onAddSubClickListener;

    public void setOnAddSubClickListener(OnAddSubClickListener onAddSubClickListener) {
        this.onAddSubClickListener = onAddSubClickListener;
    }

    public interface OnAddSubClickListener {
        void onNumberChange(int value);
    }
}

2.定义一个加1和减1的接口回调

主要实现就是上面的代码,布局很简单我就不贴了,最后附上demo的下载地址:下载demo

需要的去下载。

效果展示:



由于本人的水平有限,难免会出现一些问题,如果有任何问题或者更好的想法都可以一起分享学习,欢迎打扰!吐舌头



  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值