package com.example.day20.view.adapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.example.day20.R;
import com.example.day20.model.NewBean;
public class MyAdapter extends BaseExpandableListAdapter {
private Context mContext;
private NewBean mnewBean;
private TextView prices;
private int sum=0;
public MyAdapter(Context context){
this.mContext = context;
}
public void setData(NewBean newBean) {
this.mnewBean=newBean;
}
public void setPrice(TextView priceAll) {
this.prices=priceAll;
}
@Override
public int getGroupCount() {
if(mnewBean==null){
return 0;
}
return mnewBean.getData().size();
}
@Override
public int getChildrenCount(int groupPosition) {
if(mnewBean==null){
return 0;
}
return mnewBean.getData().get(groupPosition).getSpus().size();
}
private CheckBox mcheckbox;
public void setView(final CheckBox checkBox) {
this.mcheckbox=checkBox;
mcheckbox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CheckBox checkBox1=(CheckBox)v;
boolean checked = checkBox1.isChecked();
selectAll(checked);
if(checked){
for (int i = 0; i <mnewBean.getData().get(groupPosition).getSpus().size() ; i++) {
int praise_num = mnewBean.getData().get(groupPosition).getSpus().get(i).getPraise_num();
sum+=praise_num;
}
}else{
for (int i = 0; i <mnewBean.getData().get(groupPosition).getSpus().size(); i++) {
int praise_num = mnewBean.getData().get(groupPosition).getSpus().get(i).getPraise_num();
sum-=praise_num;
}
}
}
});
}
@Override
public Object getGroup(int groupPosition) {
return null;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return null;
}
@Override
public long getGroupId(int groupPosition) {
return 0;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return 0;
}
@Override
public boolean hasStableIds() {
return true;
}
//父类
@Override
public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
parentViewHolder parentViewHolder;
if(convertView==null){
convertView = LayoutInflater.from(mContext).inflate(R.layout.item_group, null);
parentViewHolder=new parentViewHolder();
parentViewHolder.shopName=convertView.findViewById(R.id.shopName);
parentViewHolder.checkBox=convertView.findViewById(R.id.checkbox_parent);
convertView.setTag(parentViewHolder);
parentViewHolder.checkBox.setTag(groupPosition);
parentViewHolder.checkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CheckBox checkBox=(CheckBox)v;
boolean checked = checkBox.isChecked();
//int groupPosition = Integer.parseInt(checkBox.getTag().toString());
mnewBean.getData().get(groupPosition).setCheck(checked);
selectGroup(groupPosition,checked);
boolean selectAllGroup = isSelectAllGroup();
mcheckbox.setChecked(selectAllGroup);
if(checked){
for (int i = 0; i <mnewBean.getData().get(groupPosition).getSpus().size() ; i++) {
int praise_num = mnewBean.getData().get(groupPosition).getSpus().get(i).getPraise_num();
sum+=praise_num;
}
}else{
for (int i = 0; i <mnewBean.getData().get(groupPosition).getSpus().size(); i++) {
int praise_num = mnewBean.getData().get(groupPosition).getSpus().get(i).getPraise_num();
sum-=praise_num;
}
}
prices.setText("¥"+sum);
notifyDataSetChanged();
}
});
}else{
parentViewHolder =(parentViewHolder) convertView.getTag();
}
parentViewHolder.checkBox.setChecked(mnewBean.getData().get(groupPosition).isCheck());
parentViewHolder.shopName.setText(mnewBean.getData().get(groupPosition).getName());
return convertView;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
childViewHolder childViewHolder;
if(convertView==null){
convertView = LayoutInflater.from(mContext).inflate(R.layout.item_child, null);
childViewHolder=new childViewHolder();
childViewHolder.goodsName=convertView.findViewById(R.id.goodsName);
childViewHolder.checkBox=convertView.findViewById(R.id.checkbox_child);
childViewHolder.image=convertView.findViewById(R.id.child_image);
childViewHolder.price=convertView.findViewById(R.id.goodsprice);
convertView.setTag(childViewHolder);
}else{
childViewHolder =(childViewHolder) convertView.getTag();
}
childViewHolder.checkBox.setChecked(mnewBean.getData().get(groupPosition).getSpus().get(childPosition).isCheck());
childViewHolder.goodsName.setText(mnewBean.getData().get(groupPosition).getSpus().get(childPosition).getName());
childViewHolder.price.setText(mnewBean.getData().get(groupPosition).getSpus().get(childPosition).getPraise_num()+"");
Glide.with(mContext).load(mnewBean.getData().get(groupPosition).getSpus().get(childPosition).getPic_url()).into(childViewHolder.image);
childViewHolder.checkBox.setTag(groupPosition+"#"+childPosition);
childViewHolder.checkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CheckBox checkBox=(CheckBox)v;
String tag = (String) v.getTag();
int groupPosition = Integer.parseInt(tag.split("#")[0]);
int childPosition = Integer.parseInt(tag.split("#")[1]);
NewBean.DataBean dataBean = mnewBean.getData().get(groupPosition);
NewBean.DataBean.SpusBean spusBean = dataBean.getSpus().get(childPosition);
spusBean.setCheck(checkBox.isChecked());
boolean selectGroup = SelectAllGroup(groupPosition);
dataBean.setCheck(selectGroup);
boolean selectAllGroup = isSelectAllGroup();
mcheckbox.setChecked(selectAllGroup);
notifyDataSetChanged();
if(checkBox.isChecked()){
sum+=spusBean.getPraise_num();
}else{
sum-=spusBean.getPraise_num();
}
prices.setText("¥"+sum);
notifyDataSetChanged();
}
});
return convertView;
}
private void selectAll(boolean checked) {
for (int i = 0; i <mnewBean.getData().size() ; i++) {
NewBean.DataBean dataBean = mnewBean.getData().get(i);
dataBean.setCheck(checked);
for (int j = 0; j <dataBean.getSpus().size() ; j++) {
NewBean.DataBean.SpusBean spusBean = dataBean.getSpus().get(j);
spusBean.setCheck(checked);
}
}
notifyDataSetChanged();
}
private void selectGroup(int groupPosition, boolean checkBox) {
for (int i = 0; i <mnewBean.getData().get(groupPosition).getSpus().size() ; i++) {
NewBean.DataBean.SpusBean spusBean = mnewBean.getData().get(groupPosition).getSpus().get(i);
spusBean.setCheck(checkBox);
}
notifyDataSetChanged();
}
private boolean isSelectAllGroup() {
for (int i = 0; i <mnewBean.getData().size() ; i++) {
NewBean.DataBean dataBean = mnewBean.getData().get(i);
boolean check = dataBean.isCheck();
if(!check){
return false;
}
}
return true;
}
private boolean SelectAllGroup(int groupPosition) {
for (int i = 0; i <mnewBean.getData().get(groupPosition).getSpus().size() ; i++) {
NewBean.DataBean.SpusBean spusBean = mnewBean.getData().get(groupPosition).getSpus().get(i);
boolean check = spusBean.isCheck();
if(!check){
return false;
}
}
return true;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
class parentViewHolder{
TextView shopName;
CheckBox checkBox;
}
class childViewHolder{
TextView goodsName;
CheckBox checkBox;
ImageView image;
TextView price;
}
}