drawable
bian_kuang.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="#ffffff"/> <stroke android:color="#000000" android:width="0.1dp"/> </shape>check_box_selector.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_checked="true" android:drawable="@drawable/shopping_cart_checked"/> <item android:state_checked="false" android:drawable="@drawable/shopping_cart_none_check"/> <item android:drawable="@drawable/shopping_cart_none_check"/> </selector>ic_launcher_background.xml
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:width="108dp" android:height="108dp" android:viewportHeight="108" android:viewportWidth="108"> <path android:fillColor="#26A69A" android:pathData="M0,0h108v108h-108z" /> <path android:fillColor="#00000000" android:pathData="M9,0L9,108" android:strokeColor="#33FFFFFF" android:strokeWidth="0.8" /> <path android:fillColor="#00000000" android:pathData="M19,0L19,108" android:strokeColor="#33FFFFFF" android:strokeWidth="0.8" /> <path android:fillColor="#00000000" android:pathData="M29,0L29,108" android:strokeColor="#33FFFFFF" android:strokeWidth="0.8" /> <path android:fillColor="#00000000" android:pathData="M39,0L39,108" android:strokeColor="#33FFFFFF" android:strokeWidth="0.8" /> <path android:fillColor="#00000000" android:pathData="M49,0L49,108" android:strokeColor="#33FFFFFF" android:strokeWidth="0.8" /> <path android:fillColor="#00000000" android:pathData="M59,0L59,108" android:strokeColor="#33FFFFFF" android:strokeWidth="0.8" /> <path android:fillColor="#00000000" android:pathData="M69,0L69,108" android:strokeColor="#33FFFFFF" android:strokeWidth="0.8" /> <path android:fillColor="#00000000" android:pathData="M79,0L79,108" android:strokeColor="#33FFFFFF" android:strokeWidth="0.8" /> <path android:fillColor="#00000000" android:pathData="M89,0L89,108" android:strokeColor="#33FFFFFF" android:strokeWidth="0.8" /> <path android:fillColor="#00000000" android:pathData="M99,0L99,108" android:strokeColor="#33FFFFFF" android:strokeWidth="0.8" /> <path android:fillColor="#00000000" android:pathData="M0,9L108,9" android:strokeColor="#33FFFFFF" android:strokeWidth="0.8" /> <path android:fillColor="#00000000" android:pathData="M0,19L108,19" android:strokeColor="#33FFFFFF" android:strokeWidth="0.8" /> <path android:fillColor="#00000000" android:pathData="M0,29L108,29" android:strokeColor="#33FFFFFF" android:strokeWidth="0.8" /> <path android:fillColor="#00000000" android:pathData="M0,39L108,39" android:strokeColor="#33FFFFFF" android:strokeWidth="0.8" /> <path android:fillColor="#00000000" android:pathData="M0,49L108,49" android:strokeColor="#33FFFFFF" android:strokeWidth="0.8" /> <path android:fillColor="#00000000" android:pathData="M0,59L108,59" android:strokeColor="#33FFFFFF" android:strokeWidth="0.8" /> <path android:fillColor="#00000000" android:pathData="M0,69L108,69" android:strokeColor="#33FFFFFF" android:strokeWidth="0.8" /> <path android:fillColor="#00000000" android:pathData="M0,79L108,79" android:strokeColor="#33FFFFFF" android:strokeWidth="0.8" /> <path android:fillColor="#00000000" android:pathData="M0,89L108,89" android:strokeColor="#33FFFFFF" android:strokeWidth="0.8" /> <path android:fillColor="#00000000" android:pathData="M0,99L108,99" android:strokeColor="#33FFFFFF" android:strokeWidth="0.8" /> <path android:fillColor="#00000000" android:pathData="M19,29L89,29" android:strokeColor="#33FFFFFF" android:strokeWidth="0.8" /> <path android:fillColor="#00000000" android:pathData="M19,39L89,39" android:strokeColor="#33FFFFFF" android:strokeWidth="0.8" /> <path android:fillColor="#00000000" android:pathData="M19,49L89,49" android:strokeColor="#33FFFFFF" android:strokeWidth="0.8" /> <path android:fillColor="#00000000" android:pathData="M19,59L89,59" android:strokeColor="#33FFFFFF" android:strokeWidth="0.8" /> <path android:fillColor="#00000000" android:pathData="M19,69L89,69" android:strokeColor="#33FFFFFF" android:strokeWidth="0.8" /> <path android:fillColor="#00000000" android:pathData="M19,79L89,79" android:strokeColor="#33FFFFFF" android:strokeWidth="0.8" /> <path android:fillColor="#00000000" android:pathData="M29,19L29,89" android:strokeColor="#33FFFFFF" android:strokeWidth="0.8" /> <path android:fillColor="#00000000" android:pathData="M39,19L39,89" android:strokeColor="#33FFFFFF" android:strokeWidth="0.8" /> <path android:fillColor="#00000000" android:pathData="M49,19L49,89" android:strokeColor="#33FFFFFF" android:strokeWidth="0.8" /> <path android:fillColor="#00000000" android:pathData="M59,19L59,89" android:strokeColor="#33FFFFFF" android:strokeWidth="0.8" /> <path android:fillColor="#00000000" android:pathData="M69,19L69,89" android:strokeColor="#33FFFFFF" android:strokeWidth="0.8" /> <path android:fillColor="#00000000" android:pathData="M79,19L79,89" android:strokeColor="#33FFFFFF" android:strokeWidth="0.8" /> </vector>ic_launcher_foreground.xml
<vector xmlns:android="http://schemas.android.com/apk/res/android" xmlns:aapt="http://schemas.android.com/aapt" android:width="108dp" android:height="108dp" android:viewportHeight="108" android:viewportWidth="108"> <path android:fillType="evenOdd" android:pathData="M32,64C32,64 38.39,52.99 44.13,50.95C51.37,48.37 70.14,49.57 70.14,49.57L108.26,87.69L108,109.01L75.97,107.97L32,64Z" android:strokeColor="#00000000" android:strokeWidth="1"> <aapt:attr name="android:fillColor"> <gradient android:endX="78.5885" android:endY="90.9159" android:startX="48.7653" android:startY="61.0927" android:type="linear"> <item android:color="#44000000" android:offset="0.0" /> <item android:color="#00000000" android:offset="1.0" /> </gradient> </aapt:attr> </path> <path android:fillColor="#FFFFFF" android:fillType="nonZero" android:pathData="M66.94,46.02L66.94,46.02C72.44,50.07 76,56.61 76,64L32,64C32,56.61 35.56,50.11 40.98,46.06L36.18,41.19C35.45,40.45 35.45,39.3 36.18,38.56C36.91,37.81 38.05,37.81 38.78,38.56L44.25,44.05C47.18,42.57 50.48,41.71 54,41.71C57.48,41.71 60.78,42.57 63.68,44.05L69.11,38.56C69.84,37.81 70.98,37.81 71.71,38.56C72.44,39.3 72.44,40.45 71.71,41.19L66.94,46.02ZM62.94,56.92C64.08,56.92 65,56.01 65,54.88C65,53.76 64.08,52.85 62.94,52.85C61.8,52.85 60.88,53.76 60.88,54.88C60.88,56.01 61.8,56.92 62.94,56.92ZM45.06,56.92C46.2,56.92 47.13,56.01 47.13,54.88C47.13,53.76 46.2,52.85 45.06,52.85C43.92,52.85 43,53.76 43,54.88C43,56.01 43.92,56.92 45.06,56.92Z" android:strokeColor="#00000000" android:strokeWidth="1" /> </vector>Layout布局
activity_main.xml
<RelativeLayout 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" tools:context="zhuyuanyuan.bwei.com.gouwuchetwo.MainActivity"> <ScrollView android:layout_above="@+id/linear_bottom" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <!--二级列表--> <zhuyuanyuan.bwei.com.gouwuchetwo.view.custom.MyExpanableListView android:id="@+id/expanable_list_view" android:layout_width="match_parent" android:layout_height="wrap_content"></zhuyuanyuan.bwei.com.gouwuchetwo.view.custom.MyExpanableListView> <!--recyclerView展示为你推荐--> <TextView android:background="#00ff00" android:text="为你推荐" android:layout_marginTop="10dp" android:layout_gravity="center_horizontal" android:layout_width="match_parent" android:layout_height="400dp" /> </LinearLayout> </ScrollView> <RelativeLayout android:id="@+id/relative_progress" android:visibility="gone" android:layout_above="@+id/linear_bottom" android:layout_width="match_parent" android:layout_height="match_parent"> <ProgressBar android:layout_centerInParent="true" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RelativeLayout> <LinearLayout android:orientation="horizontal" android:id="@+id/linear_bottom" android:layout_alignParentBottom="true" android:layout_width="match_parent" android:layout_height="50dp"> <CheckBox android:button="@null" android:background="@drawable/check_box_selector" android:layout_marginLeft="10dp" android:id="@+id/check_all" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/text_total" android:text="合计:¥0.00" android:layout_gravity="center_vertical" android:layout_marginLeft="10dp" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" /> <TextView android:id="@+id/text_buy" android:background="#ff0000" android:textColor="#ffffff" android:gravity="center" android:text="去结算(0)" android:layout_width="100dp" android:layout_height="match_parent" /> </LinearLayout> </RelativeLayout>child_layout.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="10dp"> <CheckBox android:id="@+id/child_check" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:background="@drawable/check_box_selector" android:button="@null" /> <ImageView android:id="@+id/child_image" android:layout_width="100dp" android:layout_height="100dp" android:layout_marginLeft="10dp" android:layout_toRightOf="@+id/child_check" /> <TextView android:id="@+id/child_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_toLeftOf="@+id/text_delete" android:layout_toRightOf="@+id/child_image" android:maxLines="2" android:minLines="2" /> <TextView android:id="@+id/child_price" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/child_image" android:layout_marginLeft="5dp" android:layout_toRightOf="@+id/child_image" android:text="¥0.00" android:textColor="#ff0000" /> <LinearLayout android:id="@+id/linearLayout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/child_image" android:layout_toLeftOf="@+id/text_delete" android:orientation="horizontal"> <TextView android:id="@+id/text_jian" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/bian_kuang" android:padding="5dp" android:text="-" /> <TextView android:id="@+id/text_num" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/bian_kuang" android:paddingBottom="5dp" android:paddingLeft="10dp" android:paddingRight="10dp" android:paddingTop="5dp" android:text="1" /> <TextView android:id="@+id/text_add" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/bian_kuang" android:padding="5dp" android:text="+" /> </LinearLayout> <TextView android:id="@+id/text_delete" android:layout_width="40dp" android:layout_height="wrap_content" android:layout_alignBottom="@+id/linearLayout" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:layout_marginLeft="5dp" android:background="#ff0000" android:gravity="center" android:text="删除" android:textColor="#ffffff" /> </RelativeLayout>group_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:padding="10dp" android:layout_width="match_parent" android:layout_height="match_parent"> <CheckBox android:button="@null" android:background="@drawable/check_box_selector" android:id="@+id/group_check" android:layout_gravity="center_vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/group_text" android:layout_marginLeft="10dp" android:layout_gravity="center_vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>model层
MainModel类 public class MainModel { private IMainPresenter iMainPresenter; public MainModel(MainPresenter iMainPresenter) { this.iMainPresenter = iMainPresenter; } //在这里真正获取购物车的数据 public void getCartData(String selectCartUrl) { Map<String, String> params = new HashMap<>(); params.put("uid","3690"); params.put("source","android"); OkHttp3Util.doPost(selectCartUrl, params, new Callback() { @Override public void onFailure(Call call, IOException e) { } @Override public void onResponse(Call call, Response response) throws IOException { if (response.isSuccessful()){ String json = response.body().string(); if ("null".equals(json)){ iMainPresenter.onSuccess(null); }else { //解析 CartBean cartBean = new Gson().fromJson(json,CartBean.class); //接口回调...presenter层 iMainPresenter.onSuccess(cartBean); } } } }); } }
presenter层
接口IMainPresenterpublic interface IMainPresenter {
void onSuccess(CartBean cartBean); }util包MainPresenter类 public class MainPresenter implements IMainPresenter { private MainModel mainModel; private IMainActivity iMainActivity; public MainPresenter(MainActivity iMainActivity) { this.iMainActivity = iMainActivity; //创建model mainModel = new MainModel(this); } //当前中间者不去直接获取网络数据,,,需要让model获取,,,创建presenter的时候就去创建出model,,,构造方法中 public void getCartData(String selectCartUrl) { //需要让model获取, mainModel.getCartData(selectCartUrl); } @Override public void onSuccess(CartBean cartBean) { //回调给view层...activity iMainActivity.onCartSuccess(cartBean); } }
ApiUtil类 public class ApiUtil { //查询购物车的路径 https://www.zhaoapi.cn/product/getCarts?uid=71&source=android public static String selectCartUrl = "https://www.zhaoapi.cn/product/getCarts"; //删除购物车...https://www.zhaoapi.cn/product/deleteCart?uid=72&pid=1 public static String deleteCartUrl ="https://www.zhaoapi.cn/product/deleteCart"; //更新购物车....?uid=71&sellerid=1&pid=1&selected=0&num=10 public static String updateCartUrl = "https://www.zhaoapi.cn/product/updateCarts"; //商品详情..... }
还有一个okhttp3工具类 (我有写一篇封装好的okhttp工具类的博客)
view层
MyAdapter
public class MyAdapter extends BaseExpandableListAdapter{ private MainPresenter mainPresenter; private RelativeLayout relative_progress; private Handler handler; private CartBean cartBean; private Context context; private int childIndex; private int allIndex; public MyAdapter(Context context, CartBean cartBean, Handler handler, RelativeLayout relative_progress, MainPresenter mainPresenter) { this.context = context; this.cartBean = cartBean; this.handler = handler; this.relative_progress = relative_progress; this.mainPresenter = mainPresenter; } @Override public int getGroupCount() { return cartBean.getData().size(); } @Override public int getChildrenCount(int groupPosition) { return cartBean.getData().get(groupPosition).getList().size(); } @Override public Object getGroup(int groupPosition) { return cartBean.getData().get(groupPosition); } @Override public Object getChild(int groupPosition, int childPosition) { return cartBean.getData().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 true; } @Override public View getGroupView(int groupPosition, boolean b, View view, ViewGroup viewGroup) { final GroupHolder holder; if (view == null){ view = View.inflate(context, R.layout.group_layout,null); holder = new GroupHolder(); holder.checkBox = view.findViewById(R.id.group_check); holder.textView = view.findViewById(R.id.group_text); view.setTag(holder); }else { holder = (GroupHolder) view.getTag(); } final CartBean.DataBean dataBean = cartBean.getData().get(groupPosition); //赋值 holder.textView.setText(dataBean.getSellerName()); holder.checkBox.setChecked(dataBean.isGroupChecked()); //商家的点击事件 holder.checkBox.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //显示progress relative_progress.setVisibility(View.VISIBLE); //根据商家的状态holder.checkbox.ischecked(),改变下面所有子条目的状态,,,10,,改变十次,更新完成一个之后再去执行下一个...递归 childIndex = 0; updateAllChildInGroup(dataBean,holder.checkBox.isChecked()); } }); return view; } /** * 根据商家状态使用递归改变所有的子条目 * @param dataBean * @param checked */ private void updateAllChildInGroup(final CartBean.DataBean dataBean, final boolean checked) { CartBean.DataBean.ListBean listBean = dataBean.getList().get(childIndex); Map<String, String> params = new HashMap<>(); //?uid=71&sellerid=1&pid=1&selected=0&num=10 params.put("uid","3690"); params.put("sellerid", String.valueOf(listBean.getSellerid())); params.put("pid", String.valueOf(listBean.getPid())); params.put("selected", String.valueOf(checked ? 1:0)); params.put("num", String.valueOf(listBean.getNum())); OkHttp3Util.doPost(ApiUtil.updateCartUrl, params, new Callback() { @Override public void onFailure(Call call, IOException e) { } @Override public void onResponse(Call call, Response response) throws IOException { //更新成功一条 if (response.isSuccessful()){ //索引增加 childIndex ++; if (childIndex < dataBean.getList().size()){ //再去更新下一条 updateAllChildInGroup(dataBean,checked); }else {//全都更新完成了....重新查询购物车 mainPresenter.getCartData(ApiUtil.selectCartUrl); } } } }); } @Override public View getChildView(int groupPosition, int childPosition, boolean b, View view, ViewGroup viewGroup) { ChildHolder holder; if (view == null){ view = View.inflate(context, R.layout.child_layout,null); holder = new ChildHolder(); holder.checkBox = view.findViewById(R.id.child_check); holder.text_title = view.findViewById(R.id.child_title); holder.imageView = view.findViewById(R.id.child_image); holder.text_price = view.findViewById(R.id.child_price); holder.text_jian = view.findViewById(R.id.text_jian); holder.text_num = view.findViewById(R.id.text_num); holder.text_add = view.findViewById(R.id.text_add); holder.text_delete = view.findViewById(R.id.text_delete); view.setTag(holder); }else { holder = (ChildHolder) view.getTag(); } final CartBean.DataBean.ListBean listBean = cartBean.getData().get(groupPosition).getList().get(childPosition); //赋值 holder.text_title.setText(listBean.getTitle()); holder.text_price.setText("¥"+listBean.getBargainPrice()); String[] strings = listBean.getImages().split("\\|"); Glide.with(context).load(strings[0]).into(holder.imageView); holder.checkBox.setChecked(listBean.getSelected() == 0? false:true);//根据0,1进行设置是否选中 //setText()我们使用一定是设置字符串 holder.text_num.setText(listBean.getNum()+""); //点击事件 holder.checkBox.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //此时需要显示进度条 relative_progress.setVisibility(View.VISIBLE); //更新购物车,,,,需要改变是否选中,,,如果现在显示的是0,改成1;;;1--->0 Map<String, String> params = new HashMap<>(); //?uid=71&sellerid=1&pid=1&selected=0&num=10 params.put("uid","3690"); params.put("sellerid", String.valueOf(listBean.getSellerid())); params.put("pid", String.valueOf(listBean.getPid())); params.put("selected", String.valueOf(listBean.getSelected() == 0? 1:0)); params.put("num", String.valueOf(listBean.getNum())); OkHttp3Util.doPost(ApiUtil.updateCartUrl, params, new Callback() { @Override public void onFailure(Call call, IOException e) { } @Override public void onResponse(Call call, Response response) throws IOException { //更新成功之后,,,,再次查询购物车的数据进行展示 if (response.isSuccessful()){ mainPresenter.getCartData(ApiUtil.selectCartUrl); } } }); } }); //加号 holder.text_add.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //此时需要显示进度条 relative_progress.setVisibility(View.VISIBLE); //更新购物车,,,,需要改变是数量,,,,需要加1 Map<String, String> params = new HashMap<>(); //?uid=71&sellerid=1&pid=1&selected=0&num=10 params.put("uid","3690"); params.put("sellerid", String.valueOf(listBean.getSellerid())); params.put("pid", String.valueOf(listBean.getPid())); params.put("selected", String.valueOf(listBean.getSelected())); params.put("num", String.valueOf(listBean.getNum()+1)); OkHttp3Util.doPost(ApiUtil.updateCartUrl, params, new Callback() { @Override public void onFailure(Call call, IOException e) { } @Override public void onResponse(Call call, Response response) throws IOException { //更新成功之后,,,,再次查询购物车的数据进行展示 if (response.isSuccessful()){ mainPresenter.getCartData(ApiUtil.selectCartUrl); } } }); } }); //减号 holder.text_jian.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { int num = listBean.getNum(); if (num == 1){ return; } //此时需要显示进度条 relative_progress.setVisibility(View.VISIBLE); //更新购物车,,,,需要改变是数量,,,,需要加1 Map<String, String> params = new HashMap<>(); //?uid=71&sellerid=1&pid=1&selected=0&num=10 params.put("uid","3690"); params.put("sellerid", String.valueOf(listBean.getSellerid())); params.put("pid", String.valueOf(listBean.getPid())); params.put("selected", String.valueOf(listBean.getSelected())); params.put("num", String.valueOf(num-1)); OkHttp3Util.doPost(ApiUtil.updateCartUrl, params, new Callback() { @Override public void onFailure(Call call, IOException e) { } @Override public void onResponse(Call call, Response response) throws IOException { //更新成功之后,,,,再次查询购物车的数据进行展示 if (response.isSuccessful()){ mainPresenter.getCartData(ApiUtil.selectCartUrl); } } }); } }); //删除 holder.text_delete.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //显示进度条 relative_progress.setVisibility(View.VISIBLE); //调用删除的接口 Map<String, String> params = new HashMap<>(); //uid=72&pid=1 params.put("uid","3690"); params.put("pid", String.valueOf(listBean.getPid())); OkHttp3Util.doPost(ApiUtil.deleteCartUrl, params, new Callback() { @Override public void onFailure(Call call, IOException e) { } @Override public void onResponse(Call call, Response response) throws IOException { if (response.isSuccessful()){ //再次请求购物车的数据 mainPresenter.getCartData(ApiUtil.selectCartUrl); } } }); } }); return view; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } /** * 计算总价和数量,,,并且发送给activity进行显示 */ public void sendPriceAndCount() { double price = 0; int count = 0; for (int i = 0;i<cartBean.getData().size();i++){ List<CartBean.DataBean.ListBean> listBeans = cartBean.getData().get(i).getList(); for (int j = 0; j< listBeans.size(); j++){ if (listBeans.get(j).getSelected() == 1){ price += listBeans.get(j).getBargainPrice() * listBeans.get(j).getNum(); count += listBeans.get(j).getNum(); } } } //double高精度,,,计算的时候可能会出现一串数字...保留两位 DecimalFormat decimalFormat = new DecimalFormat("0.00"); String priceString = decimalFormat.format(price); CountPriceBean countPriceBean = new CountPriceBean(priceString, count); //发送到主页面进行显示 Message msg = Message.obtain(); msg.what = 0; msg.obj = countPriceBean; handler.sendMessage(msg); } /** * 根据全选的状态更新所有商品的状态 * @param checked */ public void setAllChildsChecked(boolean checked) { //创建一个大的结合,,,存放所有商品的数据 List<CartBean.DataBean.ListBean> allList = new ArrayList<>(); for (int i= 0;i<cartBean.getData().size();i++){ List<CartBean.DataBean.ListBean> listBeans = cartBean.getData().get(i).getList(); for (int j=0;j<listBeans.size();j++){ allList.add(listBeans.get(j)); } } //显示progress relative_progress.setVisibility(View.VISIBLE); //递归更新.... allIndex = 0; updateAllChecked(allList,checked); } /** * 更新所有的商品 * @param allList * @param checked */ private void updateAllChecked(final List<CartBean.DataBean.ListBean> allList, final boolean checked) { CartBean.DataBean.ListBean listBean = allList.get(allIndex); Map<String, String> params = new HashMap<>(); //?uid=71&sellerid=1&pid=1&selected=0&num=10 params.put("uid","3690"); params.put("sellerid", String.valueOf(listBean.getSellerid())); params.put("pid", String.valueOf(listBean.getPid())); params.put("selected", String.valueOf(checked ? 1:0)); params.put("num", String.valueOf(listBean.getNum())); OkHttp3Util.doPost(ApiUtil.updateCartUrl, params, new Callback() { @Override public void onFailure(Call call, IOException e) { } @Override public void onResponse(Call call, Response response) throws IOException { //更新一条成功 if (response.isSuccessful()){ allIndex ++; if (allIndex < allList.size()){ //继续更新下一条 updateAllChecked(allList,checked); }else { //重新查询 mainPresenter.getCartData(ApiUtil.selectCartUrl); } } } }); } private class GroupHolder{ CheckBox checkBox; TextView textView; } private class ChildHolder{ CheckBox checkBox; ImageView imageView; TextView text_title; TextView text_price; TextView text_num; TextView text_jian; TextView text_add; TextView text_delete; } }
MyExpanableListView类 public class MyExpanableListView extends ExpandableListView { public MyExpanableListView(Context context) { super(context); } public MyExpanableListView(Context context, AttributeSet attrs) { super(context, attrs); } public MyExpanableListView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { //高度 int height = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE>>2,MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, height); } } 接口IMainActivitypublic interface IMainActivity { void onCartSuccess(CartBean cartBean); }MainActivity类 public class MainActivity extends AppCompatActivity implements IMainActivity,View.OnClickListener{ private MyExpanableListView expanableListView; private MainPresenter mainPresenter; private CheckBox check_all; private TextView text_total; private TextView text_buy; private RelativeLayout relative_progress; private MyAdapter myAdapter; private Handler handler = new Handler(){ @Override public void handleMessage(Message msg) { if (msg.what == 0){ CountPriceBean countPriceBean = (CountPriceBean) msg.obj; //设置价格和数量 text_total.setText("合计:¥"+countPriceBean.getPriceString()); text_buy.setText("去结算("+countPriceBean.getCount()+")"); } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //找到控件 expanableListView = (MyExpanableListView) findViewById(R.id.expanable_list_view); check_all = (CheckBox) findViewById(R.id.check_all); text_total = (TextView) findViewById(R.id.text_total); text_buy = (TextView) findViewById(R.id.text_buy); relative_progress = (RelativeLayout) findViewById(R.id.relative_progress); //去掉默认的指示器 expanableListView.setGroupIndicator(null); //获取数据....MVP mainPresenter = new MainPresenter(this); //全选的点击事件 check_all.setOnClickListener(this); } @Override protected void onResume() {//onResult...onRestart super.onResume(); //显示进度条 relative_progress.setVisibility(View.VISIBLE); //调用获取数据的方法 mainPresenter.getCartData(ApiUtil.selectCartUrl); } /** * 只要购物车页面显示 就要去网络获取新的数据....获取购物车数据操作放到获取焦点的生命周期方法中 */ @Override public void onCartSuccess(final CartBean cartBean) { //处于子线程!!!!!!!!!!!!!!!!!!! runOnUiThread(new Runnable() { @Override public void run() { //获取数据成功...隐藏 relative_progress.setVisibility(View.GONE); if (cartBean != null){ //cartBean再设置奢配器之前需不需要改变数据 //1.在bean类中添加商家是否选中的字段....默认值的false,初始值是根据该组下面所有孩子的状态进行改变的 for (int i = 0;i<cartBean.getData().size();i++){ //当前组中所有孩子的数据 List<CartBean.DataBean.ListBean> listBeans = cartBean.getData().get(i).getList(); //设置组的初始选中状态,,,,根据所有孩子的状态 cartBean.getData().get(i).setGroupChecked(isAllChildInGroupChecked(listBeans)); } //2.根据所有商家选中的状态,改变全选的状态 check_all.setChecked(isAllGroupChecked(cartBean)); //设置适配器 myAdapter = new MyAdapter(MainActivity.this, cartBean,handler,relative_progress,mainPresenter); expanableListView.setAdapter(myAdapter); //展开所有的组...expanableListView.expandGroup() for (int i = 0;i<cartBean.getData().size();i++){ expanableListView.expandGroup(i); } //3.计算总价和商品的数量 myAdapter.sendPriceAndCount(); }else { Toast.makeText(MainActivity.this,"购物车空,请添加购物车",Toast.LENGTH_SHORT).show(); } } }); } /** * 所有的商家是否选中 * @param cartBean * @return */ private boolean isAllGroupChecked(CartBean cartBean) { for (int i=0;i<cartBean.getData().size();i++){ //只要有一个组未选中 返回false if (! cartBean.getData().get(i).isGroupChecked()){ return false; } } return true; } /** * 当前组中所有的孩子是否选中 * @param listBeans 当前组中所有的孩子的数据 * @return */ private boolean isAllChildInGroupChecked(List<CartBean.DataBean.ListBean> listBeans) { for (int i=0;i<listBeans.size();i++){ if (listBeans.get(i).getSelected() == 0){ return false; } } return true; } @Override public void onClick(View view) { switch (view.getId()){ case R.id.check_all: //根据全选的状态更新所有商品的状态...check_all.isChecked() true...1;;;;false---0 myAdapter.setAllChildsChecked(check_all.isChecked()); break; } } } bean类 CartBean类public class CartBean { private String code; private String msg; private List<DataBean> data; public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public List<DataBean> getData() { return data; } public void setData(List<DataBean> data) { this.data = data; } public static class DataBean { private boolean groupChecked; public boolean isGroupChecked() { return groupChecked; } public void setGroupChecked(boolean groupChecked) { this.groupChecked = groupChecked; } 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 { 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;//当前的商品子条目是否选中....1表示选中,0未选中 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 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; } } } } CountPriceBean类 public class CountPriceBean { private String priceString; private int count; public CountPriceBean(String priceString, int count) { this.priceString = priceString; this.count = count; } public String getPriceString() { return priceString; } public void setPriceString(String priceString) { this.priceString = priceString; } public int getCount() { return count; } public void setCount(int count) { this.count = count; } }