compile 'org.greenrobot:eventbus:3.0.0'
主布局代码
package com.example.cll.erjigouwuche; 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(false,"商家" + i); groupList.add(groupBean); List<Childbean> list = new ArrayList<>(); for (int j = 0; j < 2; j++) { Childbean childBean = new Childbean(false,"商品" + j ); 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); } } 主布局<?xml version="1.0" encoding="utf-8"?> <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="com.example.cll.erjigouwuche.MainActivity"> <TextView android:layout_width="match_parent" android:layout_height="40dp" android:background="#ff3660" android:gravity="center" android:text="购物车" android:textColor="#ffffff" android:textSize="20sp" /> <ExpandableListView android:id="@+id/elv" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" /> <RelativeLayout android:layout_width="match_parent" android:layout_height="50dp" android:background="#33000000"> <CheckBox android:id="@+id/cbAll" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:text="全选" /> <TextView