购物车里的二级联动的复选框全选和总计


///先是main.xml的布局//


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="wangziying.baway.com.myer.MainActivity">

   <TextView
       android:layout_width="match_parent"
       android:layout_height="40dp"
       android:background="#f00"
       android:text="购物车"
       android:textSize="20dp"
       android:textColor="#fff"
       android:gravity="center"/>

    <ExpandableListView
        android:id="@+id/elv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"></ExpandableListView>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="50dp">
        <CheckBox
            android:id="@+id/main_check"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="5dp"
            android:text="全选"
            />
        <TextView
            android:id="@+id/main_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_alignParentRight="true"
            android:textColor="#000"
            android:layout_marginRight="20dp"
            android:text="合计:"/>
    </RelativeLayout>


</LinearLayout>
 
 
///再是Mainactivity//
 
public class MainActivity extends AppCompatActivity {

    private ExpandableListView elv;
    private CheckBox main_check;
    private TextView main_text;

    private List<GroupBean> groupBeanList=new ArrayList<>();

    private List<List<ChildBean>> childBeanlist=new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        EventBus.getDefault().register(this);

        elv = (ExpandableListView) findViewById(R.id.elv);
        main_check = (CheckBox) findViewById(R.id.main_check);
        main_text = (TextView) findViewById(R.id.main_text);

        //模拟数据
        inits();

        final Adapter adapter = new Adapter(groupBeanList, childBeanlist,this);

        elv.setAdapter(adapter);
//     默认没有选中
        elv.setGroupIndicator(null);
//      全部展开
        for (int i=0;i<groupBeanList.size();i++){

            elv.expandGroup(i);
        }

//        全选的点击事件

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

                adapter.allChecked(main_check.isChecked());

            }
        });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        EventBus.getDefault().unregister(this);
    }
@Subscribe  //这一定不要忘记写,要不然就是不知道谁在用它
    public void Message(MessageEvent msg){
        main_check.setChecked(msg.isFlag());
    }


    @Subscribe
    public  void messa(MessageCounEvent msg){
        main_text.setText("总计:"+msg.getCount()+"");
    }

    private void inits() {

        for(int i=0;i<3;i++){

            GroupBean groupBean = new GroupBean("商铺" + i, false);

            groupBeanList.add(groupBean);

            List<ChildBean> list=new ArrayList<>();
            for (int j=0;j<2;j++){
                ChildBean childBean = new ChildBean("商品" + j, false);

                list.add(childBean);
            }

            childBeanlist.add(list);
        }
    }
}
///别忘记给EventBus到依赖
compile 'org.greenrobot:eventbus:3.0.0'

/适配器
public class Adapter extends BaseExpandableListAdapter {

    private List<GroupBean> groupBeanList;
    private List<List<ChildBean>> childBeanlist;
    private Context context;
    private int count;

    public Adapter(List<GroupBean> groupBeanList, List<List<ChildBean>> childBeanlist, Context context) {
        this.groupBeanList = groupBeanList;
        this.childBeanlist = childBeanlist;
        this.context = context;
    }

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

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

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

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return childBeanlist.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;

        Mygroupholder holder;

        if(convertView==null){
            holder=new Mygroupholder();

            view=View.inflate(context,R.layout.item_group,null);

            holder.ck=view.findViewById(R.id.ck);
            holder.tv=view.findViewById(R.id.tv);

            view.setTag(holder);

        }else{

            view=convertView;
            holder= (Mygroupholder) view.getTag();
        }

        GroupBean groupBean = groupBeanList.get(groupPosition);

        holder.ck.setChecked(groupBean.isChecked());
        holder.tv.setText(groupBean.getName());

        holder.ck.setOnClickListener(new GroupOnClick(groupPosition));

        return view;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        View view;

        Mychildholder holder;

        if(convertView==null){
            holder=new Mychildholder();

            view=View.inflate(context,R.layout.item_child,null);

            holder.ck=view.findViewById(R.id.ck);
            holder.tv=view.findViewById(R.id.tv);

            view.setTag(holder);

        }else{

            view=convertView;
            holder= (Mychildholder) view.getTag();
        }

        ChildBean childBean = childBeanlist.get(groupPosition).get(childPosition);

        holder.ck.setChecked(childBean.isChecked());
        holder.tv.setText(childBean.getName());

        holder.ck.setOnClickListener(new ChildOnClick(groupPosition,childPosition));
        return view;
    }

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

//    优化

    class Mygroupholder{

        CheckBox ck;
        TextView tv;
    }
    class Mychildholder{

        CheckBox ck;
        TextView tv;
    }

//    group的监听
    class GroupOnClick implements View.OnClickListener{

        private int groupPosition;

        public GroupOnClick(int groupPosition) {
            this.groupPosition = groupPosition;
        }

        @Override
        public void onClick(View v) {

            if(v instanceof CheckBox){

                CheckBox ck= (CheckBox) v;

                groupBeanList.get(groupPosition).setChecked(ck.isChecked());

                List<ChildBean> childBeanList=childBeanlist.get(groupPosition);

                for(ChildBean childBean:childBeanList){

                    childBean.setChecked(ck.isChecked());
                }
                //计算选中的商品数,并发送到主界面进行显示
                MessageCounEvent msgCount = new MessageCounEvent();
                msgCount.setCount(totalCount());
                EventBus.getDefault().post(msgCount);


                MessageEvent msg=new MessageEvent();

                msg.setFlag(isGroupChecked());

                EventBus.getDefault().post(msg);
                notifyDataSetChanged();

            }
        }

    }
/*
*判断其它的商家是否选中
* */
    private boolean isGroupChecked(){

        for(GroupBean groupBean:groupBeanList){
            if (!groupBean.isChecked()){

                return false;
            }
        }

        return true;
    }


    //    child的监听
    class ChildOnClick implements View.OnClickListener{

        private int groupPosition;
        private int childPosition;

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

        @Override
        public void onClick(View v) {

            if(v instanceof CheckBox){

                CheckBox ck= (CheckBox) v;
                List<ChildBean> childBeen=childBeanlist.get(groupPosition);
                ChildBean childBean = childBeen.get(childPosition);
                childBean.setChecked(ck.isChecked());

                //计算选中的商品数,并发送到主界面进行显示
                MessageCounEvent msgCount = new MessageCounEvent();
                msgCount.setCount(totalCount());
                EventBus.getDefault().post(msgCount);

                //判断该商家的所有商品的checkbox是否都选中
                if(isChildChecked(childBeen)){
                    groupBeanList.get(groupPosition).setChecked(true);

                    MessageEvent msg=new MessageEvent();

                    msg.setFlag(isGroupChecked());
                    EventBus.getDefault().post(msg);

                    notifyDataSetChanged();
                }else{
                    groupBeanList.get(groupPosition).setChecked(false);
                    MessageEvent msg=new MessageEvent();

                    msg.setFlag(false);

                    EventBus.getDefault().post(msg);

                    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;
    }

//    主界面的全选操作

    public void allChecked(boolean bool){
        for (int i = 0; i < groupBeanList.size(); i++) {
            groupBeanList.get(i).setChecked(bool);
            for (int j = 0; j < childBeanlist.get(i).size(); j++) {
                childBeanlist.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<groupBeanList.size();i++){
            for (int j=0;j<childBeanlist.get(i).size();j++){
                if (childBeanlist.get(i).get(j).isChecked()){
                    //遍历所有的商品,只要是选中状态的,就加1
                    count++;
                }
            }
        }

        return count;
    }
}
//group的布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:orientation="horizontal">

    <CheckBox
        android:id="@+id/ck"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        />
    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:textColor="#000"
        android:textSize="15dp" />

</LinearLayout>
/child的布局/
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:padding="20dp"
    android:orientation="horizontal">

    <CheckBox
        android:id="@+id/ck"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        />
    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:textColor="#000"
        android:textSize="15dp" />

</LinearLayout>
///groupBean/
public class GroupBean {

    private String name;
    private boolean checked;

    public GroupBean() {
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public boolean isChecked() {
        return checked;
    }

    public void setChecked(boolean checked) {
        this.checked = checked;
    }
}
/childbean
public class ChildBean {

    private String name;
    private boolean checked;

    public ChildBean() {
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public boolean isChecked() {
        return checked;
    }

    public void setChecked(boolean checked) {
        this.checked = checked;
    }
}
/Message的来确定是否是否选中的bean包
public class MessageEvent {

    private boolean flag;

    public boolean isFlag() {
        return flag;
    }

    public void setFlag(boolean flag) {
        this.flag = flag;
    }
}
//Messa用来总计个数的bean包
public class MessageCounEvent {

    private int count;

    public int getCount() {
        return count;
    }

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值