Android_二级列表购物车之增删改查,全选反选,加减器,价钱数量计算

来源:互联网 发布:天猫商城数据分析 编辑:程序博客网 时间:2024/04/28 03:32


转载自:http://blog.csdn.net/Rbh73692/article/details/78603260?locationNum=3&fps=1


第一步: 导依赖

Eventbus依赖:

[html] view plain copy
  1. compile 'org.greenrobot:eventbus:3.1.1'  
gson依赖:

[html] view plain copy
  1. compile 'com.google.code.gson:gson:2.8.2'  

OkHttp依赖:

[html] view plain copy
  1. compile 'com.squareup.okhttp3:okhttp:3.9.0'  
recyclerView依赖:

[html] view plain copy
  1. compile 'com.android.support:recyclerview-v7:26.0.0-alpha1'  
glid依赖:

[html] view plain copy
  1. repositories {  
  2.   mavenCentral()  
  3.   maven { url 'https://maven.google.com' }  
  4. }  
  5.   
  6. dependencies {  
  7.   compile 'com.github.bumptech.glide:glide:4.3.1'  
  8.   annotationProcessor 'com.github.bumptech.glide:compiler:4.3.1'  
  9. }  
第二: 清单文件加权限:

[html] view plain copy
  1. <uses-permission android:name="android.permission.INTERNET" />  
  2.     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />  
  3.     <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />  
  4.     <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />  
第三:布局 Main_Activity

[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"  
  4.     xmlns:tools="http://schemas.android.com/tools"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.     android:orientation="vertical"  
  8.   
  9.     tools:context="com.bwei.www.yuekaomoni.Main2Activity">  
  10.     <ExpandableListView  
  11.         android:id="@+id/elv"  
  12.         android:layout_width="match_parent"  
  13.         android:layout_height="match_parent"  
  14.         android:layout_weight="1" />  
  15.   
  16.     <RelativeLayout  
  17.         android:layout_width="match_parent"  
  18.         android:layout_height="50dp"  
  19.         android:layout_alignParentBottom="true"  
  20.         android:background="@android:color/white"  
  21.         android:gravity="center_vertical">  
  22.   
  23.         <CheckBox  
  24.             android:id="@+id/checkbox2"  
  25.             android:layout_width="wrap_content"  
  26.             android:layout_height="wrap_content"  
  27.             android:layout_centerVertical="true"  
  28.             android:layout_marginLeft="10dp"  
  29.             android:focusable="false" />  
  30.   
  31.         <TextView  
  32.             android:layout_width="wrap_content"  
  33.             android:layout_height="50dp"  
  34.             android:layout_centerVertical="true"  
  35.             android:layout_marginLeft="10dp"  
  36.             android:layout_toRightOf="@+id/checkbox2"  
  37.             android:gravity="center_vertical"  
  38.             android:text="全选"  
  39.             android:textSize="20sp" />  
  40.   
  41.         <LinearLayout  
  42.             android:layout_width="wrap_content"  
  43.             android:layout_height="wrap_content"  
  44.             android:layout_marginLeft="100dp"  
  45.             android:orientation="horizontal">  
  46.   
  47.             <TextView  
  48.   
  49.                 android:layout_width="wrap_content"  
  50.                 android:layout_height="wrap_content"  
  51.                 android:layout_marginLeft="10dp"  
  52.                 android:text="合计 :" />  
  53.   
  54.   
  55.             <TextView  
  56.                 android:id="@+id/tv_price"  
  57.                 android:layout_width="wrap_content"  
  58.                 android:layout_height="50dp"  
  59.                 android:layout_marginLeft="10dp"  
  60.                 android:paddingRight="10dp"  
  61.                 android:text="0"  
  62.                 android:textColor="@android:color/holo_red_light" />  
  63.   
  64.   
  65.             <TextView  
  66.                 android:layout_marginLeft="150dp"  
  67.                 android:id="@+id/tv_num"  
  68.                 android:layout_width="150dp"  
  69.                 android:layout_height="50dp"  
  70.                 android:background="@android:color/holo_red_dark"  
  71.                 android:gravity="center"  
  72.                 android:padding="10dp"  
  73.                 android:text="结算(0)"  
  74.                 android:textColor="@android:color/white" />  
  75.         </LinearLayout>  
  76.     </RelativeLayout>  
  77.   
  78.   
  79.   
  80.   
  81. </LinearLayout>  
一级布局:

[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="10dp"  
  5.     android:gravity="center_vertical"  
  6.     android:orientation="horizontal">  
  7.   
  8.     <CheckBox  
  9.         android:id="@+id/cb_parent"  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content"  
  12.         android:layout_marginBottom="5dp"  
  13.         android:layout_marginLeft="10dp"  
  14.         android:layout_marginTop="5dp"  
  15.         android:focusable="false" />  
  16.   
  17.     <TextView  
  18.         android:id="@+id/tv_sign"  
  19.         android:layout_width="wrap_content"  
  20.         android:layout_height="wrap_content"  
  21.         android:layout_marginLeft="20dp"  
  22.         android:text="标记" />  
  23.   
  24.     <TextView  
  25.         android:id="@+id/tv_number"  
  26.         android:layout_width="wrap_content"  
  27.         android:layout_height="wrap_content"  
  28.         android:layout_marginLeft="20dp"  
  29.         android:text="12345678" />  
  30.   
  31. </LinearLayout>  
二级布局:

[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.   
  6.     android:gravity="center_vertical"  
  7.     android:orientation="horizontal">  
  8.   
  9.     <CheckBox  
  10.         android:id="@+id/cb_child"  
  11.         android:layout_width="wrap_content"  
  12.         android:layout_height="wrap_content"  
  13.         android:layout_marginBottom="30dp"  
  14.         android:layout_marginLeft="10dp"  
  15.         android:layout_marginTop="30dp"  
  16.         android:focusable="false" />  
  17.   
  18.    <RelativeLayout  
  19.        android:layout_width="100dp"  
  20.        android:layout_height="100dp"  
  21.        >  
  22.        <TextView  
  23.            android:id="@+id/tv_sbt"  
  24.            android:layout_width="wrap_content"  
  25.            android:layout_height="wrap_content"  
  26.            android:singleLine="true"  
  27.            android:ellipsize="end"  
  28.            android:text="三只松鼠大礼包发动机"/>  
  29.        <ImageView  
  30.            android:layout_below="@id/tv_sbt"  
  31.            android:layout_width="80dp"  
  32.            android:layout_height="80dp"  
  33.            android:id="@+id/img"  
  34.            android:src="@mipmap/ic_launcher"/>  
  35.    </RelativeLayout>  
  36.   
  37.   
  38.     <LinearLayout  
  39.         android:layout_width="match_parent"  
  40.         android:layout_height="wrap_content"  
  41.         android:layout_weight="3"  
  42.         android:orientation="vertical">  
  43.   
  44.         <TextView  
  45.             android:id="@+id/tv_pri"  
  46.             android:layout_width="wrap_content"  
  47.             android:layout_height="wrap_content"  
  48.   
  49.             android:text="¥3000.00" />  
  50.         <TextView  
  51.             android:id="@+id/tv_content"  
  52.             android:layout_width="wrap_content"  
  53.             android:layout_height="wrap_content"  
  54.             android:layout_marginLeft="20dp"  
  55.             android:text="什么" />  
  56.         <LinearLayout  
  57.             android:layout_width="match_parent"  
  58.             android:layout_height="wrap_content"  
  59.             android:gravity="center_vertical">  
  60.             <com.bwei.www.yuekaomoni.myView.MyView  
  61.                 android:id="@+id/mv"  
  62.                 android:layout_width="wrap_content"  
  63.                 android:layout_height="wrap_content"></com.bwei.www.yuekaomoni.myView.MyView>  
  64.   
  65.         </LinearLayout>  
  66.   
  67.     </LinearLayout>  
  68.   
  69.     <Button  
  70.         android:id="@+id/tv_del"  
  71.         android:layout_width="20dp"  
  72.         android:layout_height="20dp"  
  73.         android:background="@drawable/rublish"  
  74.          />  
  75. </LinearLayout>  

加减器布局:

[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="horizontal">  
  6.     <Button  
  7.         android:id="@+id/btn_add"  
  8.         android:layout_width="20dp"  
  9.         android:layout_height="20dp"  
  10.         android:background="@drawable/shopcart_add_red"/>  
  11.     <EditText  
  12.         android:id="@+id/et_et"  
  13.         android:layout_width="50dp"  
  14.         android:layout_height="wrap_content"  
  15.         android:text="1"  
  16.         android:textSize="20dp"/>  
  17.     <Button  
  18.         android:id="@+id/btn_del"  
  19.         android:layout_width="20dp"  
  20.         android:layout_height="20dp"  
  21.         android:background="@drawable/shopcart_minus_red"/>  
  22. </LinearLayout>  

第四:接口

[html] view plain copy
  1. public interface Callback {  
  2.     void onSuccess(String tag,Object o);  
  3.     void onFailure(String tag,Exception e);  
  4. }  
[html] view plain copy
  1. public interface IView {  
  2.     void Success (String tag, List<GsonBean.DataBean> list);  
  3.     void Failed(String tag,Exception e);  
  4. }  
第五: Okhttp

[html] view plain copy
  1. package com.bwei.www.yuekaomoni.okHttp;  
  2.   
  3. import android.os.Handler;  
  4. import android.text.TextUtils;  
  5. import android.util.Log;  
  6.   
  7. import com.bwei.www.yuekaomoni.Callback.Callback;  
  8. import com.google.gson.Gson;  
  9.   
  10. import java.io.IOException;  
  11. import java.util.Map;  
  12.   
  13. import okhttp3.Call;  
  14. import okhttp3.OkHttpClient;  
  15. import okhttp3.Request;  
  16. import okhttp3.Response;  
  17.   
  18. /**  
  19.  * @作者   
  20.  * @时间 2017/11/18 10:34  
  21.  */  
  22.   
  23. public class HttpUtils {  
  24.     private static final String TAG = "HttpUtils";  
  25.     private static volatile HttpUtils instanse;  
  26.     final static Handler handler = new Handler();  
  27.   
  28.     private HttpUtils() {  
  29.   
  30.     }  
  31.     public static HttpUtils getInstanse() {  
  32.         if (null == instanse) {  
  33.             synchronized (HttpUtils.class) {  
  34.                 if (instanse == null) {  
  35.                     instanse = new HttpUtils();  
  36.                 }  
  37.             }  
  38.         }  
  39.         return instanse;  
  40.     }  
  41.     public void get(String url, Map<String, String> map, final Callback callBack,  
  42.                     final Class cls, final String tag) {  
  43.         // http://www.baoidu.com/login?mobile=11111&password=11111&age=1&name=zw  
  44.   
  45.         // 1.http://www.baoidu.com/login                --------? key=value&key=value  
  46.         // 2.http://www.baoidu.com/login?               --------- key=value&key=value  
  47.         // 3.http://www.baoidu.com/login?mobile=11111   -----&key=value&key=value  
  48.         final StringBuffer sb = new StringBuffer();  
  49.         sb.append(url);  
  50.   
  51.         if (TextUtils.isEmpty(url)) {  
  52.             return;  
  53.         }  
  54.         // 如果包含?说明是2.3类型  
  55.         if (url.contains("?")) {  
  56.             // 如果包含?并且?是最后一位,对应是2类型  
  57.             if (url.indexOf("?") == url.length() - 1) {  
  58.   
  59.             } else {  
  60.                 // 如果包含?并且?不是最后一位,对应是3类型  
  61.                 sb.append("&");  
  62.             }  
  63.         }  
  64.         for (Map.Entry<String, String> entry : map.entrySet()) {  
  65.             sb.append(entry.getKey())  
  66.                     .append("=")  
  67.                     .append(entry.getValue())  
  68.                     .append("&");  
  69.         }  
  70.         if (sb.indexOf("&") != -1) {  
  71.             sb.deleteCharAt((sb.lastIndexOf("&")));  
  72.         }  
  73.         Log.i(TAG, "get:url " + sb.toString());  
  74.         OkHttpClient client = new OkHttpClient.Builder().build();  
  75.         Request request = new Request.Builder()  
  76.                 .get()  
  77.                 .url(sb.toString())  
  78.                 .build();  
  79.         Call call = client.newCall(request);  
  80.         call.enqueue(new okhttp3.Callback() {  
  81.             @Override  
  82.             public void onFailure(Call call, final IOException e) {  
  83.                 handler.post(new Runnable() {  
  84.                     @Override  
  85.                     public void run() {  
  86.                         callBack.onFailure(tag,e);  
  87.                     }  
  88.                 });  
  89.             }  
  90.             @Override  
  91.             public void onResponse(Call call, Response response) throws IOException {  
  92.                 final String s = response.body().string();  
  93.                 handler.post(new Runnable() {  
  94.                     Object o;  
  95.                     @Override  
  96.                     public void run() {  
  97.                         if(TextUtils.isEmpty(s)){  
  98.                             o=null;  
  99.                         }else  
  100.                         {  
  101.                             o = new Gson().fromJson(s, cls);  
  102.                         }  
  103.                         callBack.onSuccess(tag,o);  
  104.                     }  
  105.                 });  
  106.             }  
  107.         });  
  108.     }  
  109. }  
第六 :p层 {数据自己解析}

[html] view plain copy
  1. package com.bwei.www.yuekaomoni.present;  
  2.   
  3. import com.bwei.www.yuekaomoni.Callback.Callback;  
  4. import com.bwei.www.yuekaomoni.Callback.IView;  
  5. import com.bwei.www.yuekaomoni.bean.GsonBean;  
  6. import com.bwei.www.yuekaomoni.okHttp.HttpUtils;  
  7.   
  8. import java.util.HashMap;  
  9. import java.util.List;  
  10. import java.util.Map;  
  11.   
  12. /**  
  13.  * @作者   
  14.  * @时间 2017/11/18 10:51  
  15.  */  
  16.   
  17. public class PresentCar {  
  18.     private IView inv;  
  19.   
  20.     public void attachView(IView inv) {  
  21.         this.inv = inv;  
  22.     }  
  23.     public void getNews() {  
  24.         //type=top&key=dbedecbcd1899c9785b95cc2d17131c5  
  25.         Map<String, String> map = new HashMap<>();  
  26.   
  27.         HttpUtils.getInstanse().get("http://120.27.23.105/product/getCarts?uid=100", map, new Callback() {  
  28.             @Override  
  29.             public void onSuccess(String tag, Object o) {  
  30.                 GsonBean bean = (GsonBean) o;  
  31.                 if (bean != null) {  
  32.                     List<GsonBean.DataBean> data = bean.getData();  
  33.   
  34.                     inv.Success(tag, data);  
  35.                 }  
  36.             }  
  37.   
  38.             @Override  
  39.             public void onFailure(String tag, Exception e) {  
  40.                 inv.Failed(tag, e);  
  41.             }  
  42.   
  43.   
  44.         }, GsonBean.class, "news");  
  45.     }  
  46.     public void detachView() {  
  47.         if (inv != null) {  
  48.             inv = null;  
  49.         }  
  50.     }  
  51. }  
第七:二级列表适配器:

[html] view plain copy
  1. package com.bwei.www.yuekaomoni.adapter;  
  2.   
  3. import android.content.Context;  
  4. import android.view.View;  
  5. import android.view.ViewGroup;  
  6. import android.widget.BaseExpandableListAdapter;  
  7. import android.widget.CheckBox;  
  8. import android.widget.EditText;  
  9. import android.widget.ImageView;  
  10. import android.widget.TextView;  
  11.   
  12. import com.bumptech.glide.Glide;  
  13. import com.bwei.www.yuekaomoni.R;  
  14. import com.bwei.www.yuekaomoni.bean.GsonBean;  
  15. import com.bwei.www.yuekaomoni.check.MessageEvent;  
  16. import com.bwei.www.yuekaomoni.eventbusevent.PriceAndCountEvent;  
  17. import com.bwei.www.yuekaomoni.myView.MyView;  
  18.   
  19. import org.greenrobot.eventbus.EventBus;  
  20.   
  21. import java.util.ArrayList;  
  22. import java.util.List;  
  23.   
  24. import static com.bwei.www.yuekaomoni.R.id.img;  
  25. import static com.bwei.www.yuekaomoni.R.id.mv;  
  26. import static com.bwei.www.yuekaomoni.R.id.tv_content;  
  27. import static com.bwei.www.yuekaomoni.R.id.tv_pri;  
  28.   
  29. /**  
  30.  * @作者  
  31.  * @时间 2017/11/18 11:17  
  32.  */  
  33.   
  34. public class StarExpanderAdapter extends BaseExpandableListAdapter {  
  35.     Context context;  
  36.     List<GsonBean.DataBean> dataBeen;  
  37.     ArrayList<List<GsonBean.DataBean.ListBean>> listBean;  
  38.   
  39.   
  40.   
  41.     public StarExpanderAdapter(Context context, List<GsonBean.DataBean> dataBeen, ArrayList<List<GsonBean.DataBean.ListBean>> listBean) {  
  42.         this.context = context;  
  43.         this.dataBeen = dataBeen;  
  44.         this.listBean = listBean;  
  45.   
  46.     }  
  47.   
  48.     @Override  
  49.     public int getGroupCount() {  
  50.         return dataBeen.size();  
  51.     }  
  52.   
  53.     @Override  
  54.     public int getChildrenCount(int i) {  
  55.         return listBean.get(i).size();  
  56.     }  
  57.   
  58.     @Override  
  59.     public Object getGroup(int i) {  
  60.             return dataBeen.get(i);  
  61.     }  
  62.   
  63.     @Override  
  64.     public Object getChild(int i, int i1) {  
  65.         return listBean.get(i).get(i1);  
  66.     }  
  67.   
  68.     @Override  
  69.     public long getGroupId(int i) {  
  70.         return i;  
  71.     }  
  72.   
  73.     @Override  
  74.     public long getChildId(int i, int i1) {  
  75.         return i1;  
  76.     }  
  77.   
  78.     @Override  
  79.     public boolean hasStableIds() {  
  80.         return false;  
  81.     }  
  82.     //一级列表布局  
  83.     @Override  
  84.     public View getGroupView(final int i, boolean b, View view, ViewGroup viewGroup) {  
  85.         final GsonBean.DataBean groupBean = dataBeen.get(i);  
  86.         final GroupViewHolder holder;  
  87.         if(view==null){  
  88.             holder = new GroupViewHolder();  
  89.             view = View.inflate(context, R.layout.gw_item2, null);  
  90.             holder.tv_sbt = view.findViewById(R.id.tv_sign);  
  91.             holder.cbgroup = view.findViewById(R.id.cb_parent);  
  92.             view.setTag(holder);  
  93.         }else  
  94.         {  
  95.             holder = (GroupViewHolder) view.getTag();  
  96.         }  
  97.         holder.cbgroup.setChecked(groupBean.isGropuCb());  
  98.         holder.tv_sbt.setText(groupBean.getSellerName());  
  99.         //一级列表checkBox的点击事件  
  100.         holder.cbgroup.setOnClickListener(new View.OnClickListener() {  
  101.             @Override  
  102.             public void onClick(View view) {  
  103.                 //判断一级列表复选框的状态  设置为true或false  
  104.                 groupBean.setGropuCb(holder.cbgroup.isChecked());  
  105.                 //改变二级checkbod的状态  
  106.                 changeChildeCbState(i,holder.cbgroup.isChecked());  
  107.                 //算钱  
  108.                 EventBus.getDefault().post(compute());  
  109.                 //改变全选状态   isAllGroupCbSelect判断一级是否全部选中  
  110.                 changeAllCbState(isAllGroupCbSelected());  
  111.                 //必刷新  
  112.                 notifyDataSetChanged();  
  113.             }  
  114.         });  
  115.         return view;  
  116.     }  
  117.     //二级列表布局  
  118.     @Override  
  119.     public View getChildView(final int i, final int i1, boolean b, View view, ViewGroup viewGroup) {  
  120.         final GsonBean.DataBean.ListBean clildBean = this.listBean.get(i).get(i1);  
  121.         final ChildViewHolder childViewHolder;  
  122.         if(view==null){  
  123.            view = View.inflate(context, R.layout.gwc_item, null);  
  124.            childViewHolder = new ChildViewHolder();  
  125.             childViewHolder.tv_sbt = (TextView) view.findViewById(R.id.tv_sbt);  
  126.             childViewHolder.img = (ImageView) view.findViewById(img);  
  127.             childViewHolder.tv_pri = (TextView) view.findViewById(tv_pri);  
  128.             childViewHolder.tv_content = (TextView) view.findViewById(tv_content);  
  129.             childViewHolder.cbChild =view.findViewById(R.id.cb_child);  
  130.             childViewHolder.mv = view.findViewById(mv);  
  131.             childViewHolder.tv_del = view.findViewById(R.id.tv_del);  
  132.             childViewHolder.et_et = (EditText) childViewHolder.mv.findViewById(R.id.et_et);  
  133.             view.setTag(childViewHolder);  
  134.        }else{  
  135.             childViewHolder = (ChildViewHolder) view.getTag();  
  136.         }  
  137.         childViewHolder.tv_sbt.setText(clildBean.getTitle());  
  138.         String images = clildBean.getImages();  
  139.         String[] split = images.split("\\|");  
  140.         Glide.with(context).load(split[0]).into(childViewHolder.img);  
  141.         childViewHolder.tv_pri.setText(clildBean.getPrice()+"");  
  142.         childViewHolder.tv_content.setText(clildBean.getSubhead());  
  143.         childViewHolder.cbChild.setChecked(clildBean.isChildCb());  
  144.         childViewHolder.et_et.setText(clildBean.getNum()+"");  
  145.         //加号  
  146.         childViewHolder.mv.setOnAddDelClickLstener(new MyView.OnAddDelClickLstener() {  
  147.             @Override  
  148.             public void onAddClick(View v) {  
  149.                 int num = clildBean.getNum();  
  150.                 //num为int类型所以要加空字符串  
  151.                 childViewHolder.et_et.setText(++num+"");  
  152.                 clildBean.setNum(num);  
  153.                 //如果二级列表的checkbox为选中,计算价钱  
  154.                 if (childViewHolder.cbChild.isChecked()){  
  155.                     PriceAndCountEvent priceAndCountEvent = compute();  
  156.                     EventBus.getDefault().post(priceAndCountEvent);  
  157.                 }  
  158.             }  
  159.         //减号  
  160.             @Override  
  161.             public void onDelClick(View v) {  
  162.                 int num = clildBean.getNum();  
  163.                 if(num==1){  
  164.                     return;  
  165.                 }  
  166.                 childViewHolder.et_et.setText(--num+"");  
  167.                 clildBean.setNum(num);  
  168.                 if(childViewHolder.cbChild.isChecked()){  
  169.                     PriceAndCountEvent priceAndCountEvent = compute();  
  170.                     EventBus.getDefault().post(priceAndCountEvent);  
  171.                 }  
  172.             }  
  173.         });  
  174.         //删除  
  175.         childViewHolder.tv_del.setOnClickListener(new View.OnClickListener() {  
  176.             @Override  
  177.             public void onClick(View view) {  
  178.                 List<GsonBean.DataBean.ListBean> listBeen = listBean.get(i);  
  179.                 GsonBean.DataBean.ListBean remove = listBeen.remove(i1);  
  180.                 if(listBeen.size()==0){  
  181.                     //先移除二级列表的集合,再移除一级列表的集合  
  182.                     listBean.remove(i);  
  183.                     dataBeen.remove(i);  
  184.                 }  
  185.                 //算钱  
  186.                 EventBus.getDefault().post(compute());  
  187.                 notifyDataSetChanged();  
  188.             }  
  189.         });  
  190.   
  191.         //二级列表的点击事件  
  192.         childViewHolder.cbChild.setOnClickListener(new View.OnClickListener() {  
  193.             @Override  
  194.             public void onClick(View view) {  
  195.                 //设置该条目对象里的checked属性值  
  196.                 clildBean.setChildCb(childViewHolder.cbChild.isChecked());  
  197.                 //计算价钱  
  198.                 PriceAndCountEvent priceAndCountEvent = compute();  
  199.                 EventBus.getDefault().post(priceAndCountEvent);  
  200.                 //判断当前checkbox是选中的状态  
  201.                 if(childViewHolder.cbChild.isChecked()){  
  202.                     //如果全部选中(isAllChildCbSelected)  
  203.                     if(isAllChildCbSelected(i)){  
  204.                         //改变一级列表的状态  
  205.                         changGroupCbState(i,true);  
  206.                         //改变全选的状态  
  207.                         changeAllCbState(isAllGroupCbSelected());  
  208.                     }  
  209.                 }else {  
  210.                     //如果没有全部选中,一级列表的checkbox为false不为选中  
  211.                     changGroupCbState(i,false);  
  212.                     changeAllCbState(isAllGroupCbSelected());  
  213.                 }  
  214.                 notifyDataSetChanged();  
  215.             }  
  216.         });  
  217.   
  218.         return view;  
  219.     }  
  220.   
  221.     @Override  
  222.     public boolean isChildSelectable(int i, int i1) {  
  223.         return true;  
  224.     }  
  225.     class GroupViewHolder {  
  226.         CheckBox cbgroup;  
  227.         TextView tv_sbt;  
  228.     }  
  229.     class ChildViewHolder {  
  230.         TextView tv_sbt;  
  231.         ImageView img;  
  232.         TextView tv_pri;  
  233.         TextView tv_content;  
  234.         CheckBox cbChild;  
  235.         MyView mv;  
  236.         TextView tv_del;  
  237.   
  238.         EditText et_et;  
  239.     }  
  240.   
  241.     /**  
  242.      * 改变二级列表checkbox状态  
  243.      * 如果一级选中,控制二级也选中  
  244.      * @param i  
  245.      * @param flag  
  246.      */  
  247.      private void changeChildeCbState(int i,boolean flag){  
  248.          List<GsonBean.DataBean.ListBean> listBeen = listBean.get(i);  
  249.          for (int j = 0; j <listBeen.size(); j++) {  
  250.              GsonBean.DataBean.ListBean listBean = listBeen.get(j);  
  251.              listBean.setChildCb(flag);  
  252.          }  
  253.      }  
  254.   
  255.     /**  
  256.      * 判断一级列表是否全选中  
  257.      * @return  
  258.      */  
  259.      private boolean isAllGroupCbSelected(){  
  260.          for (int i = 0; i <dataBeen.size() ; i++) {  
  261.              GsonBean.DataBean dataBean = dataBeen.get(i);  
  262.              if(!dataBean.isGropuCb()){  
  263.                  return false;  
  264.              }  
  265.          }  
  266.          return true;  
  267.      }  
  268.     /**  
  269.      * 改变全选的状态  
  270.      *  
  271.      * @param flag  
  272.      */  
  273.      private void changeAllCbState(boolean flag){  
  274.          MessageEvent messageEvent = new MessageEvent();  
  275.          messageEvent.setChecked(flag);  
  276.          EventBus.getDefault().post(messageEvent);  
  277.      }  
  278.   
  279.     /**  
  280.      * 判断二级列表是否全部选中  
  281.      * @param i  
  282.      * @return  
  283.      */  
  284.     private boolean isAllChildCbSelected (int i){  
  285.          List<GsonBean.DataBean.ListBean> listBeen = listBean.get(i);  
  286.          for (int j = 0; j <listBeen.size() ; j++) {  
  287.              GsonBean.DataBean.ListBean listBean = listBeen.get(j);  
  288.              if(!listBean.isChildCb()){  
  289.                  return false;  
  290.              }  
  291.          }  
  292.          return true;  
  293.      }  
  294.     /**  
  295.      * 改变一级列表checkbox状态  
  296.      *  
  297.      * @param i  
  298.      */  
  299.      private void changGroupCbState(int i,boolean flag){  
  300.          GsonBean.DataBean dataBean = dataBeen.get(i);  
  301.          dataBean.setGropuCb(flag);  
  302.      }  
  303.   
  304.     /**  
  305.      * 改变二级列表checkbox状态  
  306.      * @param i  
  307.      * @param flag  
  308.      */  
  309.      private void changeChildCbState(int i,boolean flag){  
  310.          List<GsonBean.DataBean.ListBean> listBeen = listBean.get(i);  
  311.          for (int j = 0; j <listBeen.size() ; j++) {  
  312.              GsonBean.DataBean.ListBean listBean = listBeen.get(j);  
  313.              listBean.setChildCb(flag);  
  314.          }  
  315.      }  
  316.     /**  
  317.      * 计算列表中,选中的钱和数量  
  318.      */  
  319.      private PriceAndCountEvent compute(){  
  320.         int count = 0;  
  321.          int price = 0;  
  322.          for (int i = 0; i <listBean.size() ; i++) {  
  323.              List<GsonBean.DataBean.ListBean> listBeen = listBean.get(i);  
  324.              for (int j = 0; j <listBeen.size() ; j++) {  
  325.                  GsonBean.DataBean.ListBean listBean = listBeen.get(j);  
  326.                  if(listBean.isChildCb()){  
  327.                      price+=listBean.getNum()*listBean.getPrice();  
  328.                      count+=listBean.getNum();  
  329.                  }  
  330.              }  
  331.          }  
  332.          PriceAndCountEvent priceAndCountEvent = new PriceAndCountEvent();  
  333.          priceAndCountEvent.setCount(count);  
  334.          priceAndCountEvent.setPrice(price);  
  335.          return priceAndCountEvent;  
  336.      }  
  337.     /**  
  338.      * 设置全选、反选  
  339.      *  
  340.      * @param flag  
  341.      */  
  342.     public void changeAllListCbState(boolean flag) {  
  343.         for (int i = 0; i < dataBeen.size(); i++) {  
  344.             changGroupCbState(i, flag);  
  345.             changeChildCbState(i, flag);  
  346.         }  
  347.         //算钱  
  348.         EventBus.getDefault().post(compute());  
  349.         notifyDataSetChanged();  
  350.     }  
  351.   
  352. }  
第八:MessageEvent类:
[html] view plain copy
  1. public class MessageEvent {  
  2.     private boolean checked;  
  3.   
  4.     public boolean isChecked() {  
  5.         return checked;  
  6.     }  
  7.   
  8.     public void setChecked(boolean checked) {  
  9.         this.checked = checked;  
  10.     }  
  11. }  

  PriceAndCountEvent 类:
[html] view plain copy
  1. public class PriceAndCountEvent {  
  2.     private int price;  
  3.     private int count;  
  4.   
  5.     public int getPrice() {  
  6.         return price;  
  7.     }  
  8.   
  9.     public void setPrice(int price) {  
  10.         this.price = price;  
  11.     }  
  12.   
  13.     public int getCount() {  
  14.         return count;  
  15.     }  
  16.   
  17.     public void setCount(int count) {  
  18.         this.count = count;  
  19.     }  
  20. }  
第九:
MainActivity:
[html] view plain copy
  1. package com.bwei.www.yuekaomoni;  
  2.   
  3. import android.os.Bundle;  
  4. import android.support.v7.app.AppCompatActivity;  
  5. import android.view.View;  
  6. import android.widget.CheckBox;  
  7. import android.widget.ExpandableListView;  
  8. import android.widget.TextView;  
  9.   
  10. import com.bwei.www.yuekaomoni.Callback.IView;  
  11. import com.bwei.www.yuekaomoni.adapter.StarExpanderAdapter;  
  12. import com.bwei.www.yuekaomoni.bean.GsonBean;  
  13. import com.bwei.www.yuekaomoni.eventbusevent.MessageEvent;  
  14. import com.bwei.www.yuekaomoni.eventbusevent.PriceAndCountEvent;  
  15. import com.bwei.www.yuekaomoni.present.PresentCar;  
  16.   
  17. import org.greenrobot.eventbus.EventBus;  
  18. import org.greenrobot.eventbus.Subscribe;  
  19.   
  20. import java.util.ArrayList;  
  21. import java.util.List;  
  22.   
  23. public class Main2Activity extends AppCompatActivity implements IView {  
  24.   
  25.     private ExpandableListView elv;  
  26.     ArrayList<GsonBean.DataBean> dataBeen = new ArrayList<>();  
  27.     private CheckBox checkquan;  
  28.     private List<GsonBean.DataBean.ListBean> lists;  
  29.     ArrayList<List<GsonBean.DataBean.ListBean>> listBean = new ArrayList<>();  
  30.     private StarExpanderAdapter adapter;  
  31.     private TextView tv_price;  
  32.     private TextView tv_num;  
  33.     private PresentCar presentCar;  
  34.   
  35.     @Override  
  36.     protected void onCreate(Bundle savedInstanceState) {  
  37.         super.onCreate(savedInstanceState);  
  38.         setContentView(R.layout.activity_main2);  
  39.         //注册一下  
  40.         EventBus.getDefault().register(this);  
  41.         //获取控件  
  42.         elv = (ExpandableListView) findViewById(R.id.elv);  
  43.         checkquan = (CheckBox) findViewById(R.id.checkbox2);  
  44.         tv_price = (TextView) findViewById(R.id.tv_price);  
  45.         tv_num = (TextView) findViewById(R.id.tv_num);  
  46.         //获取数据  
  47.         presentCar = new PresentCar();  
  48.         presentCar.attachView(this);  
  49.         presentCar.getNews();  
  50.         checkquan.setOnClickListener(new View.OnClickListener() {  
  51.             @Override  
  52.             public void onClick(View view) {  
  53.                 //设置全选  
  54.                 adapter.changeAllListCbState(checkquan.isChecked());  
  55.             }  
  56.         });  
  57.   
  58.     }  
  59.     //销毁方法  
  60.     @Override  
  61.     protected void onDestroy() {  
  62.         super.onDestroy();  
  63.         //eventBus销毁  
  64.         EventBus.getDefault().unregister(this);  
  65.         if (presentCar!=null){  
  66.             presentCar.detachView();  
  67.         }  
  68.   
  69.     }  
  70.     @Override  
  71.     public void Success(String tag, List<GsonBean.DataBean> list) {  
  72.        dataBeen.addAll(list);  
  73.         for (int i = 0; i < dataBeen.size(); i++) {  
  74.             GsonBean.DataBean bean = dataBeen.get(i);  
  75.             lists = bean.getList();  
  76.             listBean.add(lists);  
  77.         }  
  78.   
  79.   
  80.         adapter = new StarExpanderAdapter(Main2Activity.this,dataBeen,listBean);  
  81.         elv.setAdapter(adapter);  
  82.         //隐藏二级列表前小三角  
  83.         elv.setGroupIndicator(null);  
  84.         //使二级列表一直展示  
  85.         for (int i = 0; i <dataBeen.size() ; i++) {  
  86.             elv.expandGroup(i);  
  87.         }  
  88.   
  89.     }  
  90.   
  91.     @Override  
  92.     public void Failed(String tag, Exception e) {  
  93.   
  94.     }  
  95.     //必写注解  
  96.     @Subscribe  
  97.     public void onMessageEvent(MessageEvent event) {  
  98.         checkquan.setChecked(event.isChecked());  
  99.     }  
  100.   
  101.     @Subscribe  
  102.     public void onMessageEvent(PriceAndCountEvent event) {  
  103.         tv_num.setText("结算(" + event.getCount() + ")");  
  104.         tv_price.setText(event.getPrice() + "");  
  105.     }  
  106. }  
还有一个 自定义View:

[html] view plain copy
  1. package com.bwei.www.yuekaomoni.myView;  
  2.   
  3. import android.content.Context;  
  4. import android.support.annotation.Nullable;  
  5. import android.util.AttributeSet;  
  6. import android.util.Log;  
  7. import android.view.View;  
  8. import android.widget.Button;  
  9. import android.widget.EditText;  
  10. import android.widget.LinearLayout;  
  11.   
  12. import com.bwei.www.yuekaomoni.R;  
  13.   
  14. /**  
  15.  * @作者   
  16.  * @时间 2017/11/18 09:56  
  17.  */  
  18.   
  19. public class MyView extends LinearLayout{  
  20.     private OnAddDelClickLstener lister;  
  21.     private Button btn_add;  
  22.     private Button txtAdd;  
  23.     private EditText et_et;  
  24.     private Button btn_del;  
  25.   
  26.     //定义一个对外开放的接口  
  27.     public interface OnAddDelClickLstener{  
  28.         void onAddClick(View v);  
  29.         void onDelClick(View v);  
  30.     }  
  31.     public void setOnAddDelClickLstener(OnAddDelClickLstener lister){  
  32.         if(lister!=null){  
  33.             this.lister = lister;  
  34.         }  
  35.     }  
  36.     public MyView(Context context) {  
  37.         this(context,null);  
  38.     }  
  39.   
  40.     public MyView(Context context, @Nullable AttributeSet attrs) {  
  41.         this(context, attrs,0);  
  42.     }  
  43.   
  44.     public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {  
  45.         super(context, attrs, defStyleAttr);  
  46.         initView(context, attrs, defStyleAttr);  
  47.     }  
  48.   
  49.     private void initView(Context context, AttributeSet attrs, int defStyleAttr) {  
  50.         View.inflate(context, R.layout.myview,this);  
  51.         btn_add = (Button) findViewById(R.id.btn_add);  
  52.         btn_del = (Button) findViewById(R.id.btn_del);  
  53.         et_et = (EditText) findViewById(R.id.et_et);  
  54.   
  55.         btn_del.setOnClickListener(new OnClickListener() {  
  56.             @Override  
  57.             public void onClick(View view) {  
  58.                 Log.e("AddDeleteView","点击了减号");  
  59.                 lister.onDelClick(view);  
  60.             }  
  61.         });  
  62.         btn_add.setOnClickListener(new OnClickListener() {  
  63.             @Override  
  64.             public void onClick(View view) {  
  65.                 Log.e("txtAdd","点击了加号");  
  66.                 lister.onAddClick(view);  
  67.             }  
  68.         });  
  69.   
  70.     }  
  71.   
  72.   
  73. }  

阅读全文
0 0
原创粉丝点击