ExpandableListAdapter适配器

Context context;
List groupList;
List> childrenList;
int price;
int count;
public MyAdapter(Context context, List data, List> childrenList) {
    this.context = context;
    this.groupList = data;
    this.childrenList = childrenList;
}

@Override
public int getGroupCount() {
    return groupList.size();
}

@Override
public int getChildrenCount(int groupPosition) {
    return childrenList.get(groupPosition).size();
}

@Override
public Object getGroup(int groupPosition) {
    return groupList.get(groupPosition);
}

@Override
public Object getChild(int groupPosition, int childPosition) {
    return childrenList.get(groupPosition).get(childPosition);
}

@Override
public long getGroupId(int groupPosition) {
    return groupPosition;
}

@Override
public long getChildId(int groupPosition, int childPosition) {
    return childPosition;
}

@Override
public boolean hasStableIds() {
    return false;
}

@Override
public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
    View view = null;
    final GroupViewHolder holder;
    if (convertView == null) {
        holder = new GroupViewHolder();
        view = View.inflate(context, R.layout.group_item_layout, null);
        holder.check_group = view.findViewById(R.id.check_group);
        holder.text_group = view.findViewById(R.id.text_group);
        view.setTag(holder);
    } else {
        view = convertView;
        holder = (GroupViewHolder) view.getTag();
    }
    holder.text_group.setText(groupList.get(groupPosition).getSellerName());
    holder.check_group.setChecked(groupList.get(groupPosition).isGroupChecked());


    holder.check_group.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if(holder.check_group.isChecked()){
                groupList.get(groupPosition).setGroupChecked(true);
                AllChildrenChecked(groupPosition);
                boolean allGroupChecked = isAllGroupChecked();
                if(allGroupChecked){
                    EventBus.getDefault().post(new MessageEvent(true));
                }
                AllPrice();
                notifyDataSetChanged();
            }else{
                groupList.get(groupPosition).setGroupChecked(false);
                AllNoChildrenChecked(groupPosition);
                AllChildrenChecked(groupPosition);
                boolean allGroupChecked = isAllGroupChecked();
                if(!allGroupChecked){
                    EventBus.getDefault().post(new MessageEvent(false));
                    for (int i = 0; i < childrenList.get(groupPosition).size(); i++) {
                        childrenList.get(groupPosition).get(i).setChildrenChecked(groupList.get(groupPosition).isGroupChecked());
                    }
                }
                AllPrice();
            }
            notifyDataSetChanged();
        }
    });
    return view;
}

@Override
public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
    View view = null;
    final ChildViewHolder holder;
    if (convertView == null) {
        holder = new ChildViewHolder();
        view = View.inflate(context, R.layout.child_item_layout, null);
        holder.check_child = view.findViewById(R.id.check_child);
        holder.image_good = view.findViewById(R.id.image_good);
        holder.text_price = view.findViewById(R.id.text_price);
        holder.text_title = view.findViewById(R.id.text_title);
        holder.text_num = view.findViewById(R.id.text_num);
        holder.text_add = view.findViewById(R.id.text_add);
        holder.text_jian = view.findViewById(R.id.text_jian);
        view.setTag(holder);
    } else {
        view = convertView;
        holder = (ChildViewHolder) view.getTag();
    }
    final CartBean.DataBean.ListBean listBean = childrenList.get(groupPosition).get(childPosition);
    String images = listBean.getImages();
    Log.e("TAG",images);
    Glide.with(context).load(images.split("\\|")[0]).into(holder.image_good);
    holder.check_child.setChecked(listBean.isChildrenChecked());
    holder.text_title.setText(listBean.getTitle());
    holder.text_price.setText(listBean.getBargainPrice()+"");
    holder.text_num.setText(listBean.getNum()+"");
    holder.text_add.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int num = childrenList.get(groupPosition).get(childPosition).getNum();
            listBean.setNum(num+1);
            AllPrice();
        }
    });
    holder.text_jian.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int num = childrenList.get(groupPosition).get(childPosition).getNum();
            listBean.setNum(num-1);
            AllPrice();
        }
    });
    holder.check_child.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            CartBean.DataBean.ListBean listBean1 = childrenList.get(groupPosition).get(childPosition);
            if( holder.check_child.isChecked()){
                listBean1.setChildrenChecked(true);

                for (int i = 0; i < childrenList.get(groupPosition).size(); i++) {
                    if (childrenList.get(groupPosition).get(i).isChildrenChecked()) {
                        groupList.get(groupPosition).setGroupChecked(true);
                        boolean allGroupChecked = isAllGroupChecked();
                        if(allGroupChecked){
                            EventBus.getDefault().post(new MessageEvent(true));
                        }else{
                            EventBus.getDefault().post(new MessageEvent(false));
                        }
                    }
                }
                AllPrice();
                notifyDataSetChanged();
            }else{
                listBean1.setChildrenChecked(false);
                for (int i = 0; i < childrenList.get(groupPosition).size(); i++) {
                    if (!childrenList.get(groupPosition).get(i).isChildrenChecked()) {
                        groupList.get(groupPosition).setGroupChecked(false);
                        EventBus.getDefault().post(new MessageEvent(false));
                    }
                }
                AllPrice();
            }
            notifyDataSetChanged();

        }
    });
    return view;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
    return true;
}

class GroupViewHolder {
    CheckBox check_group;
    TextView text_group;
}

class ChildViewHolder {
    CheckBox check_child;
    TextView text_title;
    TextView text_price;
    ImageView image_good;
    TextView text_num;
    TextView text_jian;
    TextView text_add;
}

public boolean isAllGroupChecked(){
    for (int i = 0; i < groupList.size(); i++) {
        if(!groupList.get(i).isGroupChecked()){
            return false;
        }
    }
    return true;
}

public void AllChildrenChecked(int groupPosition){
    List listBeen = childrenList.get(groupPosition);
    for (int i = 0; i < listBeen.size(); i++) {
        CartBean.DataBean.ListBean listBean = listBeen.get(i);
        listBean.setChildrenChecked(true);

    }

}
public void AllNoChildrenChecked(int groupPosition){
    List listBeen = childrenList.get(groupPosition);
    for (int i = 0; i < listBeen.size(); i++) {
        CartBean.DataBean.ListBean listBean = listBeen.get(i);
        listBean.setChildrenChecked(false);

    }
}

public void Allxuan(boolean flag){
    for (int i = 0; i < groupList.size(); i++) {
        groupList.get(i).setGroupChecked(flag);
        for (int j = 0; j < childrenList.get(i).size(); j++) {
            childrenList.get(i).get(j).setChildrenChecked(flag);
        }
    }
}


public void AllPrice() {
    price = 0;
    count = 0;
    for (int i = 0; i < groupList.size(); i++) {
        int size = childrenList.get(i).size();
        for (int j = 0; j < size; j++) {
            CartBean.DataBean.ListBean listBean = childrenList.get(i).get(j);
            if (listBean.isChildrenChecked()) {
                count += listBean.getNum();
                price += listBean.getBargainPrice() * listBean.getNum();
            }
        }
    }
    notifyDataSetChanged();
    EventBus.getDefault().post(new MessageEvent(isAllGroupChecked(), price, count));
}

你可能感兴趣的:(日常练习)