Android二级购物车逻辑+RecycleView嵌套实现

上一篇文章已将说了一级购物车的逻辑,在这在按步骤贴一下二级购物车代码,我是用RecycleView嵌套RecycleView完成的,没有用二级列表,无非是下面几个逻辑,具体的看注释,具有一定的参考价值,感兴趣的可以参考一下!

先去第五步完成自定义View加减器,布局用;

逻辑:

全选或全不选(复选框);

商品控制商家(复选框);

商家控制商品复选框(复选框);

商家和商品控制全选(复选框);

复选框选中状态计算价格;


第一步:封装bean对象时,记得在商家和商品的bean对象内添加boolean变量。

//商家
public static class DataBean {

       
    private boolean isGroupChecked;
    ..............................

     
    public boolean isGroupChecked() {
       return isGroupChecked;
    }

    public void setGroupChecked(boolean groupChecked) {
       isGroupChecked = groupChecked;
    }

 }

————————————————————————————————————————————————————————————
//商品
public class ChildGoods {

    private boolean isChildChecked;
    ..............................


    public boolean isChildChecked() {
        return isChildChecked;
    }

    public void setChildChecked(boolean childChecked) {
        isChildChecked = childChecked;
    }

}

第二步:(ActivityFragment页面) 

public class CartsFragment extends Fragment implements CartsView {

    @BindView(R.id.checkAll)
    CheckBox checkAll;
    @BindView(R.id.priceAll)
    TextView priceAll;
    Unbinder unbinder;
    @BindView(R.id.rlv_group)
    RecyclerView rlvGroup;
    private CartsPresenter presenter;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_carts, null, false);
        unbinder = ButterKnife.bind(this, view);
        rlvGroup.setLayoutManager(new LinearLayoutManager(getActivity()));
        //实例p
        presenter = new CartsPresenter(this);
        presenter.attachView(this);

        //关联p
        presenter.onRelated();

        return view;
    }

    @Override
    public void getViewData(GroupGoods groupGoods) {
        if (groupGoods != null) {
            final List data = groupGoods.getData();
            final GroupAdapter groupAdapter = new GroupAdapter(getActivity(), data);
            rlvGroup.setAdapter(groupAdapter);

            //商家控制全选
            groupAdapter.setGroupListener(new GroupAdapter.onGroupListener() {
                @Override
                public void onGroup(List list) {
                    double priceSum = 0.0;//累计总价
                    int num1 = 0;//累计商品数量
                    int num2 = 0;//累计商品商品选中数量
                    for (int i = 0; i < list.size(); i++) {
                        List childGoods = list.get(i).getList();
                        for (int j = 0; j < childGoods.size(); j++) {
                            num1++;
                            boolean childChecked = childGoods.get(j).isChildChecked();
                            if (childChecked) {
                                num2++;
                                priceSum += childGoods.get(j).getPrice() * childGoods.get(j).getNum();
                            }
                        }
                    }
                    //判断商品选中数量和商品数量是否相等
                    if (num1 == num2) {
                        checkAll.setChecked(true);
                    } else {
                        checkAll.setChecked(false);
                    }
                    priceAll.setText("总价:" + priceSum);
                }
            });


            /**
             * 全选全不选
             */
            checkAll.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    double priceSum = 0.0;
                    boolean isChecked = ((CheckBox) v).isChecked();
                    if (isChecked) {
                        for (int i = 0; i < data.size(); i++) {
                            data.get(i).setGroupChecked(true);
                            List list = data.get(i).getList();
                            for (int j = 0; j < list.size(); j++) {
                                list.get(j).setChildChecked(true);
                                //选中状态累加总价
                                priceSum += list.get(j).getPrice() * list.get(j).getNum();
                            }
                        }
                    } else {
                        for (int i = 0; i < data.size(); i++) {
                            data.get(i).setGroupChecked(false);
                            List list = data.get(i).getList();
                            for (int j = 0; j < list.size(); j++) {
                                list.get(j).setChildChecked(false);
                            }
                        }
                    }
                    groupAdapter.notifyDataSetChanged();
                    priceAll.setText("总价:"+priceSum);
                }
            });
        }
    }

    //销毁
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
        presenter.detachView();
    }

}

第三步:(商家适配器)

public class GroupAdapter extends RecyclerView.Adapter {

    private Context context;
    private List list;

    public GroupAdapter(Context context, List list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.group_item, null, false);
        MyViewHolder myViewHolder = new MyViewHolder(view);
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull final MyViewHolder myViewHolder, final int i) {
        myViewHolder.checkGroup.setChecked(list.get(i).isGroupChecked());
        myViewHolder.nameGroup.setText(list.get(i).getSellerName());

        //给商品设置适配器
        myViewHolder.rlvChild.setLayoutManager(new LinearLayoutManager(context));
        final List childlist = this.list.get(i).getList();
        if (childlist.size() != 0) {
            //商品适配器
            final ChildAdapter childAdapter = new ChildAdapter(context, childlist);
            myViewHolder.rlvChild.setAdapter(childAdapter);

            //商品适配器接口监听
            childAdapter.setChildListener(new ChildAdapter.onChildListener() {
                @Override
                public void onChild() {
                    //商品监听到就调用商家接口回调到主页面
                    if (groupListener != null) {
                        groupListener.onGroup(list);
                    }
                    //声明变量
                    boolean isChecked = true;
                    //遍历商品状态
                    for (int j = 0; j < childlist.size(); j++) {
                        boolean childChecked = childlist.get(j).isChildChecked();
                        if (!childChecked) {
                            isChecked = false;
                            break;
                        }
                    }
                    //通过商品状态改变商家状态
                    myViewHolder.checkGroup.setChecked(isChecked);
                    list.get(i).setGroupChecked(isChecked);
                }
            });

            //商家控制商品
            myViewHolder.checkGroup.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //先改变商家状态
                    myViewHolder.checkGroup.setChecked(myViewHolder.checkGroup.isChecked());
                    //改变商品状态-->调用商品适配器方法
                    childAdapter.setCheckChild(myViewHolder.checkGroup.isChecked());
                }
            });
        }

    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.check_group)
        CheckBox checkGroup;
        @BindView(R.id.name_group)
        TextView nameGroup;
        @BindView(R.id.rlv_child)
        RecyclerView rlvChild;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface onGroupListener {
        void onGroup(List list);
    }

    public onGroupListener groupListener;

    public void setGroupListener(onGroupListener groupListener) {
        this.groupListener = groupListener;
    }
}

第四步:(商品适配器)

public class ChildAdapter extends RecyclerView.Adapter {

    private Context context;
    private List list;

    public ChildAdapter(Context context, List list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.child_item, null, false);
        MyViewHolder myViewHolder = new MyViewHolder(view);
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull final MyViewHolder myViewHolder, final int i) {
        myViewHolder.checkChild.setChecked(list.get(i).isChildChecked());
        myViewHolder.imgChild.setImageURI(Uri.parse(list.get(i).getImages()));
        myViewHolder.priceChild.setText("¥:" + list.get(i).getPrice());
        myViewHolder.titleChild.setText(list.get(i).getTitle());
        //调用自定义view加减器内方法
        myViewHolder.myaddjian.setData(this,list,i);
        //监听加减器接口
        myViewHolder.myaddjian.setNumChangeListener(new CustomAddJian.onNumChangeListener() {
            @Override
            public void onResult() {
                if (childListener!=null){
                   childListener.onChild();
                }
            }
        });

        //商品状态改变
        myViewHolder.checkChild.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (childListener!=null){
                    //先改变自身状态
                    list.get(i).setChildChecked(isChecked);
                    childListener.onChild();
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public void setCheckChild(boolean checked) {
        //商家适配器调用的方法
        //通过该方法实现商家内部全选
        for (int i = 0; i < list.size(); i++) {
            list.get(i).setChildChecked(checked);
        }
        notifyDataSetChanged();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.img_child)
        SimpleDraweeView imgChild;
        @BindView(R.id.title_child)
        TextView titleChild;
        @BindView(R.id.price_child)
        TextView priceChild;
        @BindView(R.id.check_child)
        CheckBox checkChild;
        @BindView(R.id.myaddjian)
        CustomAddJian myaddjian;
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
    public interface onChildListener{
        void onChild();
    }
    public onChildListener childListener;

    public void setChildListener(onChildListener childListener) {
        this.childListener = childListener;
    }
}

第五步:(自定义View加减器)

public class CustomAddJian extends LinearLayout {

    private TextView add;
    private TextView jian;
    private EditText num_edit;
    private int number;
    private ChildAdapter childAdapter;
    private List list;
    private int i;

    public CustomAddJian(Context context) {
        super(context);
    }

    public CustomAddJian(final Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.custom_addjian, this);
        add = findViewById(R.id.add_custom);
        jian = findViewById(R.id.jian_custom);
        num_edit = findViewById(R.id.num_custom);

        //加号点击事件
        add.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                number++;
                num_edit.setText(number + "");
                list.get(i).setNum(number);
                numChangeListener.onResult();
                childAdapter.notifyDataSetChanged();
            }
        });
        //减号点击事件
        jian.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                
                if (number <= 1) {
                    Toast.makeText(context, "不能再减了", Toast.LENGTH_SHORT).show();
                } else {
                    number--;
                    num_edit.setText(number + "");
                    list.get(i).setNum(number);
                    numChangeListener.onResult();
                    childAdapter.notifyDataSetChanged();
                }
            }
        });

    }

    //商品适配器调用的方法
    public void setData(ChildAdapter childAdapter, List list, int i) {
        this.childAdapter = childAdapter;
        this.list = list;
        this.i = i;
        number = list.get(i).getNum();
        num_edit.setText(list.get(i).getNum() + "");
    }

    public interface onNumChangeListener {
        void onResult();
    }

    public onNumChangeListener numChangeListener;

    public void setNumChangeListener(onNumChangeListener numChangeListener) {
        this.numChangeListener = numChangeListener;
    }
}

 

你可能感兴趣的:(Android)