购物车的页面


```java
在这里插入代码片

public class ShopCartFragment extends Fragment {

@BindView(R.id.recyclerView_shopCart)
RecyclerView recyclerViewShopCart;
@BindView(R.id.cb_select_all)
CheckBox cbSelectAll;
@BindView(R.id.tv_total_price)
TextView tvTotalPrice;
@BindView(R.id.tv_total_count)
TextView tvTotalCount;
private Unbinder unbinder;

private List<HomeshopsBean> homeshopsBeanList;//购物车展示的数据源集合
private List<HomeshopsBean> homeshopsBeanListSelected = new ArrayList<>();//被选中的商品数据源集合
private ShopCartAdapter adapter;
private Realm realm;


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

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    recyclerViewShopCart.setLayoutManager(new LinearLayoutManager(getActivity()));
    //查询数据库购物车数据并展示
    homeshopsBeanList = realm.where(HomeshopsBean.class).findAll();
    adapter = new ShopCartAdapter(getActivity(), homeshopsBeanList);
    recyclerViewShopCart.setAdapter(adapter);

}

@Override
public void onDestroyView() {
    super.onDestroyView();
    unbinder.unbind();

}

@Override
public void onDestroy() {
    super.onDestroy();
    if (!realm.isClosed()) {

        realm.close();
    }
}

/*全选*/
@OnClick(R.id.cb_select_all)
public void onViewClicked() {
    realm.beginTransaction();//开启事务
    boolean checked = cbSelectAll.isChecked();
    for (int i = 0; i < homeshopsBeanList.size(); i++) {
        homeshopsBeanList.get(i).setSelected(checked);
    }
    //计算总价和总量
    calcTotal();
    realm.commitTransaction();//提交事务
}

/**
 * 存储总价的变量
 */
private int totalPrice;

/**
 * 存储选中商品总数量
 */
private int totalCount;

/**
 * 标识商品是否是全选的变量
 */
private boolean isSelectAll;

private void calcTotal() {
    totalPrice = 0;//重置商品总价
    totalCount = 0;//重置商品总量
    isSelectAll = true;//重置全选按钮的状态

    for (int i = 0; i < homeshopsBeanList.size(); i++) {
        if (homeshopsBeanList.get(i).isSelected()) {
            // 累计价格
            totalPrice += homeshopsBeanList.get(i).getNowprice();
            // 累计数量
            totalCount += 1;
        } else {
            isSelectAll = false;//如果有一件没选中全选就是false
        }
    }
    // 刷新适配器
    adapter.notifyDataSetChanged();
    // 设置总价
    tvTotalPrice.setText("总价:¥" + totalPrice);
    // 设置总数量
    tvTotalCount.setText("共计" + totalCount + "件商品");
    // 修改页面
    cbSelectAll.setChecked(isSelectAll);
}

@Subscribe(threadMode = ThreadMode.MAIN)
public void getEvent(MessageBean messageBean) {
    //收到计算总价和总量的事件
    if ("计算总量和总价".equals(messageBean.getMessage())) {
        calcTotal();
    }
}

@Override
public void onStart() {
    super.onStart();
    EventBus.getDefault().register(this);
}

@Override
public void onStop() {
    super.onStop();
    EventBus.getDefault().unregister(this);
}

/*立即结算购买*/
@OnClick(R.id.buy)
public void buy() {
    homeshopsBeanListSelected.clear();//清空一下上次的数据
    for (int i = 0; i < homeshopsBeanList.size(); i++) {
        if (homeshopsBeanList.get(i).isSelected()) {//用户选中的商品
            homeshopsBeanListSelected.add(homeshopsBeanList.get(i));
        }
    }
    EventBus.getDefault().postSticky(homeshopsBeanListSelected);
    //跳转到确认订单页面
    Intent intent = new Intent(getActivity(), ConfirmOrderActivity.class);
    startActivity(intent);
}

}```

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值