购物车Fragment

该博客介绍了如何创建一个名为FragmentThree的购物车Fragment,实现了数据绑定、RecyclerView展示商品、总价计算等功能。通过GouBuyPresenter接口进行交互,使用ButterKnife库绑定视图,并在CheckBox选择状态变化时更新商品选中状态和总价。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public class FragmentThree extends Fragment implements GouBuyIConter.GouBuyIView {

    @BindView(R.id.checkbox)
    CheckBox checkbox;
    @BindView(R.id.text_price)
    TextView textPrice;
    Unbinder unbinder;
    @BindView(R.id.recyc_big)
    RecyclerView recycBig;
    private GouBuyIConter.GouBuyIPresenter gouBuyPresenter;
    private BuyAdapter buyAdapter;
    private double zongjia;
    private List<GouBuyBean.DataBean> list;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragmentthree, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        gouBuyPresenter = new GouBuyPresenter();
        gouBuyPresenter.attachView(this);
        gouBuyPresenter.GouBuyRequest();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
        gouBuyPresenter.datachView(this);
    }

    @Override
    public void GouBuyShow(GouBuyBean gouBuyBean) {

        list = new ArrayList<>();
        list.addAll(gouBuyBean.getData());
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
        buyAdapter = new BuyAdapter(getContext(), list);
        recycBig.setLayoutManager(linearLayoutManager);
        recycBig.setAdapter(buyAdapter);
        getchecked();
        buyAdapter.setSjSelected(new BuyAdapter.SJSelect() {
            @Override
            public void sjselect() {
                boolean bs = true;
                for (GouBuyBean.DataBean dataBean : list) {
                    if (!dataBean.isSelect()) {
                        bs = false;
                    }
                }
                checkbox.setChecked(bs);
                getchecked();
            }
        });
        buyAdapter.setAddjianlistenter(new BuyAdapter.Addjianlistenter() {
            @Override
            public void addjianlistenter() {
                getchecked();
            }
        });
    }

    private void getchecked() {
        zongjia = 0.0;
        for (GouBuyBean.DataBean dataBean : list) {
            for (GouBuyBean.DataBean.ListBean listBean : dataBean.getList()) {
                if (listBean.getSelected() == 1) {
                    double zongnumbser = listBean.getNum() * listBean.getPrice();
                    zongjia = zongjia + zongnumbser;
                }
            }
        }
        textPrice.setText(zongjia + "");
    }

    @OnClick(R.id.checkbox)
    public void onViewClicked() {

        for(GouBuyBean.DataBean dataBean : list){
            dataBean.setSelect(checkbox.isChecked());
            for(GouBuyBean.DataBean.ListBean listBean : dataBean.getList()){
                if(checkbox.isChecked()){
                    listBean.setSelected(1);
                }else{
                    listBean.setSelected(0);
                }
            }
        }
        getchecked();
        buyAdapter.notifyDataSetChanged();

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值