1.获取数据来
Log.i("data",""+data.toString());
MyGroupAdapter myGroupAdapter = new MyGroupAdapter(this,data);
//在适配器写入传值的方法传入Id
myGroupAdapter.setSelectAll(checkAll);
myGroupAdapter.setView(sum);
myGroupAdapter.setDatas(data);
exlistview.setAdapter(myGroupAdapter);
二级列表的适配中
@Override
public View getGroupView(final int i, boolean b, View view, ViewGroup viewGroup) {
converview = View.inflate(context, R.layout.grouplistview, null);
TextView groupname = converview.findViewById(R.id.group_name);
CheckBox groupcheck = converview.findViewById(R.id.check_group);
groupname.setText(data.get(i).getSellerName());
groupcheck.setChecked(data.get(i).isCheck());
//父类全选点钟子类全选
groupcheck.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
CheckBox checkBox= (CheckBox) view;
//设置父类为选中
data.get(i).setCheck(checkBox.isChecked());
//执行循环子类并选中
CheckGroup(i,checkBox.isChecked());
boolean checked = mcheckbox.isChecked();
mcheckbox.setChecked(checked);
setChildPrice(i);
notifyDataSetChanged();
}
});
return converview;
}
//计算父条目全选价格
private void setChildPrice(int i) {
for (int j = 0; j <data.get(i).getList().size() ; j++) {
boolean check = data.get(i).getList().get(j).isCheck();
if (check){
String num = data.get(i).getList().get(j).getNum();
String price = data.get(i).getList().get(j).getPrice();
Double douprice = Double.valueOf(price);
Integer intnum = Integer.valueOf(num);
totalprice=douprice*intnum;
sum.setText("合计"+totalprice);
}else {
totalprice=0;
sum.setText("合计"+totalprice);
}
}
}
//点击父类选中子类全选
private void CheckGroup(int i, boolean checked) {
for (int j = 0; j <data.get(i).getList().size() ; j++) {
ShopBean.DataBean.ListBean listBean = data.get(i).getList().get(j);
listBean.setCheck(checked);
}
}
@Override
public View getChildView(final int i, final int i1, boolean b, View view, ViewGroup viewGroup) {
inflate = View.inflate(context, R.layout.childlistview, null);
CheckBox childcheck = inflate.findViewById(R.id.childcheck);
TextView childprice = inflate.findViewById(R.id.childprice);
add addview = inflate.findViewById(R.id.addview);
ImageView childimagview = inflate.findViewById(R.id.childimagview);
TextView childtext = inflate.findViewById(R.id.childtext);
childcheck.setChecked(data.get(i).getList().get(i1).isCheck());
childprice.setText(data.get(i).getList().get(i1).getPrice());
childtext.setText(data.get(i).getList().get(i1).getTitle());
final TextView text= addview.findViewById(R.id.textview);
text.setText(""+data.get(i).getList().get(i1).getNum());
//点击加号
addview.findViewById(R.id.add).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String num = data.get(i).getList().get(i1).getNum();
Integer integer = Integer.valueOf(num);
integer++;
data.get(i).getList().get(i1).setNum(""+integer);
String num1 = data.get(i).getList().get(i1).getNum();
text.setText(""+num1);
String num2 = data.get(i).getList().get(i1).getNum();
String price = data.get(i).getList().get(i1).getPrice();
Double aDouble = Double.valueOf(price);
Integer integer1 = Integer.valueOf(num2);
totalprice=aDouble*integer1;
sum.setText(""+totalprice);
notifyDataSetChanged();
}
});
//点击减
addview.findViewById(R.id.remove).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String num = data.get(i).getList().get(i1).getNum();
Integer integer = Integer.valueOf(num);
integer--;
data.get(i).getList().get(i1).setNum(""+integer);
String num1 = data.get(i).getList().get(i1).getNum();
text.setText(""+num1);
notifyDataSetChanged();
}
});
Glide.with(context).load(data.get(i).getList().get(i1).getImages().split("\\|")[0].replace("https","http")).into(childimagview);
childcheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
data.get(i).getList().get(i1).setCheck(b);
boolean groupCheckAll = isGroupCheckAll(i);
data.get(i).setCheck(groupCheckAll);
boolean checkAll = isCheckAll();
mcheckbox.setChecked(checkAll);
//点击条目计算价钱
setChildAllPrice(i,i1,b,0);
notifyDataSetChanged();
}
});
return inflate;
}
//子条目全选计算价格
private void setChildAllPrice(int i, int i1, boolean b, int i2) {
if (i2==1){
totalprice=0;
for (int j = 0; j <data.size() ; j++) {
for (int k = 0; k <data.get(j).getList().size() ; k++) {
boolean check = data.get(j).getList().get(k).isCheck();
if (check){
String num = data.get(j).getList().get(k).getNum();
String price = data.get(j).getList().get(k).getPrice();
Double douprice = Double.valueOf(price);
Integer intnum = Integer.valueOf(num);
totalprice=douprice*intnum;
sum.setText("合计"+totalprice);
} else {
totalprice=0;
sum.setText("合计"+totalprice);
}
}
}
}else {
if (b){
String num = data.get(i).getList().get(i1).getNum();
String price = data.get(i).getList().get(i1).getPrice();
Double douprice = Double.valueOf(price);
Integer intnum = Integer.valueOf(num);
totalprice=douprice*intnum;
sum.setText("合计"+totalprice);
notifyDataSetChanged();
}else {
String num = data.get(i).getList().get(i1).getNum();
String price = data.get(i).getList().get(i1).getPrice();
Double douprice = Double.valueOf(price);
Integer intnum = Integer.valueOf(num);
totalprice=douprice*intnum;
sum.setText("合计"+totalprice);
notifyDataSetChanged();
}
}
}
//判断是否全部选中
private boolean isCheckAll() {
for (int i = 0; i < data.size(); i++) {
boolean check = data.get(i).isCheck();
if(!check){
return false;
}
}
return true;
}
private boolean isGroupCheckAll(int i) {
for (int j = 0; j <data.get(i).getList().size() ; j++) {
boolean check = data.get(i).getList().get(j).isCheck();
if (!check){
return false;
}
}
return true;
}
@Override
public boolean isChildSelectable(int i, int i1) {
return false;
}
//全选反选及计算价格
public void setSelectAll(CheckBox checkAll) {
mcheckbox = checkAll;
mcheckbox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
for (int i = 0; i <data.size() ; i++) {
//循环父条目设为选中
List<ShopBean.DataBean.ListBean> list = data.get(i).getList();
data.get(i).setCheck(mcheckbox.isChecked());
for (int j = 0; j <list.size() ; j++) {
//循环自条目设为选中
list.get(j).setCheck(mcheckbox.isChecked());
}
}
//点击全选设置价格
setPrice();
notifyDataSetChanged();
}
});
}
//全选计算价格 有BUG
private void setPrice() {
for (int i = 0; i <data.size() ; i++) {
boolean check1 = data.get(i).isCheck();
if (check1){
List<ShopBean.DataBean.ListBean> list = data.get(i).getList();
for (int j = 0; j <list.size() ; j++) {
boolean check = list.get(j).isCheck();
if (check){
String num = list.get(j).getNum();
String price = list.get(j).getPrice();
int intnum=Integer.valueOf(num);
Double intprice = Double.valueOf(price);
totalprice=intnum*intprice;
sum.setText("合计"+totalprice);
}else {
totalprice=0;
sum.setText("合计"+totalprice);
}
}
}
}
}
public void setView(TextView sum) {
this.sum=sum;
}
public void setDatas(List<ShopBean.DataBean> data) {
this.data=data;
}
}
//自定义加减
public class add extends RelativeLayout {
private int numgood;
private int position;
private TextView text;
public add(Context context) {
super(context);
intit(context);
}
public add(Context context, AttributeSet attrs) {
super(context, attrs);
intit(context);
}
private void intit(Context context) {
View inflate = View.inflate(context, R.layout.addjian, null);
Button adds = inflate.findViewById(R.id.add);
Button removes = inflate.findViewById(R.id.remove);
text = inflate.findViewById(R.id.textview);
addView(inflate);
text.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
numgood = Integer.parseInt(charSequence.toString());
}
@Override
public void afterTextChanged(Editable editable) {
}
});
}
}