针对于RecyclerView嵌套RecyclerView的购物车功能实现(加减自定义视图,选中改变思想)

自定义加减视图

public class AddDelView extends LinearLayout {
    private OnNumChangedListener onNumChangedListener;
    private View rootView;
    private ImageView iv_add;
    private ImageView iv_del;
    private TextView tv_num;

    public AddDelView(Context context) {
        this(context,null);
    }

    public AddDelView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,-1);
    }

    public AddDelView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
        initListener();
    }

    private void initView(Context context) {
        rootView = LayoutInflater.from(context).inflate(R.layout.add_del_butn, this);
        iv_add =rootView.findViewById(R.id.iv_add);
        iv_del = rootView.findViewById(R.id.iv_del);
        tv_num = rootView.findViewById(R.id.tv_num_text);
        tv_num.setText("1");
    }

    private void initListener() {
        iv_add.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                add();
            }
        });
        iv_del.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                sub();
            }
        });
    }
    private void add() {
        String cu = tv_num.getText().toString();
        int parseInt = Integer.parseInt(cu);
        parseInt++;
        setCurentCount(parseInt);
        getTotal();
    }

    private void sub() {
        String cu = tv_num.getText().toString();
        int parseInt = Integer.parseInt(cu);
        if (parseInt > 1) {
            parseInt--;
            setCurentCount(parseInt);
        } else {
            Toast.makeText(getContext(),"不能再少了亲",Toast.LENGTH_SHORT).show();
        }
        getTotal();
    }

    public int getCurentCount() {
        return Integer.parseInt(tv_num.getText().toString());
    }



    public void setOnNumChangedListener(OnNumChangedListener onNumChangedListener) {
        this.onNumChangedListener = onNumChangedListener;
    }

    public void setCurentCount(int num) {
        tv_num.setText(num + "");
        if (onNumChangedListener != null) {
            onNumChangedListener.onNumChanged(this, num);
        }
    }

    public interface OnNumChangedListener {
        void onNumChanged(View view, int curNum);
    }
}

自定义加减布局xml



    


    


    

自定义加减的调用

  private AddDelView.OnNumChangedListener onNumChangedListener;

    public void setOnNumChangedListener(AddDelView.OnNumChangedListener onNumChangedListener) {
        this.onNumChangedListener = onNumChangedListener;
    }


 myViewHolder.addDelView.setOnNumChangedListener(new AddDelView.OnNumChangedListener() {
            @Override
            public void onNumChanged(View view, int curNum) {
                listData.get(i).setNum(curNum);
                if (onNumChangedListener != null) {
                    onNumChangedListener.onNumChanged(view, curNum);
                }
            }
        });

提供为加减的计算的方法

public static void getTotal() {
        int num = 0 ;
        double total = 0;
        for (int i = 0; i < data.size(); i++) {
            List listBeans = data.get(i).getList();
            for (int j = 0; j < listBeans.size(); j++) {
                GoodsDataBean.DataBean.ListBean listchild = listBeans.get(j);
                if (listchild.getSelected2() == 1) {
                    double price = listchild.getPrice();
                    total += price * listchild.getNum();
                    num +=listchild.getNum();
                }
            }
        }
        mTvPrice.setText(total+"刚刚添加的");
        mTvNum.setText(num+"");
    }

通过选中方法来实现加减的操作

  • 首先改变全选的计算(抽出checked方法)
b为第一层的选中,p为子信息的选中
private void checked(boolean b, int p) {
        List list = goodsHomeAdapter.getList();
        for (int i = 0; i  listBeans = dataBean.getList();
            for (int j = 0; j < listBeans.size(); j++) {
                GoodsDataBean.DataBean.ListBean bean = listBeans.get(j);
                bean.setSelected2(p);
            }
        }
    }

改变选中状态以此来实现计算的状态

mCheckbox2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                   checked(true,1);
                    getTotal();
                }else{
                    checked(false, 0);
                    mTvNum.setText("结算(" + 0 + ")");
                    mTvPrice.setText(0 + "");
                }
                goodsHomeAdapter.notifyDataSetChanged();
            }
        });

针对对于第一层的选中状态监听

  myViewHodler.checkBox.setChecked(dataBean.isSelected());
        myViewHodler.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    List listData = myViewHodler.goodsChildAdapter.getListData();
                        for (int j = 0; j < listData.size(); j++) {
                            GoodsDataBean.DataBean.ListBean bean = listData.get(j);
                            bean.setSelected2(1);
                        }
                    getTotal();
                }else{
                    List listData = myViewHodler.goodsChildAdapter.getListData();
                    for (int j = 0; j < listData.size(); j++) {
                        GoodsDataBean.DataBean.ListBean bean = listData.get(j);
                        bean.setSelected2(0);
                    }
                    getTotal();
                }
                myViewHodler.goodsChildAdapter.notifyDataSetChanged();
            }
        });

设置子类视图复选框的选中状态

   checkBox.setChecked(listData.get(position).getSelected2()==1);

设置改变子类视图的复选框监听状态

myViewHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    listData.get(i).setSelected2(1);
                    getTotal();
                }else {
                    listData.get(i).setSelected2(0);
                    getTotal();
                }
            }
        });

至此关于RecyclerView嵌套RecyclerView的购物车功能实现,便简易设置完成了

你可能感兴趣的:(安卓RecyclerView,购物车,加减视图自定义,全选)