ShoppingCart

>public class ChildBean {
    private String childName;
    private boolean checked;

  >  public ChildBean() {
    }

  >  public ChildBean(String childName, boolean checked) {
        this.childName = childName;
        this.checked = checked;
>    }

    public String getChildName() {
        return childName;
    }

>    public void setChildName(String childName) {
        this.childName = childName;
    }

    public boolean isChecked() {
        return checked;
    }

    public void setChecked(boolean checked) {
        this.checked = checked;
    }
>}
public class GroupBean {
    private String groupName;
    private boolean checked;

    public GroupBean() {
    }

    public GroupBean(String groupName, boolean checked) {
        this.groupName = groupName;
        this.checked = checked;
    }

    public String getGroupName() {
        return groupName;
    }

    public void setGroupName(String groupName) {
        this.groupName = groupName;
    }

    public boolean isChecked() {
        return checked;
    }

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

MainActivity



import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ExpandableListView;
import android.widget.TextView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private ExpandableListView mElv;
    /**
     * 全选
     */
    private CheckBox mCbAll;
    /**
     * 合计:
     */
    private TextView mTvTotal;
    private List<GroupBean> groupList = new ArrayList<>();
    private List<List<ChildBean>> childList = new ArrayList<>();
    private MyAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        EventBus.getDefault().register(this);
        initView();
        //给设置ExpandableListView设置数据
        //模拟数据
        initData();
        adapter = new MyAdapter(this, groupList, childList);
        mElv.setGroupIndicator(null);
        mElv.setAdapter(adapter);
        //全部展开
        for (int i = 0; i < groupList.size(); i++) {
            mElv.expandGroup(i);
        }
        //给全选设置点击事件
        mCbAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                adapter.allChecked(mCbAll.isChecked());
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe
    public void messageCountEvent(MessageCounEvent msg) {
        mTvTotal.setText("总计:" + msg.getCount() + "个");
    }

    @Subscribe
    public void messageEvent(MessageEvent msg) {
        mCbAll.setChecked(msg.isFlag());
    }

    private void initData() {
        for (int i = 0; i < 3; i++) {
            GroupBean groupBean = new GroupBean("商家" + i, false);
            groupList.add(groupBean);
            List<ChildBean> list = new ArrayList<>();
            for (int j = 0; j < 2; j++) {
                ChildBean childBean = new ChildBean("商品" + i, false);
                list.add(childBean);
            }
            childList.add(list);
        }
    }

    private void initView() {
        mElv = (ExpandableListView) findViewById(R.id.elv);
        mCbAll = (CheckBox) findViewById(R.id.cbAll);
        mTvTotal = (TextView) findViewById(R.id.tvTotal);
    }


}


public class MessageCounEvent {
    private int count;

    public int getCount() {
        return count;
    }

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



public class MessageEvent {
    private boolean flag;

    public boolean isFlag() {
        return flag;
    }

    public void setFlag(boolean flag) {
        this.flag = flag;
    }
}


Myadapter



import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import static com.baway.week2exam.R.id.cb;


public class MyAdapter extends BaseExpandableListAdapter {
    private Context context;
    private List<GroupBean> groupList;
    private List<List<ChildBean>> childList;
    private int count;

    public MyAdapter(Context context, List<GroupBean> groupList, List<List<ChildBean>> childList) {
        this.context = context;
        this.groupList = groupList;
        this.childList = childList;
    }

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

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

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

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return childList.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(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        View view;
        GroupViewHolder holder;
        if (convertView == null) {
            holder = new GroupViewHolder();
            view = View.inflate(context, R.layout.item, null);
            holder.cb = view.findViewById(cb);
            holder.tv = view.findViewById(R.id.tvName);
            view.setTag(holder);
        } else {
            view = convertView;
            holder = (GroupViewHolder) view.getTag();
        }
        //赋值
        GroupBean groupBean = groupList.get(groupPosition);
        holder.cb.setChecked(groupBean.isChecked());
        holder.tv.setText(groupBean.getGroupName());
        //给group的checkbox设置点击事件
        holder.cb.setOnClickListener(new GroupCbOnClickListener(groupPosition));
        return view;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        View view;
        ChildViewHolder holder;
        if (convertView == null) {
            holder = new ChildViewHolder();
            view = View.inflate(context, R.layout.childitem, null);
            holder.cb = view.findViewById(cb);
            holder.tv = view.findViewById(R.id.tvName);
            view.setTag(holder);
        } else {
            view = convertView;
            holder = (ChildViewHolder) view.getTag();
        }
        //赋值
        ChildBean childBean = childList.get(groupPosition).get(childPosition);
        holder.cb.setChecked(childBean.isChecked());
        holder.tv.setText(childBean.getChildName());

        holder.cb.setOnClickListener(new ChildCbOnClickListener(groupPosition, childPosition));
        return view;
    }

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

    class GroupViewHolder {
        CheckBox cb;
        TextView tv;
    }

    class ChildViewHolder {
        CheckBox cb;
        TextView tv;
    }

    class ChildCbOnClickListener implements View.OnClickListener {
        private int groupPosition;
        private int childPosition;

        public ChildCbOnClickListener(int groupPosition, int childPosition) {
            this.groupPosition = groupPosition;
            this.childPosition = childPosition;
        }

        @Override
        public void onClick(View v) {
            if (v instanceof CheckBox) {


                CheckBox cb = (CheckBox) v;
                List<ChildBean> childBeen = childList.get(groupPosition);
                ChildBean childBean = childBeen.get(childPosition);
                childBean.setChecked(cb.isChecked());
                //计算选中的商品数,并发送到主界面进行显示
                MessageCounEvent msgCount = new MessageCounEvent();
                msgCount.setCount(totalCount());
                EventBus.getDefault().post(msgCount);
                //判断该商家的所有商品的checkbox是否都选中
                if (isChildChecked(childBeen)) {
                    groupList.get(groupPosition).setChecked(true);
//                    //如果商品全部选中的话,则去判断所有商家是否都选中
//                    if (isGroupChecked()) {
//                        //发送消息去改变全选的状态,变成选中状态
//                    } else {
//                        //发送消息去改变全选的状态,变成未选中状态
//                    }
                    MessageEvent msg = new MessageEvent();
                    msg.setFlag(isGroupChecked());
                    EventBus.getDefault().post(msg);
                    notifyDataSetChanged();
                } else {
                    groupList.get(groupPosition).setChecked(false);
                    MessageEvent msg = new MessageEvent();
                    msg.setFlag(false);
                    EventBus.getDefault().post(msg);
                    notifyDataSetChanged();
                    notifyDataSetChanged();
                }
            }


        }
    }

    /**
     * 判断该商家的所有商品的checkbox是否都选中
     *
     * @return
     */
    private boolean isChildChecked(List<ChildBean> childBeen) {
        for (int i = 0; i < childBeen.size(); i++) {
            ChildBean childBean = childBeen.get(i);
            if (!childBean.isChecked()) {
                return false;
            }
        }
        return true;
    }

    class GroupCbOnClickListener implements View.OnClickListener {
        private int groupPostion;

        public GroupCbOnClickListener(int groupPostion) {
            this.groupPostion = groupPostion;
        }

        @Override
        public void onClick(View v) {
            if (v instanceof CheckBox) {

                //多态,因为我是给checkbox设置的点击事件,所以可以强转成checkbox
                CheckBox cb = (CheckBox) v;
                //根据cb.isChecked()是否选中,给一级列的checkbox改变状态
                groupList.get(groupPostion).setChecked(cb.isChecked());
                List<ChildBean> childBeenList = childList.get(groupPostion);
                for (ChildBean childBean : childBeenList) {
                    childBean.setChecked(cb.isChecked());
                }
                //计算选中的商品数,并发送到主界面进行显示
                MessageCounEvent msgCount = new MessageCounEvent();
                msgCount.setCount(totalCount());
                EventBus.getDefault().post(msgCount);

                //判断其它的商家是否选中
//                if (isGroupChecked()) {
//                    //发送消息去改变全选的状态,变成选中状态
//                    MessageEvent msg = new MessageEvent();
//                    msg.setFlag(true);
//                    EventBus.getDefault().post(msg);
//                } else {
//                    //发送消息去改变全选的状态,变成未选中状态
//                    MessageEvent msg = new MessageEvent();
//                    msg.setFlag(false);
//                    EventBus.getDefault().post(msg);
//                }
                MessageEvent msg = new MessageEvent();
                msg.setFlag(isGroupChecked());
                EventBus.getDefault().post(msg);
                notifyDataSetChanged();
            }
        }
    }

    /**
     * 判断其它的商家是否选中
     *
     * @return
     */
    private boolean isGroupChecked() {
        for (GroupBean groupBean : groupList) {
            if (!groupBean.isChecked()) {
                return false;
            }
        }
        return true;
    }

    /**
     * 主界面全选按钮的操作
     *
     * @param bool
     */
    public void allChecked(boolean bool) {
        for (int i = 0; i < groupList.size(); i++) {
            groupList.get(i).setChecked(bool);
            for (int j = 0; j < childList.get(i).size(); j++) {
                childList.get(i).get(j).setChecked(bool);
            }
        }
        //计算选中的商品数,并发送到主界面进行显示
        MessageCounEvent msgCount = new MessageCounEvent();
        msgCount.setCount(totalCount());
        EventBus.getDefault().post(msgCount);
        notifyDataSetChanged();

    }

    private int totalCount() {
        count = 0;
        for (int i = 0; i < groupList.size(); i++) {
            for (int j = 0; j < childList.get(i).size(); j++) {
                if (childList.get(i).get(j).isChecked()) {
                    //遍历所有的商品,只要是选中状态的,就加1
                    count++;
                }
            }
        }
        return count;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值