```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);
}
}```