布局就自己写吧不上传了
代码实现
Main22Activity
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;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;
import model.ElvDataBean;
import prestern.Elvpresenter;
import prestern.MessageEvent;
import prestern.MyElvadapter;
import prestern.PriceAndCountEvent;
import view.Iview;
public class Main22Activity extends AppCompatActivity implements Iview {
private ExpandableListView exlv;
private CheckBox checkBox;
private MyElvadapter adapter;
private List<ElvDataBean.DataBean> grouplist = new ArrayList<>();
private List<List<ElvDataBean.DataBean.ListBean>> childlist = new ArrayList<>();
private TextView price;
private TextView num;
private Elvpresenter elvp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main22);
exlv= (ExpandableListView) findViewById(R.id.exlv);
checkBox= (CheckBox) findViewById(R.id.checkbox);
price= (TextView) findViewById(R.id.price);
num= (TextView) findViewById(R.id.num);
EventBus.getDefault().register(this);
//全选/全不选
checkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//设置全选
adapter.changeAllListCbState(checkBox.isChecked());
}
});
loadData();
}
private void loadData() {
elvp = new Elvpresenter(this);
elvp.get();
}
@Override
public void Success(ElvDataBean elvDataBean) {
grouplist.clear();
List<ElvDataBean.DataBean> data = elvDataBean.getData();
grouplist.addAll(data);
childlist.clear();
for (int i=0;i<data.size();i++){
ElvDataBean.DataBean dataBean = data.get(i);
List<ElvDataBean.DataBean.ListBean> list = dataBean.getList();
childlist.add(list);
}
adapter =new MyElvadapter(this,grouplist,childlist);
exlv.setAdapter(adapter);
exlv.setGroupIndicator(null);
// 展开
for (int i=0;i<grouplist.size();i++){
exlv.expandGroup(i);
}
adapter.notifyDataSetChanged();
}
@Override
public void Failed(Exception e) {
}
@Subscribe
public void onMessageEvent(MessageEvent event) {
checkBox.setChecked(event.isCheckd());
}
@Subscribe
public void onMessageEven(PriceAndCountEvent event) {
price.setText("总价" + event.getPrice());
num.setText("共" + event.getCount() + "件商品");
}
@Override
protected void onDestroy() {
super.onDestroy();
//eventBus销毁
EventBus.getDefault().unregister(this);
elvp.onDetchView();
}
}
实现接口Bean
import java.util.List;
/**
* Created by HASEE on 2017/11/22.
*/
public class ElvDataBean {
/**
* msg : 请求成功
* code : 0
* data : [{"list":[{"bargainPrice":22.9,"createtime":"2017-10-14T21:48:08","detailUrl":"https://item.m.jd.com/product/2542855.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t1930/284/2865629620/390243/e3ade9c4/56f0a08fNbd3a1235.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t2137/336/2802996626/155915/e5e90d7a/56f0a09cN33e01bd0.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t1882/31/2772215910/389956/c8dbf370/56f0a0a2Na0c86ea6.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t2620/166/2703833710/312660/531aa913/57709035N33857877.jpg!q70.jpg","num":2,"pid":24,"price":288,"pscid":2,"selected":0,"sellerid":1,"subhead":"三只松鼠零食特惠,专区满99减50,满199减100,火速抢购》","title":"三只松鼠 坚果炒货 零食奶油味 碧根果225g/袋"}],"sellerName":"商家1","sellerid":"1"},{"list":[{"bargainPrice":11800,"createtime":"2017-10-14T21:38:26","detailUrl":"https://item.m.jd.com/product/5025518.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t8830/106/1760940277/195595/5cf9412f/59bf2ef5N5ab7dc16.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5428/70/1520969931/274676/b644dd0d/591128e7Nd2f70da0.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5566/365/1519564203/36911/620c750c/591128eaN54ac3363.jpg!q70.jpg","num":1,"pid":58,"price":6399,"pscid":40,"selected":0,"sellerid":2,"subhead":"升级4G大显存!Nvme协议Pcie SSD,速度快人一步】GTX1050Ti就选拯救者!专业游戏键盘&新模具全新设计!","title":"联想(Lenovo)拯救者R720 15.6英寸游戏笔记本电脑(i5-7300HQ 8G 1T+128G SSD GTX1050Ti 4G IPS 黑)"},{"bargainPrice":6666,"createtime":"2017-10-10T16:01:31","detailUrl":"https://item.m.jd.com/product/5089273.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t8284/363/1326459580/71585/6d3e8013/59b857f2N6ca75622.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t9346/182/1406837243/282106/68af5b54/59b8480aNe8af7f5c.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t8434/54/1359766007/56140/579509d9/59b85801Nfea207db.jpg!q70.jpg","num":1,"pid":46,"price":234,"pscid":39,"selected":0,"sellerid":2,"subhead":"【iPhone新品上市】新一代iPhone,让智能看起来更不一样","title":"Apple iPhone 8 Plus (A1864) 64GB 金色 移动联通电信4G手机"}],"sellerName":"商家2","sellerid":"2"},{"list":[{"bargainPrice":111.99,"createtime":"2017-10-14T21:39:05","detailUrl":"https://item.m.jd.com/product/4719303.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t9004/210/1160833155/647627/ad6be059/59b4f4e1N9a2b1532.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7504/338/63721388/491286/f5957f53/598e95f1N7f2adb87.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7441/10/64242474/419246/adb30a7d/598e95fbNd989ba0a.jpg!q70.jpg","num":87,"pid":10,"price":555.55,"pscid":1,"selected":0,"sellerid":3,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"}],"sellerName":"商家3","sellerid":"3"},{"list":[{"bargainPrice":11800,"createtime":"2017-10-14T21:38:26","detailUrl":"https://mitem.jd.hk/ware/view.action?wareId=1988853309&cachekey=1acb07a701ece8d2434a6ae7fa6870a1","images":"https://m.360buyimg.com/n0/jfs/t6130/97/1370670410/180682/1109582a/593276b1Nd81fe723.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5698/110/2617517836/202970/c9388feb/593276b7Nbd94ef1f.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5698/110/2617517836/202970/c9388feb/593276b7Nbd94ef1f.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5815/178/2614671118/51656/7f52d137/593276c7N107b725a.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5878/60/2557817477/30873/4502b606/593276caN5a7d6357.jpg!q70.jpg","num":1,"pid":61,"price":14999,"pscid":40,"selected":0,"sellerid":5,"subhead":"购买电脑办公部分商品满1元返火车票5元优惠券(返完即止)","title":"全球购 新款Apple MacBook Pro 苹果笔记本电脑 银色VP2新13英寸Bar i5/8G/256G"}],"sellerName":"商家5","sellerid":"5"},{"list":[{"bargainPrice":159,"createtime":"2017-10-14T21:49:15","detailUrl":"https://item.m.jd.com/product/5061723.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t8716/197/1271594444/173291/2f40bb4f/59b743bcN8509428e.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t8347/264/1286771527/92188/5cf5ec04/59b7420fN65378e9e.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7363/165/3000956253/190883/179a372/59b743bfNd0c79d93.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7399/112/2935531768/183594/b77c7d4a/59b7441aNc3d40133.jpg!q70.jpg","num":1,"pid":100,"price":2200,"pscid":112,"selected":0,"sellerid":11,"subhead":"针织针织闪闪闪亮你的眼","title":"维迩旎 2017秋冬新款长袖针织连衣裙韩版气质中长款名媛包臀A字裙 zx179709 黑色 XL"}],"sellerName":"商家11","sellerid":"11"},{"list":[{"bargainPrice":11800,"createtime":"2017-10-14T21:38:26","detailUrl":"https://mitem.jd.hk/ware/view.action?wareId=1988853309&cachekey=1acb07a701ece8d2434a6ae7fa6870a1","images":"https://m.360buyimg.com/n0/jfs/t6130/97/1370670410/180682/1109582a/593276b1Nd81fe723.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5698/110/2617517836/202970/c9388feb/593276b7Nbd94ef1f.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5698/110/2617517836/202970/c9388feb/593276b7Nbd94ef1f.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5815/178/2614671118/51656/7f52d137/593276c7N107b725a.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5878/60/2557817477/30873/4502b606/593276caN5a7d6357.jpg!q70.jpg","num":1,"pid":69,"price":16999,"pscid":40,"selected":0,"sellerid":13,"subhead":"购买电脑办公部分商品满1元返火车票5元优惠券(返完即止)","title":"全球购 新款Apple MacBook Pro 苹果笔记本电脑 银色VP2新13英寸Bar i5/8G/256G"}],"sellerName":"商家13","sellerid":"13"},{"list":[{"bargainPrice":111.99,"createtime":"2017-10-14T21:39:05","detailUrl":"https://item.m.jd.com/product/4719303.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t9004/210/1160833155/647627/ad6be059/59b4f4e1N9a2b1532.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7504/338/63721388/491286/f5957f53/598e95f1N7f2adb87.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7441/10/64242474/419246/adb30a7d/598e95fbNd989ba0a.jpg!q70.jpg","num":2,"pid":1,"price":118,"pscid":1,"selected":0,"sellerid":17,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"}],"sellerName":"商家17","sellerid":"17"},{"list":[{"bargainPrice":111.99,"createtime":"2017-10-14T21:39:05","detailUrl":"https://item.m.jd.com/product/4719303.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t9004/210/1160833155/647627/ad6be059/59b4f4e1N9a2b1532.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7504/338/63721388/491286/f5957f53/598e95f1N7f2adb87.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7441/10/64242474/419246/adb30a7d/598e95fbNd989ba0a.jpg!q70.jpg","num":3,"pid":2,"price":299,"pscid":1,"selected":0,"sellerid":18,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"}],"sellerName":"商家18","sellerid":"18"}]
*/
private String msg;
private String code;
private List<DataBean> data;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public List<DataBean> getData() {
return data;
}
public void setData(List<DataBean> data) {
this.data = data;
}
public static class DataBean {
public boolean isChecked() {
return checked;
}
public void setChecked(boolean checked) {
this.checked = checked;
}
/**
* list : [{"bargainPrice":22.9,"createtime":"2017-10-14T21:48:08","detailUrl":"https://item.m.jd.com/product/2542855.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t1930/284/2865629620/390243/e3ade9c4/56f0a08fNbd3a1235.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t2137/336/2802996626/155915/e5e90d7a/56f0a09cN33e01bd0.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t1882/31/2772215910/389956/c8dbf370/56f0a0a2Na0c86ea6.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t2620/166/2703833710/312660/531aa913/57709035N33857877.jpg!q70.jpg","num":2,"pid":24,"price":288,"pscid":2,"selected":0,"sellerid":1,"subhead":"三只松鼠零食特惠,专区满99减50,满199减100,火速抢购》","title":"三只松鼠 坚果炒货 零食奶油味 碧根果225g/袋"}]
* sellerName : 商家1
* sellerid : 1
*/
private boolean checked;
private String sellerName;
private String sellerid;
private List<ListBean> list;
public String getSellerName() {
return sellerName;
}
public void setSellerName(String sellerName) {
this.sellerName = sellerName;
}
public String getSellerid() {
return sellerid;
}
public void setSellerid(String sellerid) {
this.sellerid = sellerid;
}
public List<ListBean> getList() {
return list;
}
public void setList(List<ListBean> list) {
this.list = list;
}
public static class ListBean {
public boolean isChecked() {
return checked;
}
public void setChecked(boolean checked) {
this.checked = checked;
}
/**
* bargainPrice : 22.9
* createtime : 2017-10-14T21:48:08
* detailUrl : https://item.m.jd.com/product/2542855.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends
* images : https://m.360buyimg.com/n0/jfs/t1930/284/2865629620/390243/e3ade9c4/56f0a08fNbd3a1235.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t2137/336/2802996626/155915/e5e90d7a/56f0a09cN33e01bd0.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t1882/31/2772215910/389956/c8dbf370/56f0a0a2Na0c86ea6.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t2620/166/2703833710/312660/531aa913/57709035N33857877.jpg!q70.jpg
* num : 2
* pid : 24
* price : 288
* pscid : 2
* selected : 0
* sellerid : 1
* subhead : 三只松鼠零食特惠,专区满99减50,满199减100,火速抢购》
* title : 三只松鼠 坚果炒货 零食奶油味 碧根果225g/袋
*/
private boolean checked;
private double bargainPrice;
private String createtime;
private String detailUrl;
private String images;
private int num;
private int pid;
private float price;
private int pscid;
private int selected;
private int sellerid;
private String subhead;
private String title;
public double getBargainPrice() {
return bargainPrice;
}
public void setBargainPrice(double bargainPrice) {
this.bargainPrice = bargainPrice;
}
public String getCreatetime() {
return createtime;
}
public void setCreatetime(String createtime) {
this.createtime = createtime;
}
public String getDetailUrl() {
return detailUrl;
}
public void setDetailUrl(String detailUrl) {
this.detailUrl = detailUrl;
}
public String getImages() {
return images;
}
public void setImages(String images) {
this.images = images;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public int getPid() {
return pid;
}
public void setPid(int pid) {
this.pid = pid;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public int getPscid() {
return pscid;
}
public void setPscid(int pscid) {
this.pscid = pscid;
}
public int getSelected() {
return selected;
}
public void setSelected(int selected) {
this.selected = selected;
}
public int getSellerid() {
return sellerid;
}
public void setSellerid(int sellerid) {
this.sellerid = sellerid;
}
public String getSubhead() {
return subhead;
}
public void setSubhead(String subhead) {
this.subhead = subhead;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
}
}
Elvpresenter
import android.content.Context;import java.util.HashMap;
import java.util.List;
import java.util.Map;
import model.ElvDataBean;
import view.CallBack;
import view.Iview;
/**
* Created by HASEE on 2017/11/22.
*/
public class Elvpresenter {
private Iview ivew;
public Elvpresenter(Iview ivew) {
this.ivew = ivew;
}
public void get(){
Map map = new HashMap<String,String>();
map.put("uid","100");
HttpUtils.getInstance().get("http://120.27.23.105/product/getCarts", map, new CallBack() {
@Override
public void onSuccess(Object o) {
ElvDataBean elvDataBean = (ElvDataBean) o;
ivew.Success(elvDataBean);
}
@Override
public void onFailed(Exception e) {
ivew.Failed(e);
}
},ElvDataBean.class);
}
public void onDetchView(){
if (ivew!=null){
ivew=null;
}
}
}
public interface Iview {
void Success(ElvDataBean elvDataBean);
void Failed(Exception e);
}
MessageEvent
public class MessageEvent {
private boolean checkd;
public boolean isCheckd(){
return checkd;
}
public void setCheckd(boolean checkd){
this.checkd=checkd;
}
}
MyElvadapter
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import com.bawei.test.okmvpreanview.R;
import com.bumptech.glide.Glide;
import org.greenrobot.eventbus.EventBus;
import java.util.ArrayList;
import java.util.List;
import model.ElvDataBean;
import static android.support.v7.widget.StaggeredGridLayoutManager.TAG;
/**
* Created by HASEE on 2017/11/22.
*/
public class MyElvadapter extends BaseExpandableListAdapter {
private static final String TAG = "ExlvAdapter";
private Context context;
private List<ElvDataBean.DataBean> groupList;
private List<List<ElvDataBean.DataBean.ListBean>> childList;
private boolean allGroupCbSelect;
public MyElvadapter(Context context, List<ElvDataBean.DataBean> groupList, List<List<ElvDataBean.DataBean.ListBean>> childList) {
this.context = context;
this.groupList = groupList;
this.childList = childList;
}
@Override
public int getGroupCount() {
return groupList.size();
}
@Override
public int getChildrenCount(int i) {
return childList.get(i).size();
}
@Override
public Object getGroup(int i) {
return groupList.get(i);
}
@Override
public Object getChild(int i, int i1) {
return childList.get(i).get(i1);
}
@Override
public long getGroupId(int i) {
return i;
}
@Override
public long getChildId(int i, int i1) {
return i1;
}
@Override
public boolean hasStableIds() {
return false;
}
//一级列表布局
@Override
public View getGroupView(final int i, boolean b, View view, ViewGroup viewGroup) {
final ElvDataBean.DataBean dataBean = groupList.get(i);
final ViewHolder holder;
if (view == null) {
holder = new ViewHolder();
view = View.inflate(context, R.layout.group, null);
holder.textView = (TextView) view.findViewById(R.id.g_title);
holder.che = (CheckBox) view.findViewById(R.id.che);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
holder.textView.setText(dataBean.getSellerName());
//设置一级列表checkBox的状态
holder.che.setChecked(dataBean.isChecked());
//一级列表checkBox的点击事件
holder.che.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//判断一级列表复选框的状态 设置为true或false
dataBean.setChecked(holder.che.isChecked());
//改变二级checkbod的状态
changeChildCbState(i, holder.che.isChecked());
//算钱
EventBus.getDefault().post(computer());
//改变全选状态 isAllGroupCbSelect判断一级是否全部选中
changeAllCbState(isAllGroupCbSelect());
//必刷新
notifyDataSetChanged();
}
});
return view;
}
//二级列表布局
@Override
public View getChildView(final int i, final int i1, boolean b, View view, ViewGroup viewGroup) {
final ElvDataBean.DataBean.ListBean listBean = childList.get(i).get(i1);
final ViewHolder2 holder2;
if (view == null) {
holder2 = new ViewHolder2();
view = View.inflate(context, R.layout.chidren, null);
holder2.z_title = (TextView) view.findViewById(R.id.z_title);
holder2.z_che = (CheckBox) view.findViewById(R.id.z_che);
holder2.img = (ImageView) view.findViewById(R.id.z_img);
holder2.price = view.findViewById(R.id.z_price);
holder2.xiangqing = view.findViewById(R.id.z_shuxing);
holder2.jian = view.findViewById(R.id.iv_jian);
holder2.jia = view.findViewById(R.id.iv_add);
holder2.del = view.findViewById(R.id.del);
holder2.num = view.findViewById(R.id.tv_num);
view.setTag(holder2);
} else {
holder2 = (ViewHolder2) view.getTag();
}
holder2.num.setText(listBean.getNum() + "");
holder2.z_title.setText(listBean.getTitle());
holder2.price.setText("¥" + listBean.getPrice());
holder2.xiangqing.setText(listBean.getSubhead());
//设置二级列表checkbox的属性
holder2.z_che.setChecked(listBean.isChecked());
String images = listBean.getImages();
String[] split = images.split("\\|");
Glide.with(context).load(split[0]).into(holder2.img);
//二级列表的点击事件
holder2.z_che.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//设置该条目中的checkbox属性值
listBean.setChecked(holder2.z_che.isChecked());
//计算价钱
PriceAndCountEvent priceAndCountEvent = computer();
EventBus.getDefault().post(priceAndCountEvent);
//判断当前checkbox是选中的状态
if (holder2.z_che.isChecked()) {
//如果全部选中(isAllChildCbSelected)
if (isAllChildCbSelected(i)) {
//改变一级列表的状态
changeGroupCbState(i, true);
//改变全选的状态
changeAllCbState(isAllGroupCbSelect());
}
} else {
//如果没有全部选中,一级列表的checkbox为false不为选中
changeGroupCbState(i, false);
changeAllCbState(isAllGroupCbSelect());
}
notifyDataSetChanged();
}
});
//加号
holder2.jia.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int num = listBean.getNum();
//num为int类型所以要加空字符串
holder2.num.setText(++num + "");
listBean.setNum(num);
//如果二级列表的checkbox为选中,计算价钱
if (holder2.z_che.isChecked()) {
PriceAndCountEvent priceAndCountEvent = computer();
EventBus.getDefault().post(priceAndCountEvent);
}
}
});
//减号
holder2.jian.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int num = listBean.getNum();
if (num == 1) {
return;
}
holder2.num.setText(--num + "");
listBean.setNum(num);
if (holder2.z_che.isChecked()) {
PriceAndCountEvent priceAndCountEvent = computer();
EventBus.getDefault().post(priceAndCountEvent);
}
}
});
//删除
holder2.del.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
List<ElvDataBean.DataBean.ListBean> listBeen = childList.get(i);
ElvDataBean.DataBean.ListBean remove = listBeen.remove(i1);
if (listBeen.size() == 0) {
//先移除二级列表的集合,再移除一级列表的集合
childList.remove(i);
groupList.remove(i);
}
//算钱
EventBus.getDefault().post(computer());
notifyDataSetChanged();
}
});
return view;
}
@Override
public boolean isChildSelectable(int i, int i1) {
return true;
}
class ViewHolder {
TextView textView;
CheckBox che;
}
class ViewHolder2 {
ImageView img;
TextView z_title;
CheckBox z_che;
TextView price;
TextView xiangqing;
ImageView jia;
ImageView jian;
ImageView del;
TextView num;
}
//改变二级列表的checkbox的状态 如果一级选中,控制二级也选中
private void changeChildCbState(int i, boolean flag) {
List<ElvDataBean.DataBean.ListBean> listBeen = childList.get(i);
for (int j = 0; j < listBeen.size(); j++) {
ElvDataBean.DataBean.ListBean listBean = listBeen.get(j);
listBean.setChecked(flag);
}
Log.i(TAG, "changeChildCbState: ");
}
//判断一级列表是否全部选中
public boolean isAllGroupCbSelect() {
for (int i = 0; i < groupList.size(); i++) {
ElvDataBean.DataBean dataBean = groupList.get(i);
if (!dataBean.isChecked()) {
return false;
}
}
return true;
}
//改变全选的状态
private void changeAllCbState(boolean flag) {
MessageEvent messageEvent = new MessageEvent();
messageEvent.setCheckd(flag);
EventBus.getDefault().post(messageEvent);
}
//改变一级列表的checkbox的状态
private void changeGroupCbState(int i, boolean flag) {
ElvDataBean.DataBean dataBean = groupList.get(i);
dataBean.setChecked(flag);
}
//判断二级列表是否全部选中
private boolean isAllChildCbSelected(int i) {
List<ElvDataBean.DataBean.ListBean> listBeen = childList.get(i);
for (int j = 0; j < listBeen.size(); j++) {
ElvDataBean.DataBean.ListBean listBean = listBeen.get(j);
if (!listBean.isChecked()) {
return false;
}
}
return true;
}
//设置全选,反选
public void changeAllListCbState(boolean flag) {
for (int i = 0; i < groupList.size(); i++) {
changeGroupCbState(i, flag);
changeChildCbState(i, flag);
}
//算钱
EventBus.getDefault().post(computer());
//
notifyDataSetChanged();
}
//计算列表的价钱
private PriceAndCountEvent computer() {
int count = 0;
int price = 0;
for (int i = 0; i < childList.size(); i++) {
List<ElvDataBean.DataBean.ListBean> listBeen = childList.get(i);
for (int j = 0; j < listBeen.size(); j++) {
ElvDataBean.DataBean.ListBean listBean = listBeen.get(j);
if (listBean.isChecked()) {
price += listBean.getNum() * listBean.getPrice();
count += listBean.getNum();
}
}
}
PriceAndCountEvent priceAndCountEvent = new PriceAndCountEvent();
priceAndCountEvent.setCount(count);
priceAndCountEvent.setPrice(price);
return priceAndCountEvent;
}
}
PriceAndCountEvent
public class PriceAndCountEvent {
private int price;
private int count;
public int getPrice() {
return price;
}
public int getCount() {
return count;
}
public void setPrice(int price) {
this.price = price;
}
public void setCount(int count) {
this.count = count;
}
}