先在bean类写一个boolean 的ischecked;
然后在在适配器里面写get方法
private void xuanzhong() {
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
List<ShopBean.DataBean> data = shopAdapter.getShopBean().getData();
for (int i=0;i<data.size();i++){
ShopBean.DataBean dataBean = data.get(i);
dataBean.setChecked(isChecked);
List<ShopBean.DataBean.ListBean> list = dataBean.getList();
for (int j=0;j<list.size();j++){
ShopBean.DataBean.ListBean listBean = list.get(j);
listBean.setIschecked(isChecked);
}
}
shopAdapter.notifyDataSetChanged();
jisuan();
}
});
}
private void jisuan() {
List<ShopBean.DataBean> data = shopAdapter.getShopBean().getData();
int jia=0;
int shu=0;
for(int i=0;i<data.size();i++){
ShopBean.DataBean dataBean = data.get(i);
List<ShopBean.DataBean.ListBean> list = dataBean.getList();
for(int j=0;j<list.size();j++){
ShopBean.DataBean.ListBean listBean = list.get(j);
if(listBean.isIschecked()){
price = listBean.getPrice();
int num = listBean.getNum();
jia+=price*num;
shu+=num;
}
}
}
zongjia.setText(jia+"");
shuliang.setText(shu+"");
}
viewHolder.p_check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
List<ShopBean.DataBean.ListBean> list1 = shopBean.getData().get(i).getList();
for(int i = 0; i<list1.size(); i++){
ShopBean.DataBean.ListBean listBean = list1.get(i);
listBean.setIschecked(isChecked);
}
childAdapter.notifyDataSetChanged();
}
});