购物车的二级列表适配器

package com.lbp.yuekao.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.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;

import com.lbp.yuekao.R;
import com.lbp.yuekao.bean.GoosBean;
import com.lbp.yuekao.bean.MessageEvent;
import com.lbp.yuekao.bean.PriceAndCountEvent;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

/**
 * Created by Administrator on 2018/1/17 0017.
 */

public class MyExAdapter extends BaseExpandableListAdapter {
    private Context context;
    private List<GoosBean.DataBean> groupList;
    private List<List<GoosBean.DataBean.DatasBean>> childList;
    private final LayoutInflater inflater;

    public MyExAdapter(Context context, List<GoosBean.DataBean> groupList, List<List<GoosBean.DataBean.DatasBean>> childList) {
        this.context = context;
        this.groupList = groupList;
        this.childList = childList;
        inflater = LayoutInflater.from(context);
    }
    @Override
    public int getGroupCount() {
        return groupList.size();
    }

    @Override
    public int getChildrenCount(int i) {
        return childList.get(i).size();
    }

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

    @Override
    public Object getChild(int i, int i1) {
        return childList.get(i).get(i1);
    }

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

    @Override
    public long getChildId(int i, int i1) {
        return i1;
    }

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

    @Override
    public View getGroupView(final int i, boolean b, View view, ViewGroup viewGroup) {
        View view1;
        final GroupViewHolder groupViewHolder;
        if(view==null){
            groupViewHolder = new GroupViewHolder();
            view1 = inflater.inflate(R.layout.item_parent_market, null);
            groupViewHolder.cbGroup = view1.findViewById(R.id.cb_parent);
            groupViewHolder.tv_number = view1.findViewById(R.id.tv_number);
            view1.setTag(groupViewHolder);
        }
        else{
            view1 = view;
            groupViewHolder = (GroupViewHolder) view1.getTag();
        }
        final GoosBean.DataBean dataBean = groupList.get(i);
        groupViewHolder.cbGroup.setChecked(dataBean.isChecked());
        groupViewHolder.tv_number.setText(dataBean.getTitle());
        groupViewHolder.cbGroup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dataBean.setChecked(groupViewHolder.cbGroup.isChecked());
                changeChildCbState(i,groupViewHolder.cbGroup.isChecked());
                EventBus.getDefault().post(compute());
                changeAllCbState(isAllGroupCbSelected());
                notifyDataSetChanged();
            }
        });
        return view1;
    }



    @Override
    public View getChildView(final int i, final int i1, boolean b, View view, ViewGroup viewGroup) {
        View view1;
        final ChildViewHolder holder ;
        if(view==null){
            holder = new ChildViewHolder();
            view1 = inflater.inflate(R.layout.item_child_market, null);
            holder.cbChild = view1.findViewById(R.id.cb_child);
            holder.tv_tel = view1.findViewById(R.id.tv_tel);
            holder.tv_content = view1.findViewById(R.id.tv_content);
            holder.tv_time = view1.findViewById(R.id.tv_time);
            holder.tv_price = view1.findViewById(R.id.tv_pri);
            holder.tv_del = view1.findViewById(R.id.tv_del);
            holder.iv_add = view1.findViewById(R.id.iv_add);
            holder.iv_del = view1.findViewById(R.id.iv_del);
            holder.tv_num = view1.findViewById(R.id.tv_num);
            view1.setTag(holder);
        }else{
            view1=view;
            holder = (ChildViewHolder) view1.getTag();
        }
        final GoosBean.DataBean.DatasBean datasBean = childList.get(i).get(i1);
        holder.cbChild.setChecked(datasBean.isChecked());
        holder.tv_tel.setText(datasBean.getType_name());
        holder.tv_content.setText(datasBean.getMsg());
        holder.tv_time.setText(datasBean.getAdd_time());
        holder.tv_price.setText(datasBean.getPrice() + "");
        holder.tv_num.setText(datasBean.getNum() + "");

        holder.cbChild.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                datasBean.setChecked(holder.cbChild.isChecked());
                PriceAndCountEvent priceAndCountEvent = compute();
                EventBus.getDefault().post(priceAndCountEvent);
                if (holder.cbChild.isChecked()){
                    if(isAllChildCbSelected(i)){
                        changGroupCbState(i,true);
                        changeAllCbState(isAllGroupCbSelected());
                    }
                }else{
                    changGroupCbState(i,false);
                    changeAllCbState(isAllGroupCbSelected());
                }
                notifyDataSetChanged();
            }
        });

        holder.iv_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int num = datasBean.getNum();
                holder.tv_num.setText(++num + "");
                datasBean.setNum(num);
                if (holder.cbChild.isChecked()) {
                    PriceAndCountEvent priceAndCountEvent = compute();
                    EventBus.getDefault().post(priceAndCountEvent);
                }
            }
        });
        holder.iv_del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int num = datasBean.getNum();
                if (num == 1) {
                    return;
                }
                holder.tv_num.setText(--num + "");
                datasBean.setNum(num);
                if (holder.cbChild.isChecked()) {
                    PriceAndCountEvent priceAndCountEvent = compute();
                    EventBus.getDefault().post(priceAndCountEvent);
                }
            }
        });
        holder.tv_del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<GoosBean.DataBean.DatasBean> datasBeen = childList.get(i);
                GoosBean.DataBean.DatasBean remove = datasBeen.remove(i1);
                if (datasBeen.size() == 0) {
                    childList.remove(i);
                    groupList.remove(i);
                }
                EventBus.getDefault().post(compute());
                notifyDataSetChanged();
            }
        });
        return view1;
    }

    @Override
    public boolean isChildSelectable(int i, int i1) {
        return false;
    }



    private void changeChildCbState(int groupposition ,boolean flag){
        List<GoosBean.DataBean.DatasBean> beans = childList.get(groupposition);
        for (int i = 0; i <beans.size() ; i++) {
            GoosBean.DataBean.DatasBean bean = beans.get(i);
            bean.setChecked(flag);
        }
    }

    private void changeAllCbState(boolean flag) {
        MessageEvent event = new MessageEvent();
        event.setChecked(flag);
        EventBus.getDefault().post(event);
    }

    private void changGroupCbState(int groupPosition, boolean flag) {
        GoosBean.DataBean dataBean = groupList.get(groupPosition);
        dataBean.setChecked(flag);
    }

    private boolean isAllGroupCbSelected() {
        for (int i = 0; i < groupList.size(); i++) {
            GoosBean.DataBean dataBean = groupList.get(i);
            if (!dataBean.isChecked()) {
                return false;
            }
        }
        return true;
    }

    class GroupViewHolder {
        CheckBox cbGroup;
        TextView tv_number;
    }

    class ChildViewHolder {
        CheckBox cbChild;
        TextView tv_tel;
        TextView tv_content;
        TextView tv_time;
        TextView tv_price;
        TextView tv_del;
        ImageView iv_del;
        ImageView iv_add;
        TextView tv_num;
    }

    private PriceAndCountEvent compute() {
        int count = 0;
        int price = 0;
        for (int i = 0; i < childList.size(); i++) {
            List<GoosBean.DataBean.DatasBean> datasBeen = childList.get(i);
            for (int j = 0; j < datasBeen.size(); j++) {
                GoosBean.DataBean.DatasBean datasBean = datasBeen.get(j);
                if (datasBean.isChecked()) {
                    price += datasBean.getNum() * datasBean.getPrice();
                    count += datasBean.getNum();
                }
            }
        }
        PriceAndCountEvent priceAndCountEvent = new PriceAndCountEvent();
        priceAndCountEvent.setCount(count);
        priceAndCountEvent.setPrice(price);
        return priceAndCountEvent;
    }

    private boolean isAllChildCbSelected(int groupPosition) {
        List<GoosBean.DataBean.DatasBean> datasBeen = childList.get(groupPosition);
        for (int i = 0; i < datasBeen.size(); i++) {
            GoosBean.DataBean.DatasBean datasBean = datasBeen.get(i);
            if (!datasBean.isChecked()) {
                return false;
            }
        }
        return true;
    }

    public void changeAllListCbState(boolean flag) {
        for (int i = 0; i < groupList.size(); i++) {
            changGroupCbState(i, flag);
            changeChildCbState(i, flag);
        }
        EventBus.getDefault().post(compute());
        notifyDataSetChanged();
    }
}



帮助类 
package com.lbp.yuekao.bean;

/**
 * Created by peng on 2017/11/17.
 */

public class MessageEvent {
    private boolean checked;

    public boolean isChecked() {
        return checked;
    }

    public void setChecked(boolean checked) {
        this.checked = checked;
    }
}

package com.lbp.yuekao.bean;

/**
 * Created by peng on 2017/11/17.
 */

public class PriceAndCountEvent {
    private int price;
    private int count;

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值