购物车加减

1.在子商品的adapter:

 //自定义view
        viewHolder.customCounterView.setData(this,mdata,i);
        viewHolder.customCounterView.setListener(new CustomCounterView.goodslistener() {
            @Override
            public void callback() {
                if(goodsListener!=null)
                {
                    goodsListener.callback();
                }
            }
        });

      //接口回调
    private  GoodsListener goodsListener;

    public void  setOnclick(GoodsListener listener)
    {
        this.goodsListener=listener;
    }
    public interface  GoodsListener
    {
        void  callback();
    }

2.在自定义View

package com.bw.ymy.dat20;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bw.ymy.dat20.bean.GoodsBean;

import java.util.ArrayList;
import java.util.List;

public class CustomCounterView extends RelativeLayout implements View.OnClickListener {

    private EditText num;
    private TextView add,jian;

    private List list=new ArrayList<>();
    private GoodsAdapter goodsAdapter;
    private int position;
    private int nums;
    private Context context;

    public CustomCounterView(Context context) {
        super(context);
        init(context);
    }

    public CustomCounterView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public CustomCounterView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }
    private  void init(Context context)
    {
        this.context=context;
        //获取布局
        View view=View.inflate(context,R.layout.view_add,null);
        add=view.findViewById(R.id.add);
        jian=view.findViewById(R.id.jian);
        num=view.findViewById(R.id.num);
        jian.setOnClickListener(this);
        add.setOnClickListener(this);
        addView(view);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId())
        {
            case R.id.jian:
                if(nums>1)
                {
                    nums--;
                }else
                {
                    Toast.makeText(context, "最少为1", Toast.LENGTH_SHORT).show();
                }
                list.get(position).setNum(nums);
                goodslistener.callback();
                goodsAdapter.notifyDataSetChanged();
                break;
            case R.id.add:

                nums++;
                num.setText(nums+"");
                list.get(position).setNum(nums);
                goodslistener.callback();
                goodsAdapter.notifyDataSetChanged();
                break;


        }

    }
    public void setData(GoodsAdapter goodsAdapter, List mdata, int i) {

        this.list=mdata;
        this.goodsAdapter=goodsAdapter;
        position=i;
        nums=list.get(i).getNum();
        num.setText(nums+"");

    }

    private   goodslistener goodslistener;

    public  void  setListener(goodslistener listener)
    {
        this.goodslistener=listener;
    }
    public  interface  goodslistener
    {
        void  callback();
    }


}

自定义view布局




    
    
    


输入框



    

    
    


 

你可能感兴趣的:(购物车加减)