购物车Bean类:
public class ShoopingCardBean { /** * msg : 请求成功 * code : 0 * data : [{"list":[{"bargainPrice":99,"createtime":"2017-10-14T21:38:26","detailUrl":"https://item.m.jd.com/product/4345173.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t6037/35/2944615848/95178/6cd6cff0/594a3a10Na4ec7f39.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t6607/258/1025744923/75738/da120a2d/594a3a12Ne3e6bc56.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t6370/292/1057025420/64655/f87644e3/594a3a12N5b900606.jpg!q70.jpg","num":1,"pid":45,"price":2999,"pscid":39,"selected":0,"sellerid":1,"subhead":"高清双摄,就是清晰!2000+1600万高清摄像头,6GB大内存+高通骁龙835处理器,性能怪兽!","title":"一加手机5 (A5000) 6GB+64GB 月岩灰 全网通 双卡双待 移动联通电信4G手机"}],"sellerName":"商家1","sellerid":"1"},{"list":[{"bargainPrice":2999,"createtime":"2017-10-14T21:48:08","detailUrl":"https://item.m.jd.com/product/2385655.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t2068/298/2448145915/157953/7be197df/56d51a42Nd86f1c8e.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t2437/128/1687178395/117431/bcc190c1/56d3fcbaNb2963d21.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t2467/222/2263160610/95597/927b8a2f/56d3eafeNdecebeb6.jpg!q70.jpg","num":1,"pid":53,"price":777,"pscid":39,"selected":0,"sellerid":9,"subhead":"Super AMOLED三星双曲面2K 屏,支持无线充电!","title":"三星 Galaxy S7 edge(G9350)4GB+32GB 铂光金 移动联通电信4G手机 双卡双待"}],"sellerName":"商家9","sellerid":"9"}] */ 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 { /** * list : [{"bargainPrice":99,"createtime":"2017-10-14T21:38:26","detailUrl":"https://item.m.jd.com/product/4345173.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t6037/35/2944615848/95178/6cd6cff0/594a3a10Na4ec7f39.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t6607/258/1025744923/75738/da120a2d/594a3a12Ne3e6bc56.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t6370/292/1057025420/64655/f87644e3/594a3a12N5b900606.jpg!q70.jpg","num":1,"pid":45,"price":2999,"pscid":39,"selected":0,"sellerid":1,"subhead":"高清双摄,就是清晰!2000+1600万高清摄像头,6GB大内存+高通骁龙835处理器,性能怪兽!","title":"一加手机5 (A5000) 6GB+64GB 月岩灰 全网通 双卡双待 移动联通电信4G手机"}] * sellerName : 商家1 * sellerid : 1 */ private boolean groupchecked; private String sellerName; private String sellerid; private List<ListBean> list; public boolean isGroupchecked() { return groupchecked; } public void setGroupchecked(boolean groupchecked) { this.groupchecked = groupchecked; } 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 { /** * bargainPrice : 99.0 * createtime : 2017-10-14T21:38:26 * detailUrl : https://item.m.jd.com/product/4345173.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term=QQfriends * images : https://m.360buyimg.com/n0/jfs/t6037/35/2944615848/95178/6cd6cff0/594a3a10Na4ec7f39.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t6607/258/1025744923/75738/da120a2d/594a3a12Ne3e6bc56.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t6370/292/1057025420/64655/f87644e3/594a3a12N5b900606.jpg!q70.jpg * num : 1 * pid : 45 * price : 2999.0 * pscid : 39 * selected : 0 * sellerid : 1 * subhead : 高清双摄,就是清晰!2000+1600万高清摄像头,6GB大内存+高通骁龙835处理器,性能怪兽! * title : 一加手机5 (A5000) 6GB+64GB 月岩灰 全网通 双卡双待 移动联通电信4G手机 */ private boolean childchecked; private double bargainPrice; private String createtime; private String detailUrl; private String images; private int num; private int pid; private double price; private int pscid; private int selected; private int sellerid; private String subhead; private String title; public boolean isChildchecked() { return childchecked; } public void setChildchecked(boolean childchecked) { this.childchecked = childchecked; } 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 double getPrice() { return price; } public void setPrice(double 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; } } } }
一级列表布局:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <CheckBox android:id="@+id/groupsataus" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>二级列表布局:<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:fresco="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_vertical" android:orientation="horizontal"> <CheckBox android:id="@+id/childsataus" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" /> <com.facebook.drawee.view.SimpleDraweeView android:id="@+id/img" android:layout_width="100dp" android:layout_height="150dp" fresco:placeholderImage="@mipmap/ic_launcher_round" /> <LinearLayout android:layout_width="match_parent" android:layout_height="150dp" android:orientation="vertical"> <TextView android:id="@+id/tielss" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:ellipsize="end" android:lines="2" android:text="爱的撒旦说道说道说道说道是多少的是多少的说道说道" android:textColor="#000000" android:textSize="16sp" /> <TextView android:id="@+id/miaoshu" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:ellipsize="end" android:lines="2" android:text="爱的撒旦说道说道说道说道是多少的是多少的说道说道" /> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp"> <TextView android:id="@+id/newprice" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:gravity="center_vertical" android:text="$111" android:textColor="#FFFF6F00" android:textSize="14sp" /> <TextView android:id="@+id/oldprice" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="15dp" android:layout_toRightOf="@id/newprice" android:gravity="center_vertical" android:text="$111" android:textSize="12sp" /> <TextView android:id="@+id/jiajia" android:layout_width="50dp" android:layout_height="60dp" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginRight="1dp" android:background="#FFCECACA" android:gravity="center" android:text="+" android:textColor="#ffffff" android:textSize="25sp" /> <TextView android:id="@+id/num" android:layout_width="50dp" android:layout_height="60dp" android:layout_centerVertical="true" android:layout_marginRight="2dp" android:layout_toLeftOf="@id/jiajia" android:background="#FFCECACA" android:gravity="center" android:text="59" android:textColor="#ffffff" android:textSize="20sp" /> <TextView android:id="@+id/jianjian" android:layout_width="50dp" android:layout_height="60dp" android:layout_centerVertical="true" android:layout_marginRight="2dp" android:layout_toLeftOf="@id/num" android:background="#FFCECACA" android:gravity="center" android:text="—" android:textColor="#ffffff" android:textSize="25sp" /> </RelativeLayout> </LinearLayout> </LinearLayout>二级适配器:public class CardAdapter extends BaseExpandableListAdapter { private List<ShoopingCardBean.DataBean> grouplist; private List<List<ShoopingCardBean.DataBean.ListBean>> childlist; private Context context; private double price; public CardAdapter(List<ShoopingCardBean.DataBean> grouplist, List<List<ShoopingCardBean.DataBean.ListBean>> childlist, Context context) { this.grouplist = grouplist; this.childlist = childlist; this.context = context; //初始化Fresco Fresco.initialize(this.context); } @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) { View groupview; final ViewHolder1 viewHolder1; if (view == null) { viewHolder1 = new ViewHolder1(); groupview = View.inflate(context, R.layout.group, null); viewHolder1.groupchecked = groupview.findViewById(R.id.groupsataus); groupview.setTag(viewHolder1); } else { groupview = view; viewHolder1 = (ViewHolder1) groupview.getTag(); } viewHolder1.groupchecked.setChecked(grouplist.get(i).isGroupchecked()); viewHolder1.groupchecked.setText(grouplist.get(i).getSellerName()); //父项点击对子项进行全选/反选 viewHolder1.groupchecked.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { isgroupAllchild(viewHolder1.groupchecked.isChecked(), i); grouplist.get(i).setGroupchecked(viewHolder1.groupchecked.isChecked()); notifyDataSetChanged(); //判断父项是否全部选中 boolean groupAll = isGroupAll(); if (groupAll) { EventBus.getDefault().post(new Status(true)); } else { EventBus.getDefault().post(new Status(false)); } //计算价钱 getprice(); } }); return groupview; } @Override public View getChildView(final int i, final int i1, boolean b, View view, ViewGroup viewGroup) { View childview; final ViewHolder2 viewHolder2; if (view == null) { viewHolder2 = new ViewHolder2(); childview = View.inflate(context, R.layout.child, null); viewHolder2.childchecked = childview.findViewById(R.id.childsataus); viewHolder2.img = childview.findViewById(R.id.img); viewHolder2.tiltess = childview.findViewById(R.id.tielss); viewHolder2.miaoshu = childview.findViewById(R.id.miaoshu); viewHolder2.newprice = childview.findViewById(R.id.newprice); viewHolder2.oldprice = childview.findViewById(R.id.oldprice); viewHolder2.jianjian = childview.findViewById(R.id.jianjian); viewHolder2.num = childview.findViewById(R.id.num); viewHolder2.jiajia = childview.findViewById(R.id.jiajia); childview.setTag(viewHolder2); } else { childview = view; viewHolder2 = (ViewHolder2) childview.getTag(); } viewHolder2.childchecked.setChecked(childlist.get(i).get(i1).isChildchecked()); viewHolder2.img.setImageURI(Uri.parse(childlist.get(i).get(i1).getImages().split("!")[0])); viewHolder2.tiltess.setText(childlist.get(i).get(i1).getTitle()); viewHolder2.miaoshu.setText(childlist.get(i).get(i1).getSubhead()); viewHolder2.newprice.setText("¥" + childlist.get(i).get(i1).getBargainPrice() + ""); //加下划线 viewHolder2.oldprice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG); viewHolder2.oldprice.setText("¥" + childlist.get(i).get(i1).getPrice()); viewHolder2.num.setText(childlist.get(i).get(i1).getNum() + ""); //子项全选中,选中父项,否则选中自己 viewHolder2.childchecked.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (viewHolder2.childchecked.isChecked()) { childlist.get(i).get(i1).setChildchecked(viewHolder2.childchecked.isChecked()); boolean ischiledAll = ischiledAllchecked(i); if (ischiledAll) { grouplist.get(i).setGroupchecked(viewHolder2.childchecked.isChecked()); } notifyDataSetChanged(); //判断父项是否全部选中 boolean groupAll = isGroupAll(); if (groupAll) { EventBus.getDefault().post(new Status(true)); } //计算价钱 getprice(); } else { childlist.get(i).get(i1).setChildchecked(viewHolder2.childchecked.isChecked()); boolean ischiledAll = ischiledAllchecked(i); if (!ischiledAll) { grouplist.get(i).setGroupchecked(viewHolder2.childchecked.isChecked()); } notifyDataSetChanged(); //判断父项是否全部选中 boolean groupAll = isGroupAll(); if (!groupAll) { EventBus.getDefault().post(new Status(false)); } //计算价钱 getprice(); } } }); //数量加加 viewHolder2.jiajia.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { int num = childlist.get(i).get(i1).getNum(); childlist.get(i).get(i1).setNum(num += 1); notifyDataSetChanged(); //计算价钱 getprice(); } }); //数量减减 viewHolder2.jianjian.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { int num = childlist.get(i).get(i1).getNum(); if (num > 1) { childlist.get(i).get(i1).setNum(num -= 1); notifyDataSetChanged(); } else { Toast.makeText(context, "数量最低为1", Toast.LENGTH_SHORT).show(); } //计算价钱 getprice(); } }); return childview; } @Override public boolean isChildSelectable(int i, int i1) { return true; } //优化类 class ViewHolder1 { CheckBox groupchecked; } class ViewHolder2 { CheckBox childchecked; SimpleDraweeView img; TextView tiltess; TextView miaoshu; TextView newprice; TextView oldprice; TextView jianjian; TextView num; TextView jiajia; } //子项全选中,选中父项,否则选中自己 private boolean ischiledAllchecked(int postion) { List<ShoopingCardBean.DataBean.ListBean> listBeen = childlist.get(postion); for (int i = 0; i < listBeen.size(); i++) { if (!listBeen.get(i).isChildchecked()) { return false; } } return true; } //父项点击对子项进行全选/反选 private void isgroupAllchild(boolean b, int postion) { List<ShoopingCardBean.DataBean.ListBean> listBeen = childlist.get(postion); for (int i = 0; i < listBeen.size(); i++) { listBeen.get(i).setChildchecked(b); } } //判断父项是否全部选中 private boolean isGroupAll() { for (int i = 0; i < grouplist.size(); i++) { if (!grouplist.get(i).isGroupchecked()) { return false; } } return true; } //全选/反选 public void All(boolean b) { for (int i = 0; i < grouplist.size(); i++) { grouplist.get(i).setGroupchecked(b); } for (int i = 0; i < childlist.size(); i++) { for (int j = 0; j < childlist.get(i).size(); j++) { childlist.get(i).get(j).setChildchecked(b); } } //刷新适配器 notifyDataSetChanged(); //计算价钱 getprice(); } //计算价钱 private void getprice() { price = 0; for (int i = 0; i < childlist.size(); i++) { for (int j = 0; j < childlist.get(i).size(); j++) { if (childlist.get(i).get(j).isChildchecked()) { price += childlist.get(i).get(j).getNum() * childlist.get(i).get(j).getBargainPrice(); } } } EventBus.getDefault().post(new Money(price)); } //删除判断是否有选中的商品 public boolean getdele() { for (int i = 0; i < childlist.size(); i++) { for (int j = 0; j < childlist.get(i).size(); j++) { if (childlist.get(i).get(j).isChildchecked()) { return true; } } } return false; } //发送要删除的商品Pid public void sendDelPid() { for (int i = 0; i < childlist.size(); i++) { for (int j = 0; j < childlist.get(i).size(); j++) { if (childlist.get(i).get(j).isChildchecked()) { EventBus.getDefault().post(new DelPid(childlist.get(i).get(j).getPid() + "")); } } } } }Activity:public class ShoopingCardActivity extends AppCompatActivity implements GetCardView { @Bind(R.id.erjiliebiao) ExpandableListView erjiliebiao; @Bind(R.id.all) CheckBox all; @Bind(R.id.conutprice) TextView conutprice; @Bind(R.id.sale) TextView sale; @Bind(R.id.del) TextView del; private Passer<GetCardView> getCardViewPasser; private List<List<ShoopingCardBean.DataBean.ListBean>> childlist; private CardAdapter cardAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_shooping_card); ButterKnife.bind(this); //订阅EventBus EventBus.getDefault().register(this); //请除网络数据 getCardViewPasser = new Passer<GetCardView>(this); getCardViewPasser.getCard("android", "11782"); } //得到购物车结果 @Override public void getCard(ShoopingCardBean shoopingCardBean) { if (shoopingCardBean.getCode().equals("0")) { Toast.makeText(this, shoopingCardBean.getMsg(), Toast.LENGTH_SHORT).show(); List<ShoopingCardBean.DataBean> data = shoopingCardBean.getData(); childlist = new ArrayList<>(); for (int i = 0; i < data.size(); i++) { childlist.add(data.get(i).getList()); } //构建适配器 cardAdapter = new CardAdapter(data, childlist, ShoopingCardActivity.this); erjiliebiao.setAdapter(cardAdapter); erjiliebiao.setGroupIndicator(null); for (int i = 0; i < data.size(); i++) { erjiliebiao.expandGroup(i); } } else { Toast.makeText(this, shoopingCardBean.getMsg(), Toast.LENGTH_SHORT).show(); } } //Eventbus接收全选状态 @Subscribe() public void getSatus(Status status) { all.setChecked(status.ison()); } //Eventbus接收总价钱 @Subscribe() public void getPrice(Money money) { DecimalFormat df = new DecimalFormat("######0.00"); String format = df.format(money.getPrice()); conutprice.setText(format); } //接收要删除的商品pid进行删除 @Subscribe() public void delGoods(DelPid delPid) { if (getCardViewPasser != null) { getCardViewPasser.delGoods("android", "11782", delPid.getPid()); } } //解除绑定 @Override protected void onDestroy() { super.onDestroy(); //取消订阅EventBus EventBus.getDefault().unregister(this); if (getCardViewPasser != null) { getCardViewPasser.destroy(); } } //点击事件 @OnClick({R.id.all, R.id.sale, R.id.del}) public void onViewClicked(View view) { switch (view.getId()) { //全选/反选 case R.id.all: cardAdapter.All(all.isChecked()); break; //点击购买 case R.id.sale: if (Double.parseDouble(conutprice.getText() + "") > 0) { Toast.makeText(this, "正在生成订单", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(this, "请选购商品", Toast.LENGTH_SHORT).show(); } break; //点击删除商品 case R.id.del: boolean getdele = cardAdapter.getdele(); if (!getdele) { Toast.makeText(this, "请选购要删除商品", Toast.LENGTH_SHORT).show(); } else { cardAdapter.sendDelPid(); } break; } } @Override public void dellGoods(DelBean delBean) { if (delBean.getCode().equals("0")) { Toast.makeText(this, delBean.getMsg(), Toast.LENGTH_SHORT).show(); if (getCardViewPasser != null) { getCardViewPasser.getCard("android", "11782"); } } else { Toast.makeText(this, delBean.getMsg(), Toast.LENGTH_SHORT).show(); } } }