1.展开二级列表
expandView.setAdapter(eAdapter);
for (int i = 0; i < eAdapter.getGroupCount(); i++) {
expandView.expandGroup(i);
}
2.全选
private void initData() {
int groupCount = eAdapter.getGroupCount();
for (int i = 0; i < groupCount; i++) {
User.DataBean group = (User.DataBean) eAdapter.getGroup(i);
List listBeans = group.getList();
group.setYiCheck(quanxuan.isChecked());
for (int j = 0; j < listBeans.size(); j++) {
listBeans.get(j).setErCheck(quanxuan.isChecked());
getTotal();
}
}
eAdapter.notifyDataSetChanged();
}
3.父级选子级
//大的选小的
expandView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
User.DataBean group = (User.DataBean) eAdapter.getGroup(groupPosition);
group.setYiCheck(!group.isYiCheck());
boolean flag = true;
if (group.isYiCheck()) {
flag = false;
}
List listBeans = group.getList();
for (int i = 0; i < listBeans.size(); i++) {
listBeans.get(i).setErCheck(!flag);
}
eAdapter.notifyDataSetChanged();
getTotal();
return true;
}
});
4.子级的选中
//小的
expandView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
User.DataBean.ListBean child = (User.DataBean.ListBean) eAdapter.getChild(groupPosition, childPosition);
if (fa) {
child.setErCheck(true);
fa = false;
} else {
fa = true;
child.setErCheck(false);
}
eAdapter.notifyDataSetChanged();
getTotal();
return true;
}
});
5.价格的变化
eAdapter.setNumChangeListenern(new AppView.NumChangeListener() {
@Override
public void onNumChangeListener(int pp) {
getTotal();
}
});
private void getTotal() {
double total = 0;
int groupCount = eAdapter.getGroupCount();
for (int i = 0; i < groupCount; i++) {
User.DataBean group = (User.DataBean) eAdapter.getGroup(i);
List listBeans = group.getList();
for (int j = 0; j < listBeans.size(); j++) {
User.DataBean.ListBean listBean = listBeans.get(j);
boolean erCheck = listBean.isErCheck();
if (erCheck == true) {
String price = listBean.getPrice();
total = total + Float.valueOf(price) * listBean.getNum();
}
}
heji.setText("合计:" + total);
}
}
6.adapter
public class ExpandAdapter extends BaseExpandableListAdapter { private Context mContext; private ListmGoodsCartBeanList; private AddView.OnNumberChangeListener mOnNumberChangeListener; public ExpandAdapter(Context context, List goodsCartBeanList) { mContext = context; mGoodsCartBeanList = goodsCartBeanList; } public void setGoodsCartBeanList(List goodsCartBeanList) { mGoodsCartBeanList = goodsCartBeanList; } @Override public int getGroupCount() { return mGoodsCartBeanList.size(); } @Override public int getChildrenCount(int groupPosition) { return mGoodsCartBeanList.get(groupPosition).getGoods().size(); } @Override public Object getGroup(int groupPosition) { return mGoodsCartBeanList.get(groupPosition); } @Override public Object getChild(int groupPosition, int childPosition) { return mGoodsCartBeanList.get(groupPosition).getGoods().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 boolean isChildSelectable(int groupPosition, int childPosition) { return false; } @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { GroupViewHolder groupViewHolder = null; if (convertView == null) { convertView = View.inflate(mContext, R.layout.group_adapter_item, null); groupViewHolder = new GroupViewHolder(); groupViewHolder.groupCk = convertView.findViewById(R.id.ck_groupBox); groupViewHolder.groupName = convertView.findViewById(R.id.tv_groupName); convertView.setTag(groupViewHolder); } else { groupViewHolder = (GroupViewHolder) convertView.getTag(); } groupViewHolder.groupCk.setChecked(mGoodsCartBeanList.get(groupPosition).getGroup_ck()); groupViewHolder.groupName.setText(mGoodsCartBeanList.get(groupPosition).getVend_name()); return convertView; } @Override public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { ChildViewHolder childViewHolder = null; if (convertView == null) { convertView = View.inflate(mContext, R.layout.child_adapter_item, null); childViewHolder = new ChildViewHolder(); childViewHolder.childCk = convertView.findViewById(R.id.ck_childBox); childViewHolder.childImage = convertView.findViewById(R.id.iv_childImage); childViewHolder.childIma = convertView.findViewById(R.id.iv_childIma); childViewHolder.childBianhao = convertView.findViewById(R.id.tv_childBianhao); childViewHolder.childXuhao = convertView.findViewById(R.id.tv_childXuhao); childViewHolder.childMoney = convertView.findViewById(R.id.tv_childPrice); childViewHolder.childPhoto = convertView.findViewById(R.id.bt_photo); childViewHolder.addView = convertView.findViewById(R.id.addView); convertView.setTag(childViewHolder); } else { childViewHolder = (ChildViewHolder) convertView.getTag(); } childViewHolder.childCk.setChecked(mGoodsCartBeanList.get(groupPosition).getGoods().get(childPosition).isChild_ck()); childViewHolder.childBianhao.setText(mGoodsCartBeanList.get(groupPosition).getGoods().get(childPosition).getGoods_name()); childViewHolder.childXuhao.setText(mGoodsCartBeanList.get(groupPosition).getGoods().get(childPosition).getChoose_num()); childViewHolder.childMoney.setText(mGoodsCartBeanList.get(groupPosition).getGoods().get(childPosition).getGoods_price()); Glide.with(mContext).load(mGoodsCartBeanList.get(groupPosition).getGoods().get(childPosition).getGoods_thumb()).into(childViewHolder.childImage); Glide.with(mContext).load(mGoodsCartBeanList.get(groupPosition).getGoods().get(childPosition).getGoods_thumb()).into(childViewHolder.childIma); childViewHolder.childPhoto.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mHttpPhoto.getPhoto(v,groupPosition, childPosition); } }); childViewHolder.addView.setOnNumberChangeListener(new AddView.OnNumberChangeListener() { @Override public void onNumChange(View view, int po) { mGoodsCartBeanList.get(groupPosition).getGoods().get(childPosition).setGoods_number(String.valueOf(po)); if (mOnNumberChangeListener != null) { mOnNumberChangeListener.onNumChange(view, po); } } }); return convertView; } public void setOnNumberChangeListener(AddView.OnNumberChangeListener mOnNumberChangeListener) { this.mOnNumberChangeListener = mOnNumberChangeListener; } class GroupViewHolder { CheckBox groupCk; TextView groupName; } class ChildViewHolder { CheckBox childCk; ImageView childImage, childIma; TextView childBianhao, childXuhao, childMoney; AddView addView; Button childPhoto; } private httpPhoto mHttpPhoto; public void setHttpPhoto(httpPhoto mHttpPhoto) { this.mHttpPhoto = mHttpPhoto; } public interface httpPhoto { void getPhoto(View view, int groupPo, int childPo); } }