Maimn布局
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorAccent"
android:fitsSystemWindows="true"
app:title="@string/toolbar_name" />
<com.scwang.smartrefresh.layout.SmartRefreshLayout
android:id="@+id/srl_container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_business"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</com.scwang.smartrefresh.layout.SmartRefreshLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center_vertical">
<CheckBox
android:id="@+id/cb_all"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="全选" />
<Button
android:id="@+id/btn_price"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:gravity="center_vertical"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:text="结算" />
<TextView
android:id="@+id/tv_count"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginRight="20dp"
android:layout_toLeftOf="@+id/btn_price"
android:gravity="center_vertical"
android:text="总计:0元" />
</RelativeLayout>
外部条目布局
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<CheckBox
android:id="@+id/cb_business"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center" />
<TextView
android:id="@+id/tv_business_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center"
android:paddingLeft="5dp"
android:text="店名"
android:textSize="22sp" />
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_goods"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
内部条目布局
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
fresco:cardCornerRadius="@dimen/dp_4"
fresco:contentPaddingBottom="@dimen/dp_4">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp">
<CheckBox
android:id="@+id/cb_goods"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true" />
<ImageView
android:id="@+id/iv_goodsIcon"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_toRightOf="@id/cb_goods"
android:paddingLeft="5dp"
android:src="@mipmap/ic_launcher" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_toRightOf="@id/iv_goodsIcon"
android:orientation="vertical">
<TextView
android:id="@+id/tv_goodsTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:hint="商品名称"
android:maxEms="20"
android:paddingLeft="5dp"
android:textStyle="bold" />
<TextView
android:id="@+id/tv_goodsPrice"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/tv_goodsTitle"
android:layout_marginTop="@dimen/dp_10"
android:gravity="center"
android:hint="商品价格" />
/*自定义view加减器*/
<com.small.lx_0116.AddSubView
android:id="@+id/cv_calculatorView"
android:layout_width="match_parent"
android:layout_height="30dp"
android:gravity="center"></com.small.lx_0116.AddSubView>
</LinearLayout>
AddSubView
public class AddSubView extends LinearLayout implements View.OnClickListener {
private Button btn_add;
private Button btn_decrease;
private TextView tv_number;
public AddSubView(Context context, AttributeSet attrs) {
super(context, attrs);
//填充的条目布局
View rootView = LayoutInflater.from(context).inflate(R.layout.calculator_layout, this);
btn_add = rootView.findViewById(R.id.btn_add);
btn_decrease = rootView.findViewById(R.id.btn_decrease);
tv_number = rootView.findViewById(R.id.tv_number);
btn_add.setOnClickListener(this);
btn_decrease.setOnClickListener(this);
}
@Override
public void onClick(View v) {
String numberString = tv_number.getText().toString();
int number = Integer.parseInt(numberString);
switch (v.getId()) {
case R.id.btn_decrease:
number = number - 1;
if (number < 0) {
number = 0;
//最小数量为0
tv_number.setText(String.valueOf(number));
}
tv_number.setText(String.valueOf(number));
//接口回调回传数字
onCalCulatorLisenter.onDecrese(number);
break;
case R.id.btn_add:
number = number + 1;
tv_number.setText(String.valueOf(number));
//接口回调回传数字
onCalCulatorLisenter.onAdd(number);
break;
}
}
//接口回调
private OnCalCulatorLisenter onCalCulatorLisenter;
public interface OnCalCulatorLisenter {
//减少
public void onDecrese(int number);
//增加
public void onAdd(int number);
}
public void setOnCalCulatorLisenter(OnCalCulatorLisenter onCalCulatorLisenter) {
this.onCalCulatorLisenter = onCalCulatorLisenter;
}
}
计算总价-Activity
private void calculateTotalCount() {
//对总价进行计算
double totalCount = 0;
//外层条目
for (int i = 0; i < cartBean.size(); i++) {
//内层条目
for (int j = 0; j < cartBean.get(i).getList().size(); j++) {
//判断内层条目是否勾选
if (cartBean.get(i).getList().get(j).getGoodsChecked() == true) {
//获取商品数据*商品价格
double price = cartBean.get(i).getList().get(j).getPrice();
int defalutNumber = cartBean.get(i).getList().get(j).getDefalutNumber();
double goodsPrice = price * defalutNumber;
totalCount = totalCount + goodsPrice;
}
}
}
tvCount.setText("总价是:" + String.valueOf(totalCount));
}
全选状态
//全选逻辑的处理
for (int i = 0; i < cartBean.size(); i++) {
//首先让外层的商家条目全部选中
cartBean.get(i).setBusinessChecked(cbAll.isChecked());
//然后让里层的商品条目全部选中
for (int j = 0; j < cartBean.get(i).getList().size(); j++) {
cartBean.get(i).getList().get(j).setGoodsChecked(cbAll.isChecked());
}
}
//计算总价
calculateTotalCount();
adapter.notifyDataSetChanged();
V层方法
cbAll.setOnCheckedChangeListener(null);
cbAll.setOnClickListener(this);
//数据源
cartBean = shoppingCartBean.getData();
//设置布局管理器
manager = new LinearLayoutManager(mContext, LinearLayoutManager
.VERTICAL, false);
rvBusiness.setLayoutManager(manager);
//设置适配器
adapter = new BusinessAdapter(R.layout.recycler_business_item,
cartBean);
rvBusiness.setAdapter(adapter);
adapter.setOnBusinessItemClickLisenter(new BusinessAdapter.OnBusinessItemClickLisenter() {
@Override
public void onCallBack() {
boolean result = true;
for (int i = 0; i < cartBean.size(); i++) {
//外层选中状态
boolean businessChecked = cartBean.get(i).getBusinessChecked();
result = result & businessChecked;
for (int j = 0; j < cartBean.get(i).getList().size(); j++) {
//里层选中状态
boolean goodsChecked = cartBean.get(i).getList().get(j).getGoodsChecked();
result = result & goodsChecked;
}
}
cbAll.setChecked(result);
//计算总价
calculateTotalCount();
}
});
外层条目适配器
public class BusinessAdapter extends BaseQuickAdapter<ShoppingCartBean.DataBean, BaseViewHolder> {
/*接口回调*/
OnBusinessItemClickLisenter onBusinessItemClickLisenter;
public interface OnBusinessItemClickLisenter {
public void onCallBack();
}
public void setOnBusinessItemClickLisenter(OnBusinessItemClickLisenter
onBusinessItemClickLisenter) {
this.onBusinessItemClickLisenter = onBusinessItemClickLisenter;
}
//**********************
public BusinessAdapter(int layoutResId, @Nullable List<ShoppingCartBean.DataBean> data) {
super(layoutResId, data);
}
@Override
protected void convert(BaseViewHolder helper, final ShoppingCartBean.DataBean item) {
//设置商家名称
helper.setText(R.id.tv_business_name, item.getSellerName());
//设置商家下的子商品条目
RecyclerView rv_goods = helper.getView(R.id.rv_goods);
//避免焦点抢占
final CheckBox cb_business = helper.getView(R.id.cb_business);
cb_business.setOnCheckedChangeListener(null);
//获取商家条目是否选中
cb_business.setChecked(item.getBusinessChecked());
//子商品条目数据源
List<ShoppingCartBean.DataBean.ListBean> goodsList = item.getList();
LinearLayoutManager manager = new LinearLayoutManager(mContext, LinearLayoutManager
.VERTICAL, false);
rv_goods.setLayoutManager(manager);
//设置子条目适配器
final GoodsAdapter goodsAdapter = new GoodsAdapter(R.layout.recycler_goods_item, goodsList);
rv_goods.setAdapter(goodsAdapter);
//内部条目复选框更新外部条目联动
goodsAdapter.setOnGoodsItemClckListener(new GoodsAdapter.OnGoodsItemClckListener() {
@Override
public void onCallBack() {
//遍历获取所有子条目,只要有一个未勾选,商品类别也应该是未勾选
boolean result = true;
for (int i = 0; i < item.getList().size(); i++) {
result = result & item.getList().get(i).getGoodsChecked();
}
cb_business.setChecked(result);
//刷新适配器
goodsAdapter.notifyDataSetChanged();
//最终状态回传
onBusinessItemClickLisenter.onCallBack();
}
});
//外部条目复选框更新内部条目联动
cb_business.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//获取商品类别勾选状态
//外层商品类别条目获取的关键:cb_business.isChecked()
for (int i = 0; i < item.getList().size(); i++) {
item.getList().get(i).setGoodsChecked(cb_business.isChecked());
}
item.setBusinessChecked(cb_business.isChecked());
notifyDataSetChanged();
//把最后的状态进行回传
onBusinessItemClickLisenter.onCallBack();
}
});
}
}
内层条目适配器
public class GoodsAdapter extends BaseQuickAdapter<ShoppingCartBean.DataBean.ListBean,
BaseViewHolder> {
//接口回调
private OnGoodsItemClckListener onGoodsItemClckListener;
public interface OnGoodsItemClckListener {
void onCallBack();
}
public void setOnGoodsItemClckListener(OnGoodsItemClckListener onGoodsItemClckListener) {
this.onGoodsItemClckListener = onGoodsItemClckListener;
}
//*******************
public GoodsAdapter(int layoutResId, @Nullable List<ShoppingCartBean.DataBean.ListBean> data) {
super(layoutResId, data);
}
@Override
protected void convert(BaseViewHolder helper, final ShoppingCartBean.DataBean.ListBean item) {
//条目数据设置
helper.setText(R.id.tv_goodsPrice, "¥:" + item.getPrice());
helper.setText(R.id.tv_goodsTitle, item.getTitle());
//图片
ImageView iv_goodsIcon = helper.getView(R.id.iv_goodsIcon);
String imagesString = item.getImages();
//接口截取
String[] imagesStr = imagesString.split("\\|");
Glide.with(mContext).load(imagesStr[0]).into(iv_goodsIcon);
//避免焦点抢占
CheckBox cb_goods = helper.getView(R.id.cb_goods);
cb_goods.setOnCheckedChangeListener(null);
cb_goods.setChecked(item.getGoodsChecked());
//接口方式传状态给外层
cb_goods.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//更新bean对象
item.setGoodsChecked(isChecked);
onGoodsItemClckListener.onCallBack();
}
});
//加减器
AddSubView calculatorView = helper.getView(R.id.cv_calculatorView);
calculatorView.setOnCalCulatorLisenter(new AddSubView.OnCalCulatorLisenter() {
@Override
public void onDecrese(int number) {
//对新增的字段进行改动
item.setDefalutNumber(number);
onGoodsItemClckListener.onCallBack();
}
@Override
public void onAdd(int number) {
//对新增的字段进行改动
item.setDefalutNumber(number);
onGoodsItemClckListener.onCallBack();
}
});
}
}
Bean层特殊处理
增加外层 内层选中状态,定义数量值