view层代码
///
private ExpandableListView mElv;
/**
* 全选
*/
private CheckBox mAll;
private ElvAdapter elvAdapter;
/**
* 总价
*/
private TextView mZongjia;
private ShopPresenter shopPresenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shop_good);
initView();
//全选/反选
mAll.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
elvAdapter.changeAllListCbState(mAll.isChecked());
}
});
initData();
}
private void initData() {
shopPresenter = new ShopPresenter(this);
shopPresenter.getDataFrom(Contans.GOODSHOP + "?uid=71");
}
private void initView() {
mElv = (ExpandableListView) findViewById(R.id.elv);
mAll = (CheckBox) findViewById(R.id.all);
mZongjia = (TextView) findViewById(R.id.zongjia);
}
@Override
public void onSuccess(String string) {
ShopBean shopBean = new Gson().fromJson(string, ShopBean.class);
List<ShopBean.DataBean> data = shopBean.getData();
List<List<ShopBean.DataBean.ListBean>> childList = new ArrayList<>();
for (int i = 0; i < data.size(); i++) {
List<ShopBean.DataBean.ListBean> list = data.get(i).getList();
childList.add(list);
}
Log.e("child", "child ---" + childList.size());
elvAdapter = new ElvAdapter(data, childList, this);
elvAdapter.notifyDataSetChanged();
mElv.setAdapter(elvAdapter);
//默认展开
for (int i = 0; i < data.size(); i++) {
mElv.expandGroup(i);
}
}
//eventbus使用
@Override
protected void onStart() {
super.onStart();
EventBus.getDefault().register(this);
}
//全选框
@Subscribe
public void onMessageEvent(MessageEvent event) {
mAll.setChecked(event.isChecked());
}
//总价赋值
@Subscribe
public void onMessageEvent(PriceAndCountEvent event) {
// mTvNum.setText("结算(" + event.getCount() + ")");
mZongjia.setText("¥"+event.getPrice() );
}
//解绑
@Override
protected void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
if ( shopPresenter!= null) {
shopPresenter.detachView();
}
}
}
//////////////////////////////////////////
二级列表适配器
public class ElvAdapter extends BaseExpandableListAdapter {
private List<ShopBean.DataBean> data;
private List<List<ShopBean.DataBean.ListBean>> childList;
private Context context;
private boolean box = true;
private boolean cBox = false;
public ElvAdapter(List<ShopBean.DataBean> data, List<List<ShopBean.DataBean.ListBean>> childList, Context context) {
this.data = data;
this.childList = childList;
this.context = context;
}
@Override
public int getGroupCount() {
return data.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return childList.get(groupPosition).size();
}
@Override
public Object getGroup(int groupPosition) {
return data.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(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
final GroupViewHolder groupViewHolder;
if (convertView == null) {
groupViewHolder = new GroupViewHolder();
convertView = View.inflate(context, R.layout.group_item, null);
groupViewHolder.cbSeller = convertView.findViewById(R.id.group_box);
groupViewHolder.tvSeller = convertView.findViewById(R.id.shop);
convertView.setTag(groupViewHolder);
} else {
groupViewHolder = (GroupViewHolder) convertView.getTag();
}
//设置值
// ShopBean.DataBean dataBean = data.get(groupPosition);
groupViewHolder.tvSeller.setText(data.get(groupPosition).getSellerName());
groupViewHolder.cbSeller.setChecked(data.get(groupPosition).isGroupBox());
groupViewHolder.cbSeller.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
data.get(groupPosition).setGroupBox(groupViewHolder.cbSeller.isChecked());
changeChildCbState(groupPosition,groupViewHolder.cbSeller.isChecked());
EventBus.getDefault().post(compute());
changeAllCbState(isAllGroupCbSelected());
notifyDataSetChanged();
}
});
return convertView;
}
@Override
public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
final ChildViewHolder childViewHolder;
if (convertView == null) {
childViewHolder = new ChildViewHolder();
convertView = View.inflate(context, R.layout.child_item, null);
childViewHolder.cbProduct = convertView.findViewById(R.id.child_box);
childViewHolder.iv = convertView.findViewById(R.id.good_img);
childViewHolder.tvTitle = convertView.findViewById(R.id.good_title);
childViewHolder.tvPrice = convertView.findViewById(R.id.good_price);
convertView.setTag(childViewHolder);
} else {
childViewHolder = (ChildViewHolder) convertView.getTag();
}
//根据服务器返回的select值,给checkBox设置是否选中
childViewHolder.cbProduct.setChecked(childList.get(groupPosition).get(childPosition).isChildBox());
childViewHolder.tvTitle.setText(childList.get(groupPosition).get(childPosition).getTitle());
childViewHolder.tvPrice.setText("优惠价" + childList.get(groupPosition).get(childPosition).getBargainPrice() + "");
Glide.with(context).load(childList.get(groupPosition).get(childPosition).getImages().split("\\|")[0]).into(childViewHolder.iv);
childViewHolder.cbProduct.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
childList.get(groupPosition).get(childPosition).setChildBox(childViewHolder.cbProduct.isChecked());
PriceAndCountEvent compute = compute();
EventBus.getDefault().post(compute);
if (childViewHolder.cbProduct.isChecked()){
if (isAllChildCbSelected(groupPosition)){
changGroupCbState(groupPosition,childViewHolder.cbProduct.isChecked());
changeAllCbState(isAllGroupCbSelected());
}
}else{
changGroupCbState(groupPosition,childViewHolder.cbProduct.isChecked());
changeAllCbState(isAllGroupCbSelected());
}
notifyDataSetChanged();
}
});
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
class GroupViewHolder {
CheckBox cbSeller;
TextView tvSeller;
}
class ChildViewHolder {
CheckBox cbProduct;
ImageView iv;
TextView tvTitle;
TextView tvPrice;
}
/*
改变全选状态
*/
private void changeAllCbState(boolean flag){
MessageEvent messageEvent = new MessageEvent();
messageEvent.setChecked(flag);
EventBus.getDefault().post(messageEvent);
}
/**
* 改变一级列表checkbox状态
*
* @param groupPosition
*/
private void changGroupCbState(int groupPosition, boolean flag) {
ShopBean.DataBean dataBean = data.get(groupPosition);
dataBean.setGroupBox(flag);
}
/**
* 改变二级列表的CheckBox
*/
private void changeChildCbState(int groupPosition, boolean flag){
List<ShopBean.DataBean.ListBean> listBeans = childList.get(groupPosition);
for (int i = 0; i < listBeans.size(); i++) {
ShopBean.DataBean.ListBean listBean = listBeans.get(i);
listBean.setChildBox(flag);
}
}
/*
判断一级是否全选
*/
private boolean isAllGroupCbSelected(){
for (int i = 0; i < data.size(); i++) {
ShopBean.DataBean dataBean = data.get(i);
if (!dataBean.isGroupBox()){
return false;
}
}
return true;
}
/*
判断二级列表是否全选
*/
private boolean isAllChildCbSelected(int grouPosition){
List<ShopBean.DataBean.ListBean> listBeans = childList.get(grouPosition);
for (int i = 0; i < listBeans.size(); i++) {
ShopBean.DataBean.ListBean listBean = listBeans.get(i);
if (!listBean.isChildBox()){
return false;
}
}
return true;
}
/*
计算列表 选中的钱
*/
private PriceAndCountEvent compute(){
int price = 0 ;
for (int i = 0; i < childList.size(); i++) {
List<ShopBean.DataBean.ListBean> listBeans = childList.get(i);
for (int j = 0; j < listBeans.size(); j++) {
ShopBean.DataBean.ListBean listBean = listBeans.get(j);
if (listBean.isChildBox()){
price+=listBean.getBargainPrice()*listBean.getNum();
}
}
}
Log.e("e","e---"+price);
PriceAndCountEvent priceAndCountEvent = new PriceAndCountEvent();
priceAndCountEvent.setPrice(price);
return priceAndCountEvent;
}
/*
设置全选。反选
*/
public void changeAllListCbState(boolean flag) {
for (int i = 0; i < data.size(); i++) {
changGroupCbState(i,flag);
changeChildCbState(i,flag);
}
EventBus.getDefault().post(compute());
notifyDataSetChanged();
}
}