二级商品适配器
myViewHolder.checkBox.setChecked(list.get(i).isChildCheck());
myViewHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (childLisenter != null) {
// 先改变自身状态
list.get(i).setChildCheck(isChecked);
childLisenter.onChild();
}
}
});
二级商家调用适配器
public void setCheckChild(boolean checked) {
// 商家适配器的调用方法
for (int i = 0; i < list.size(); i++) {
list.get(i).setChildCheck(checked);
}
// 刷新
notifyDataSetChanged();
}
二级商品接口回调
public interface onChildLisenter {
void onChild();
}
public onChildLisenter childLisenter;
public void setChildLisenter(onChildLisenter onChildLisenter) {
this.childLisenter = onChildLisenter;
}
一级商家适配器
@Override
public void onBindViewHolder(@NonNull final MyViewHolder myViewHolder, final int i) {
myViewHolder.title.setText(data.get(i).getSellerName());
myViewHolder.rlv2.setLayoutManager(new LinearLayoutManager(context));
final Listlist = data.get(i).getList();
final ShopChildAdapter shopChildAdapter = new ShopChildAdapter(context, list);
myViewHolder.rlv2.setAdapter(shopChildAdapter);
// 选
myViewHolder.checkBox.setChecked(data.get(i).isParentCheck());
// 二级商品适配器接口监听
shopChildAdapter.setChildLisenter(new ShopChildAdapter.onChildLisenter() {
@Override
public void onChild() {
// 商品收到监听回主页面
if (parentLisenter != null) {
parentLisenter.onParent(data);
}
boolean isChecked = true;
// 遍历商品状态
for (int j = 0; j < list.size(); j++) {
boolean childCheck = list.get(j).isChildCheck();
if (!childCheck) {
isChecked = false;
break;
}
}
// 通过商品状态改变商家状态
myViewHolder.checkBox.setChecked(isChecked);
data.get(i).setParentCheck(isChecked);
}
});
// 控制商品
myViewHolder.checkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 先改变商家状态
myViewHolder.checkBox.setChecked(myViewHolder.checkBox.isChecked());
// 改变商品状态,调用商品适配器
shopChildAdapter.setCheckChild(myViewHolder.checkBox.isChecked());
}
});
}
一级商家接口回调
// 接口回调
public interface onParentLisenter {
void onParent(Listdata);
}
public onParentLisenter parentLisenter;
public void setParentLisenter(onParentLisenter onParentLisenter) {
this.parentLisenter = onParentLisenter;
}
Activity或者Fragment
@Override
public void getDataView(JsonBean jsonBean) {
final Listdata = jsonBean.getData();
// Toast.makeText(getActivity(), "" + data.toString(), Toast.LENGTH_SHORT).show();
final ShopParentAdapter shopParentAdapter = new ShopParentAdapter(getActivity(), data);
rlv.setAdapter(shopParentAdapter);
// 商家控制全选全不选
shopParentAdapter.setParentLisenter(new ShopParentAdapter.onParentLisenter() {
@Override
public void onParent(Listdata) {
double priceSum = 0.0;
int num1 = 0;
int num2 = 0;
for (int i = 0; i < data.size(); i++) {
Listlist = data.get(i).getList();
for (int j = 0; j < list.size(); j++) {
num1++;
boolean childCheck = list.get(j).isChildCheck();
if (childCheck) {
num2++;
priceSum += list.get(j).getPrice() * list.get(j).getNum();
}
}
}
// 判断商品中数量和商品数量是否相等
if (num1 == num2) {
shopcheckbox.setChecked(true);
} else {
shopcheckbox.setChecked(false);
}
Callnum.setText("总价" + priceSum);
}
});
/*
全选/反选
*/
shopcheckbox.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).setParentCheck(true);
Listlist = data.get(i).getList();
for (int j = 0; j < list.size(); j++) {
list.get(j).setChildCheck(true);
//选中状态累计加总价
priceSum += list.get(j).getPrice() * list.get(j).getNum();
}
}
} else {
for (int i = 0; i < data.size(); i++) {
data.get(i).setParentCheck(false);
Listlist = data.get(i).getList();
for (int j = 0; j < list.size(); j++) {
list.get(j).setChildCheck(false);
}
}
}
shopParentAdapter.notifyDataSetChanged();
Callnum.setText("总价" + priceSum);
}
});
}
https://github.com/mapengtao6/hh