>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();
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());
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);
if (isChildChecked(childBeen)) {
groupList.get(groupPosition).setChecked(true);
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 cb = (CheckBox) v;
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);
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()) {
count++;
}
}
}
return count;
}
}