依赖
compile 'com.squareup.okhttp3:okhttp:3.9.0' compile 'com.google.code.gson:gson:2.8.2' compile 'com.github.bumptech.glide:glide:3.6.1' compile 'com.squareup.okhttp3:okhttp:3.2.0'
权限
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
activity_main的布局
xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <include layout="@layout/layout_title" /> <ExpandableListView android:id="@+id/expandList" android:layout_weight="1" android:layout_width="match_parent" android:layout_height="match_parent"> ExpandableListView> <View android:layout_width="match_parent" android:layout_height="0.5dp" android:layout_marginTop="2dp" android:background="#000000"/> <include layout="@layout/layout_bottom" /> LinearLayout>
layout_bottom的布局
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="wrap_content" android:orientation="horizontal"> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="全选" android:textSize="25sp" android:padding="10dp" android:id="@+id/btnCheckAll"/> <TextView android:id="@+id/tvTotalPrice" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18sp" android:padding="10dp" android:textColor="@android:color/black" android:layout_weight="1" android:gravity="center" android:text="合计:¥0.00"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="结算(0)" android:textColor="@android:color/white" android:textSize="25sp" android:padding="10dp" android:background="@android:color/holo_red_light" android:id="@+id/btnAmount"/> LinearLayout>
layout_child_item的布局
xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <CheckBox android:id="@+id/ck_child_choose" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="5dp" android:scaleX="0.6" android:scaleY="0.6" /> <ImageView android:id="@+id/iv_show_pic" android:layout_width="70dp" android:layout_height="80dp" android:layout_centerVertical="true" android:layout_marginLeft="5dp" android:src="@mipmap/ic_launcher" android:layout_toRightOf="@id/ck_child_choose" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_marginTop="15dp" android:layout_toRightOf="@id/iv_show_pic" android:orientation="vertical"> <TextView android:id="@+id/tv_commodity_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="酒红色纯红色纯羊毛西服套装" android:textColor="@android:color/black" android:textSize="12sp" android:textStyle="bold" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:id="@+id/tv_commodity_attr" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="3dp" android:text="属性:粉蓝色" android:textSize="12sp" android:textColor="@color/colorPrimary" /> LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="4dp" android:orientation="horizontal"> <TextView android:id="@+id/tv_commodity_price" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="¥390" android:textColor="@android:color/holo_red_dark" android:textSize="12sp" android:textStyle="bold" /> <TextView android:id="@+id/tv_commodity_num" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:text="x1" android:textColor="@android:color/darker_gray" /> <LinearLayout android:id="@+id/rl_edit" android:layout_width="120dp" android:background="@android:color/holo_orange_light" android:layout_height="30dp" android:layout_marginLeft="20dp" > <TextView android:id="@+id/iv_sub" android:layout_width="0dp" android:layout_weight="1" android:gravity="center" android:textColor="@android:color/black" android:background="@android:color/white" android:layout_margin="1dp" android:layout_height="match_parent" android:text=" - " /> <TextView android:id="@+id/tv_commodity_show_num" android:layout_width="0dp" android:layout_weight="1" android:gravity="center" android:background="@android:color/white" android:layout_margin="1dp" android:layout_height="match_parent" android:text="1" /> <TextView android:id="@+id/iv_add" android:layout_width="0dp" android:layout_weight="1" android:gravity="center" android:layout_margin="1dp" android:background="@android:color/white" android:layout_height="match_parent"
RelativeLayout>
layout_group_item的布局
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/ck_group_choosed" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="商家1" android:gravity="center_vertical" android:textSize="25sp" android:focusable="false" android:padding="10dp"/> LinearLayout>layout_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="wrap_content" android:background="#f40505" android:orientation="horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="返回" android:textSize="25sp" android:padding="10dp" android:textColor="@android:color/white" android:id="@+id/btnBack"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="25sp" android:padding="10dp" android:textColor="@android:color/white" android:layout_weight="1" android:gravity="center" android:text="购物车"/> <TextView android:id="@+id/btnEditor" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="编辑" android:textColor="@android:color/white" android:textSize="25sp" android:padding="10dp"/> LinearLayout>
创建 bean view model persenter utils 包bean 创建
ShoppCarBean
public class ShoppCarBean { /** * 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":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":3,"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":1599,"createtime":"2017-10-14T21:48:08","detailUrl":"https://item.m.jd.com/product/1993026402.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t5863/302/8961270302/97126/41feade1/5981c81cNc1b1fbef.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7003/250/1488538438/195825/53bf31ba/5981c57eN51e95176.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5665/100/8954482513/43454/418611a9/5981c57eNd5fc97ba.jpg!q70.jpg","num":1,"pid":47,"price":111,"pscid":39,"selected":0,"sellerid":3,"subhead":"碳黑色 32GB 全网通 官方标配 1件","title":"锤子 坚果Pro 特别版 巧克力色 酒红色 全网通 移动联通电信4G手机 双卡双待 碳黑色 32GB 全网通"}],"sellerName":"商家3","sellerid":"3"}] */ private String msg; private String code; private Listdata; 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 getData() { return data; } public void setData(List 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 String sellerName; private String sellerid; private List list; //商家是否被选中 组条目是否被选中 private boolean isGroupChoosed; public boolean isGroupChoosed() { return isGroupChoosed; } public void setGroupChoosed(boolean groupChoosed) { isGroupChoosed = groupChoosed; } //截止到上面 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 getList() { return list; } public void setList(List 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 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; //子条目商品是否被选中的一个字段状态 private boolean isChildChoosed; public boolean isChildChoosed() { return isChildChoosed; } public void setChildChoosed(boolean childChoosed) { isChildChoosed = childChoosed; } //截止到上面 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; } } } }
model 创建 接口
IShoppCarModel
public interface IShoppCarModel { // IShooppPrensenter iShooppPrensenter是P层的接口 void getCarInfo(String uid, IShooppPrensenter iShooppPrensenter); }
ShoppCarModel 层
public class ShoppCarModel implements IShoppCarModel{ @Override public void getCarInfo(String uid, final IShooppPrensenter iShooppPrensenter) { HashMapparams = new HashMap<>(); params.put("uid",uid); OkHttpUtils instance = OkHttpUtils.getInstance(); instance.doGet(Constants.GET_CARTS, params, new OnFinishListener() { @Override public void onFailed(String msg) { iShooppPrensenter.onFailed(msg); } @Override public void onSuccess(Object obj) { Gson gson = new Gson(); ShoppCarBean shoppCarBean = gson.fromJson(obj.toString(), ShoppCarBean.class); iShooppPrensenter.onSuccess(shoppCarBean.getData()); } }); } }
persenter 层 接口
IShooppPrensenter
public interface IShooppPrensenter { void onSuccess(Listdata); //是bean里面的List集合 void onFailed(String msg); void onDestory(); void getCarInfo(String uid); }
ShooppPresenter 层
public class ShooppPresenter implements IShooppPrensenter { //需要model层的接口 private IShoppCarModel iShoppCarModel; private IMainView iMainView; //需要从P层传到V层 public ShooppPresenter(MainActivity iMainView) { this.iMainView = iMainView; //需要model层的接口 iShoppCarModel = new ShoppCarModel(); } @Override public void getCarInfo(String uid) { iShoppCarModel.getCarInfo(uid, this); } @Override public void onSuccess(Listdata) { if (iMainView != null) { iMainView.onSuccess(data); } } @Override public void onFailed(String msg) { if (iMainView != null) { iMainView.onFailed(msg); } } @Override public void onDestory() { if (iMainView != null) { iMainView = null; } } }
view 创建 acativity adapter 包
acativity 接口
IMainView
public interface IMainView { void onSuccess(Listdata); void onFailed(String msg); }
MainActivity
public class MainActivity extends AppCompatActivity implements IMainView, MyExpandAdapter.ModifyGoodsItemNumberListener, MyExpandAdapter.CheckGroupItemListener { private ExpandableListView expandList; private CheckBox btnCheckAll; private TextView tvTotalPrice; private TextView btnAmount; private TextView btnEditor; private MyExpandAdapter adapter; private IShooppPrensenter iShooppPrensenter; private Listapadter 创建list; //购买商品的总数量 private int totalNum = 0; //购买商品的总价 private double totalPrice = 0.00; private boolean flag; public MainActivity() { iShooppPrensenter = new ShooppPresenter(this); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //隐藏actionbar getSupportActionBar().hide(); expandList = (ExpandableListView) findViewById(R.id.expandList); //购物车底部栏的全选框 btnCheckAll = (CheckBox) findViewById(R.id.btnCheckAll); //合计,计算总价 tvTotalPrice = (TextView) findViewById(R.id.tvTotalPrice); //结算按钮 btnAmount = (TextView) findViewById(R.id.btnAmount); //找到编辑控件 btnEditor = (TextView) findViewById(R.id.btnEditor); //去除默认指示器 expandList.setGroupIndicator(null); //设置适配器 adapter = new MyExpandAdapter(this); expandList.setAdapter(adapter); //获取购物车信息 iShooppPrensenter.getCarInfo("10192"); //设置商品加减的监听 adapter.setModifyGoodsItemNumberListener(this); //设置商家以及商品是否被选中的监听 adapter.setCheckGroupItemListener(this); //全选的点击 btnCheckAll.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //isChoosedAll 在下面定义的 全选与反选 isChoosedAll(((CheckBox) view).isChecked()); //计算商品总价 //statisticsPrice 在下面定义的计算你所选中的商品总价 statisticsPrice(); } }); //编辑 /*btnEditor.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if(!flag){//编辑 -> 完成\ flag = true; btnEditor.setText("完成"); adapter.showDeleteButton(flag); }else{ flag = false; btnEditor.setText("编辑"); adapter.showDeleteButton(flag); } } });*/ } //二级列表默认展开 private void defaultExpand() { for (int i = 0; i < adapter.getGroupCount(); ++i) { expandList.expandGroup(i); } } @Override public void onSuccess(List data) { this.list = data; //设置数据 adapter.setList(list); defaultExpand(); } @Override public void onFailed(String msg) { } @Override public void checkGroupItem(int groupPosition, boolean isChecked) { //Toast.makeText(this, "商家", Toast.LENGTH_SHORT).show(); //商家javabean ShoppCarBean.DataBean dataBean = list.get(groupPosition); dataBean.setGroupChoosed(isChecked); //遍历商家里面的商品,将其置为选中状态 for (ShoppCarBean.DataBean.ListBean listBean : dataBean.getList()) { listBean.setChildChoosed(isChecked); } //底部结算那个checkbox状态(全选) // isCheckAll 是在下面定义的 购物车商品是否全部选中 if (isCheckAll()) { btnCheckAll.setChecked(true); } else { btnCheckAll.setChecked(false); } //刷新适配器 adapter.notifyDataSetChanged(); //计算价格 statisticsPrice(); } @Override public void checkChildItem(int groupPosition, int childPosition, boolean isChecked) { //Toast.makeText(this, "商品", Toast.LENGTH_SHORT).show(); //ShoppCarBean.DataBean.ListBean listBean = list.get(groupPosition).getList().get(childPosition); ShoppCarBean.DataBean dataBean = list.get(groupPosition);//商家那一层 List listBeans = dataBean.getList(); ShoppCarBean.DataBean.ListBean listBean = listBeans.get(childPosition); //你点击商家的商品条目将其选中状态记录 listBean.setChildChoosed(isChecked); //检测商家里面的每一个商品是否被选中,如果被选中,返回boolean // isGoodsCheckAll 是在下面检测某个商家的商品是否都选中,如果都选中的话,商家CheckBox应该是选中状态 boolean result = isGoodsCheckAll(groupPosition); if (result) { dataBean.setGroupChoosed(result); } else { dataBean.setGroupChoosed(result); } //底部结算那个checkbox状态(全选) if (isCheckAll()) { btnCheckAll.setChecked(true); } else { btnCheckAll.setChecked(false); } //刷新适配器 adapter.notifyDataSetChanged(); //计算总价 statisticsPrice(); } @Override public void doIncrease(int groupPosition, int childPosition, View view) { //Toast.makeText(this, "用户点击了商品减少操作", Toast.LENGTH_SHORT).show(); ShoppCarBean.DataBean.ListBean listBean = list.get(groupPosition).getList().get(childPosition); //取出当前的商品数量 int currentNum = listBean.getNum(); currentNum++; listBean.setNum(currentNum); //刷新适配器 adapter.notifyDataSetChanged(); //计算商品价格 statisticsPrice(); } @Override public void doDecrease(int groupPosition, int childPosition, View view) { //Toast.makeText(this, "用户点击了商品减少操作", Toast.LENGTH_SHORT).show(); ShoppCarBean.DataBean.ListBean listBean = list.get(groupPosition).getList().get(childPosition); //取出当前的商品数量 int currentNum = listBean.getNum(); //直接结束这个方法 if (currentNum == 1) { return; } currentNum--; listBean.setNum(currentNum); //刷新适配器 adapter.notifyDataSetChanged(); //计算商品价格 statisticsPrice(); } //全选与反选 private void isChoosedAll(boolean isChecked) { for (ShoppCarBean.DataBean dataBean : list) { dataBean.setGroupChoosed(isChecked); for (ShoppCarBean.DataBean.ListBean listBean : dataBean.getList()) { listBean.setChildChoosed(isChecked); } } //刷新适配器 adapter.notifyDataSetChanged(); } /** * 计算你所选中的商品总价 */ private void statisticsPrice() { //初始化值 totalNum = 0; totalPrice = 0.00; for (ShoppCarBean.DataBean dataBean : list) { for (ShoppCarBean.DataBean.ListBean listBean : dataBean.getList()) { if (listBean.isChildChoosed()) { totalNum++; System.out.println("number : " + totalNum); totalPrice += listBean.getNum() * listBean.getPrice(); } } } //设置文本信息 合计、结算的商品数量 tvTotalPrice.setText("合计:¥" + totalPrice); btnAmount.setText("结算(" + totalNum + ")"); } //购物车商品是否全部选中 private boolean isCheckAll() { for (ShoppCarBean.DataBean dataBean : list) { if (!dataBean.isGroupChoosed()) { return false; } } return true; } //检测某个商家的商品是否都选中,如果都选中的话,商家CheckBox应该是选中状态 private boolean isGoodsCheckAll(int groupPosition) { List listBeans = this.list.get(groupPosition).getList(); //遍历某一个商家的每个商品是否被选中 for (ShoppCarBean.DataBean.ListBean listBean : listBeans) { if (listBean.isChildChoosed()) {//是选中状态 continue; } else { return false; } } return true; } }
MyExpandAdapter
public class MyExpandAdapter extends BaseExpandableListAdapter { private Listdata; private Context context; //商品加减的一个接口变量 是在下面定义的接口 private ModifyGoodsItemNumberListener modifyGoodsItemNumberListener; private CheckGroupItemListener checkGroupItemListener; //接收是否处于编辑状态的一个boolean值 private boolean isEditor; public MyExpandAdapter(Context context) { this.context = context; } //设置数据 public void setList(List data) { this.data = data; notifyDataSetChanged(); } //商家以及商品是否被选中的一个监听 public void setCheckGroupItemListener(CheckGroupItemListener checkGroupItemListener) { this.checkGroupItemListener = checkGroupItemListener; } //设置商品的加减监听 public void setModifyGoodsItemNumberListener(ModifyGoodsItemNumberListener modifyGoodsItemNumberListener) { this.modifyGoodsItemNumberListener = modifyGoodsItemNumberListener; } //是否显示删除按钮 public void showDeleteButton(boolean isEditor) { this.isEditor = isEditor; //刷新适配器 notifyDataSetChanged(); } @Override public int getGroupCount() { return data != null ? data.size() : 0; } @Override public int getChildrenCount(int groupPosition) { return data != null && data.get(groupPosition).getList() != null ? data.get(groupPosition).getList().size() : 0; } @Override public Object getGroup(int groupPosition) { return data.get(groupPosition); } @Override public Object getChild(int groupPosition, int childPosition) { return data.get(groupPosition).getList().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) { if (convertView == null) { convertView = LayoutInflater.from(context).inflate(R.layout.layout_group_item, parent, false); } CheckBox ck_group_choosed = convertView.findViewById(R.id.ck_group_choosed); //isGroupChoosed 需要在bean定义 if (data.get(groupPosition).isGroupChoosed()) { ck_group_choosed.setChecked(true); } else { ck_group_choosed.setChecked(false); } ck_group_choosed.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { checkGroupItemListener.checkGroupItem(groupPosition, ((CheckBox) v).isChecked()); } }); //getSellerName 需要在bean定义 ck_group_choosed.setText(data.get(groupPosition).getSellerName()); return convertView; } @Override public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { if (convertView == null) { convertView = LayoutInflater.from(context).inflate(R.layout.layout_child_item, parent, false); } //商品选择 CheckBox ck_child_choosed = convertView.findViewById(R.id.ck_child_choose); //商品图片 ImageView iv_show_pic = convertView.findViewById(R.id.iv_show_pic); //商品主标题 TextView tv_commodity_name = convertView.findViewById(R.id.tv_commodity_name); //商品副标题 TextView tv_commodity_attr = convertView.findViewById(R.id.tv_commodity_attr); //商品价格 TextView tv_commodity_price = convertView.findViewById(R.id.tv_commodity_price); //商品数量 TextView tv_commodity_num = convertView.findViewById(R.id.tv_commodity_num); //商品减 TextView iv_sub = convertView.findViewById(R.id.iv_sub); //商品加减中的数量变化 final TextView tv_commodity_show_num = convertView.findViewById(R.id.tv_commodity_show_num); //商品加 TextView iv_add = convertView.findViewById(R.id.iv_add); //删除按钮 Button btn_commodity_delete = convertView.findViewById(R.id.btn_commodity_delete); //设置文本信息 tv_commodity_name.setText(data.get(groupPosition).getList().get(childPosition).getTitle()); tv_commodity_attr.setText(data.get(groupPosition).getList().get(childPosition).getSubhead()); tv_commodity_price.setText("¥" + data.get(groupPosition).getList().get(childPosition).getPrice()); tv_commodity_num.setText("x" + data.get(groupPosition).getList().get(childPosition).getNum()); tv_commodity_show_num.setText(data.get(groupPosition).getList().get(childPosition).getNum() + ""); //分割图片地址 String images = data.get(groupPosition).getList().get(childPosition).getImages(); String[] urls = images.split("\\|"); //加载商品图片 Glide.with(context) .load(urls[0]) .crossFade() .into(iv_show_pic); //商品加 iv_add.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { modifyGoodsItemNumberListener.doIncrease(groupPosition, childPosition, tv_commodity_show_num); } }); //设置商品加减的按钮 //商品减 iv_sub.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { modifyGoodsItemNumberListener.doDecrease(groupPosition, childPosition, tv_commodity_show_num); } }); //商品复选框是否被选中 ck_child_choosed.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { checkGroupItemListener.checkChildItem(groupPosition, childPosition, ((CheckBox) v).isChecked()); } }); //处理商品的选中状态 //isChildChoosed 需要在bean定义 if (data.get(groupPosition).getList().get(childPosition).isChildChoosed()) { ck_child_choosed.setChecked(true); } else { ck_child_choosed.setChecked(false); } //控制删除按钮的隐藏与显示 /*if(isEditor){ btn_commodity_delete.setVisibility(View.VISIBLE); }else{ btn_commodity_delete.setVisibility(View.GONE); }*/ return convertView; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return false; } /** * 商家的复选框以及商品的复选框是否被选中的接口 */ /** * 商家复选框以及商品复选框是否被选中接口 */ public interface CheckGroupItemListener { //商家的条目的复选框监听 void checkGroupItem(int groupPosition, boolean isChecked); //商品的 void checkChildItem(int groupPosition, int childPosition, boolean isChecked); } /** * 商品加减接口 */ public interface ModifyGoodsItemNumberListener { //商品添加操作 void doIncrease(int groupPosition, int childPosition, View view); //商品减少操作 void doDecrease(int groupPosition, int childPosition, View view); } }
清单文件
activity android:name="view.acativity.MainActivity"