欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Rxjava+Retrofit+MVP实现购物车功能

程序员文章站 2023-12-16 23:57:10
本文实例为大家分享了rxjava retrofit实现购物车的具体代码,供大家参考,具体内容如下 效果图: 1.依赖 annotationproces...

本文实例为大家分享了rxjava retrofit实现购物车的具体代码,供大家参考,具体内容如下

效果图:

Rxjava+Retrofit+MVP实现购物车功能

Rxjava+Retrofit+MVP实现购物车功能

1.依赖

annotationprocessor 'com.jakewharton:butterknife-compiler:8.8.1' 
 compile 'com.jakewharton:butterknife:8.8.1' 
 compile 'com.android.support:recyclerview-v7:26.0.0-alpha1' 
 compile 'com.squareup.retrofit2:retrofit:2.3.0' 
 compile 'com.squareup.retrofit2:converter-gson:2.3.0' 
 compile 'com.facebook.fresco:fresco:0.12.0' 
 compile 'com.facebook.fresco:animated-base-support:0.12.0' 
 compile 'com.facebook.fresco:animated-webp:0.12.0' 
 compile 'com.facebook.fresco:webpsupport:0.12.0' 
 compile 'io.reactivex.rxjava2:rxjava:2.1.7' 
 compile 'com.squareup.retrofit2:adapter-rxjava2:2.3.0' 
 compile 'io.reactivex.rxjava2:rxandroid:2.0.1' 
 compile 'com.squareup.okhttp3:okhttp:3.9.0' 
 compile 'com.google.code.gson:gson:2.8.1' 

2.布局

1.加减器布局   layout_add_delete

<?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="horizontal" 
 android:weightsum="1"> 
 
 <textview 
 android:id="@+id/txt_delete" 
 android:layout_width="30dp" 
 android:layout_height="30dp" 
 android:text="减" 
 android:gravity="center" 
 android:background="#8b948b"/> 
 
 <edittext 
 android:id="@+id/et_number" 
 android:layout_margintop="2dp" 
 android:layout_width="50dp" 
 android:layout_height="30dp" 
 android:background="@drawable/edit" 
 android:layout_weight="0.00" 
 android:gravity="center" 
 android:text="1"/> 
 <textview 
 android:id="@+id/txt_add" 
 android:layout_width="30dp" 
 android:layout_height="30dp" 
 android:text="加" 
 android:gravity="center" 
 android:background="#8b948b"/> 
 
 
</linearlayout> 

2. 商品详情布局   activity_main

<?xml version="1.0" encoding="utf-8"?> 
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" 
 xmlns:fresco="http://schemas.android.com/apk/res-auto" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent"> 
 
 
 <linearlayout 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 android:orientation="vertical"> 
 
 <com.facebook.drawee.view.simpledraweeview 
  android:id="@+id/my_image_view" 
  android:layout_width="match_parent" 
  android:layout_height="450dp" 
  fresco:placeholderimage="@mipmap/ic_launcher" /> 
 
 <textview 
  android:id="@+id/goods_title" 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:layout_margin="10dp" 
  android:text="a啊啊是你的" 
  android:textsize="20dp" /> 
 
 <textview 
  android:id="@+id/goods_price" 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:layout_margin="10dp" 
  android:text="¥ 18132" 
  android:textcolor="#dc5f2e" 
  android:textsize="18dp" /> 
 
 <textview 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:layout_margin="10dp" 
  android:text="我是商家19" 
  android:textsize="16dp" /> 
 
 
 </linearlayout> 
 
 <linearlayout 
 android:layout_alignparentbottom="true" 
 android:layout_width="match_parent" 
 android:layout_height="50dp" 
 android:orientation="horizontal"> 
 
 <button 
  android:id="@+id/btn_addcart" 
  android:layout_weight="1" 
  android:layout_width="0dp" 
  android:layout_height="match_parent" 
  android:background="#ffc207" 
  android:text="加入购物车"/> 
 
 <button 
  android:id="@+id/btn_shopping" 
  android:layout_weight="1" 
  android:layout_width="0dp" 
  android:layout_height="match_parent" 
  android:background="#ff6b06" 
  android:text="立即购买"/> 
 
 </linearlayout> 
 
 
</relativelayout> 

3.购物车布局 activity_main2

<?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"> 
 
 <!--头布局--> 
 <linearlayout 
 android:id="@+id/top_bar" 
 android:layout_width="match_parent" 
 android:layout_height="48dp" 
 android:background="#f7f7f7" 
 android:orientation="vertical" > 
 
 <relativelayout 
  android:layout_width="match_parent" 
  android:layout_height="48dp" 
  android:background="@android:color/transparent" 
  android:orientation="vertical" > 
 
  <imageview 
  android:id="@+id/back" 
  android:layout_width="48dp" 
  android:layout_height="48dp" 
  android:layout_alignparentleft="true" 
  android:layout_gravity="center_vertical" 
  android:padding="12dp" 
  android:src="@drawable/back" /> 
 
  <textview 
  android:layout_width="match_parent" 
  android:layout_height="wrap_content" 
  android:gravity="center" 
  android:minheight="48dp" 
  android:text="购物车" 
  android:textcolor="#1a1a1a" 
  android:textsize="16sp" /> 
 
  <textview 
  android:id="@+id/edit" 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:layout_alignparentright="true" 
  android:layout_marginright="40dp" 
  android:gravity="center" 
  android:minheight="48dp" 
  android:text="编辑" 
  android:textcolor="#1a1a1a" 
  android:textsize="14sp" 
  android:visibility="visible" /> 
 </relativelayout> 
 </linearlayout> 
 
 <expandablelistview 
 android:id="@+id/exlistview" 
 android:layout_width="match_parent" 
 android:layout_height="0dp" 
 android:layout_weight="1" 
 android:childindicator="@null" 
 android:groupindicator="@null" > 
 </expandablelistview> 
 
 <linearlayout 
 android:layout_width="match_parent" 
 android:layout_height="50dp" 
 android:gravity="center_vertical" 
 android:orientation="horizontal" > 
 
 <checkbox 
  android:id="@+id/all_chekbox" 
  android:layout_marginleft="20dp" 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" /> 
 <textview 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:text="全选"/> 
 <linearlayout 
  android:id="@+id/ll_info" 
  android:layout_width="0dp" 
  android:layout_height="wrap_content" 
  android:layout_weight="4" 
  > 
  <linearlayout 
  android:layout_width="match_parent" 
  android:layout_height="wrap_content" 
  android:orientation="vertical" 
  android:layout_marginright="20dp" 
  android:layout_weight="1" 
  > 
  <linearlayout 
   android:layout_width="match_parent" 
   android:layout_height="wrap_content" 
   android:orientation="horizontal" 
   android:gravity="right" 
   > 
   <textview 
   android:layout_width="wrap_content" 
   android:layout_height="wrap_content" 
   android:layout_marginleft="5dp" 
   android:text="合计:" 
   android:textsize="18sp" 
   android:textstyle="bold" /> 
 
   <textview 
   android:id="@+id/total_price" 
   android:layout_width="wrap_content" 
   android:layout_height="wrap_content" 
   android:text="¥0.00" 
   android:textcolor="#f23232" 
   android:textsize="16sp" 
   android:textstyle="bold" /> 
  </linearlayout> 
  <textview 
   android:id="@+id/total_number" 
   android:layout_width="match_parent" 
   android:layout_height="wrap_content" 
   android:text="共有商品:0件" 
   android:gravity="right" 
   android:textsize="16sp" 
   android:textstyle="bold" /> 
  </linearlayout> 
  <textview 
  android:id="@+id/tv_go_to_pay" 
  android:layout_width="match_parent" 
  android:layout_height="match_parent" 
  android:layout_weight="3" 
  android:background="#fd7a05" 
  android:clickable="true" 
  android:gravity="center" 
  android:text="结算" 
  android:textcolor="#fafafa" 
 
  /> 
  <textview 
  android:id="@+id/tv_go_to_del" 
  android:layout_width="match_parent" 
  android:layout_height="match_parent" 
  android:layout_weight="3" 
  android:background="#e2231c" 
  android:clickable="true" 
  android:gravity="center" 
  android:text="删除" 
  android:textcolor="#fafafa" 
  android:visibility="gone" 
  /> 
 </linearlayout> 
 
 </linearlayout> 
</linearlayout> 

4.二级列表组级布局 ex_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:background="#cfc3c3" 
 android:orientation="horizontal"> 
 
 <checkbox 
 android:id="@+id/group_checkbox" 
 android:layout_marginleft="20dp" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:focusable="false"/> 
 
 <textview 
 android:id="@+id/shop_name" 
 android:layout_marginleft="20dp" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:textsize="16dp" /> 
 
</linearlayout> 

5.二级列表子布局  ex_child_item

<?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:fresco="http://schemas.android.com/apk/res-auto" 
 android:layout_width="match_parent" 
 android:layout_height="230dp" 
 android:orientation="horizontal"> 
 
 <checkbox 
 android:id="@+id/child_checkbox" 
 android:layout_margintop="50dp" 
 android:layout_marginleft="20dp" 
 android:layout_marginbottom="50dp" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 /> 
 
 <relativelayout 
 android:layout_marginleft="20dp" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent"> 
 
 <textview 
  android:id="@+id/shop_title" 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:layout_alignparentleft="true" 
  android:layout_alignparentstart="true" 
  android:layout_marginleft="17dp" 
  android:layout_marginstart="17dp" 
  android:text="textview" 
  android:layout_alignparenttop="true" /> 
 
 <!--<imageview 
  android:id="@+id/shop_img" 
  android:layout_width="90dp" 
  android:layout_height="90dp" 
  android:layout_margintop="30dp" 
  app:srccompat="@mipmap/ic_launcher" 
  android:layout_below="@+id/shop_name" 
  android:layout_alignparentleft="true" 
  android:layout_alignparentstart="true" />--> 
 <com.facebook.drawee.view.simpledraweeview 
  android:id="@+id/shop_img" 
  android:layout_width="90dp" 
  android:layout_height="90dp" 
  android:layout_margintop="30dp" 
  fresco:placeholderimage="@mipmap/ic_launcher" 
  android:layout_below="@+id/shop_name" 
  android:layout_alignparentleft="true" 
  android:layout_alignparentstart="true" 
  /> 
 
 <textview 
  android:id="@+id/shop_price" 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:layout_aligntop="@+id/shop_img" 
  android:layout_centerhorizontal="true" 
  android:layout_margintop="10dp" 
  android:text="¥20" 
  android:textcolor="#f23232"/> 
 
 <com.bwei.gepeng.myview.adddeleteview 
  android:id="@+id/adv" 
  android:layout_width="160dp" 
  android:layout_height="30dp" 
  android:layout_below="@+id/shop_price" 
  android:layout_margintop="30dp" 
  android:layout_marginleft="140dp" 
  app:left_text="-" 
  app:right_text="+" 
  app:middle_text="1" 
  android:focusable="false" 
  > 
 </com.bwei.gepeng.myview.adddeleteview> 
 
 <button 
  android:id="@+id/shop_delete" 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:layout_alignparentend="true" 
  android:layout_alignparentright="true" 
  android:layout_centervertical="true" 
  android:visibility="invisible" 
  android:text="删除" /> 
 </relativelayout> 
 
 
</linearlayout> 

3.实体类

1. messagebean

package com.bwei.gepeng.entity; 
 
public class messagebean <t>{ 
 
 
 private string code; 
 private t data; 
 private string msg; 
 private sellerbean seller; 
 
 public string getcode() { 
 return code; 
 } 
 
 public void setcode(string code) { 
 this.code = code; 
 } 
 
 public t getdata() { 
 return data; 
 } 
 
 public void setdata(t data) { 
 this.data = data; 
 } 
 
 public string getmsg() { 
 return msg; 
 } 
 
 public void setmsg(string msg) { 
 this.msg = msg; 
 } 
 
 public sellerbean getseller() { 
 return seller; 
 } 
 
 public void setseller(sellerbean seller) { 
 this.seller = seller; 
 } 
 
 
 
 public static class sellerbean { 
 /** 
  * description : 我是商家19 
  * icon : http://120.27.23.105/images/icon.png 
  * name : 商家19 
  * productnums : 999 
  * score : 5.0 
  * sellerid : 19 
  */ 
 
 private string description; 
 private string icon; 
 private string name; 
 private int productnums; 
 private double score; 
 private int sellerid; 
 
 public string getdescription() { 
  return description; 
 } 
 
 public void setdescription(string description) { 
  this.description = description; 
 } 
 
 public string geticon() { 
  return icon; 
 } 
 
 public void seticon(string icon) { 
  this.icon = icon; 
 } 
 
 public string getname() { 
  return name; 
 } 
 
 public void setname(string name) { 
  this.name = name; 
 } 
 
 public int getproductnums() { 
  return productnums; 
 } 
 
 public void setproductnums(int productnums) { 
  this.productnums = productnums; 
 } 
 
 public double getscore() { 
  return score; 
 } 
 
 public void setscore(double score) { 
  this.score = score; 
 } 
 
 public int getsellerid() { 
  return sellerid; 
 } 
 
 public void setsellerid(int sellerid) { 
  this.sellerid = sellerid; 
 } 
 } 
} 

2.goodsshowbean

package com.bwei.gepeng.entity; 
 
public class goodsshowbean { 
 
 private double bargainprice; 
 private string createtime; 
 private string detailurl; 
 private string images; 
 private int itemtype; 
 private int pid; 
 private double price; 
 private int pscid; 
 private int salenum; 
 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 getitemtype() { 
  return itemtype; 
 } 
 
 public void setitemtype(int itemtype) { 
  this.itemtype = itemtype; 
 } 
 
 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 getsalenum() { 
  return salenum; 
 } 
 
 public void setsalenum(int salenum) { 
  this.salenum = salenum; 
 } 
 
 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; 
 } 
 
 @override 
 public string tostring() { 
 return "goodsshowbean{" + 
  "bargainprice=" + bargainprice + 
  ", createtime='" + createtime + '\'' + 
  ", detailurl='" + detailurl + '\'' + 
  ", images='" + images + '\'' + 
  ", itemtype=" + itemtype + 
  ", pid=" + pid + 
  ", price=" + price + 
  ", pscid=" + pscid + 
  ", salenum=" + salenum + 
  ", sellerid=" + sellerid + 
  ", subhead='" + subhead + '\'' + 
  ", title='" + title + '\'' + 
  '}'; 
 } 
} 

3. cartbean

package com.bwei.gepeng.entity; 
 
import java.util.list; 
 
public class cartbean { 
 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; 
 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; 
 } 
 } 
 
 @override 
 public string tostring() { 
 return "cartbean{" + 
  "sellername='" + sellername + '\'' + 
  ", sellerid='" + sellerid + '\'' + 
  ", list=" + list + 
  '}'; 
 } 
} 

4. groupbean

package com.bwei.gepeng.entity; 
 
public class groupbean { 
 private string sellername; 
 private boolean gropucb; 
 
 public groupbean(string sellername, boolean gropucb) { 
 this.sellername = sellername; 
 this.gropucb = gropucb; 
 } 
 
 public string getsellername() { 
 return sellername; 
 } 
 
 public void setsellername(string sellername) { 
 this.sellername = sellername; 
 } 
 
 public boolean isgropucb() { 
 return gropucb; 
 } 
 
 public void setgropucb(boolean gropucb) { 
 this.gropucb = gropucb; 
 } 
 
 @override 
 public string tostring() { 
 return "groupbean{" + 
  "sellername='" + sellername + '\'' + 
  ", gropucb=" + gropucb + 
  '}'; 
 } 
} 

5.childbean

package com.bwei.gepeng.entity; 
 
public class childbean { 
 private string title; 
 private string images; 
 private double price; 
 private int num; 
 private boolean childcb; 
 private boolean btn; 
 private int pid; 
 
 
 public childbean(string title, string images, double price, int num, boolean childcb, boolean btn, int pid) { 
 this.title = title; 
 this.images = images; 
 this.price = price; 
 this.num = num; 
 this.childcb = childcb; 
 this.btn = btn; 
 this.pid = pid; 
 } 
 
 public int getpid() { 
 return pid; 
 } 
 
 public void setpid(int pid) { 
 this.pid = pid; 
 } 
 
 public boolean isbtn() { 
 return btn; 
 } 
 
 public void setbtn(boolean btn) { 
 this.btn = btn; 
 } 
 
 
 public string gettitle() { 
 return title; 
 } 
 
 public void settitle(string title) { 
 this.title = title; 
 } 
 
 public string getimages() { 
 return images; 
 } 
 
 public void setimages(string images) { 
 this.images = images; 
 } 
 
 public double getprice() { 
 return price; 
 } 
 
 public void setprice(double price) { 
 this.price = price; 
 } 
 
 public int getnum() { 
 return num; 
 } 
 
 public void setnum(int num) { 
 this.num = num; 
 } 
 
 public boolean ischildcb() { 
 return childcb; 
 } 
 
 public void setchildcb(boolean childcb) { 
 this.childcb = childcb; 
 } 
 
 @override 
 public string tostring() { 
 return "childbean{" + 
  "title='" + title + '\'' + 
  ", images='" + images + '\'' + 
  ", price=" + price + 
  ", num=" + num + 
  ", childcb=" + childcb + 
  ", btn=" + btn + 
  ", pid=" + pid + 
  '}'; 
 } 
} 

4.接口

1.apiservice

package com.bwei.gepeng.http; 
 
 
import com.bwei.gepeng.entity.cartbean; 
import com.bwei.gepeng.entity.goodsshowbean; 
import com.bwei.gepeng.entity.messagebean; 
 
import java.util.list; 
import java.util.map; 
 
import io.reactivex.flowable; 
import retrofit2.http.get; 
import retrofit2.http.querymap; 
 
public interface apiservice { 
 //查询商品详情75 
 //http://120.27.23.105/product/getproductdetail?pid=75&source=android 
 @get("product/getproductdetail") 
 flowable<messagebean<goodsshowbean>> getnews(@querymap map<string,string> map); 
 
 //查询购物车 
 //http://120.27.23.105/product/getcarts?uid=3802&source=android 
 @get("product/getcarts") 
 flowable<messagebean<list<cartbean>>> getcart(@querymap map<string,string> map); 
 
 
 //删除购物车 
 //http://120.27.23.105/product/getcarts?uid=3802&source=android 
 @get("product/deletecart") 
 flowable<messagebean<list<cartbean>>> getdel(@querymap map<string,string> map); 
 
 //添加购物车 
 //http://120.27.23.105/product/addcart?uid=3802&pid=75&source=android 
 @get("product/addcart") 
 flowable<messagebean<list<cartbean>>> getadd(@querymap map<string,string> map); 
} 
2.iview
[java] view plain copy
public interface iview { 
 void onsuccess(object o,string tag); 
 void onfailed(exception e,string tag); 
} 
3.ipresenter
[java] view plain copy
import java.util.map; 
 
public interface ipresenter { 
 void getdata(map<string,string> map, string tag); 
} 
4.imodel
[java] view plain copy
import java.util.map; 
 
public interface imodel { 
 void getdata(map<string,string> map,string tag); 
} 

5.代码

1.自定义view 加减器 adddeleteview

package com.bwei.gepeng.myview; 
 
import android.content.context; 
import android.content.res.typedarray; 
import android.graphics.color; 
import android.util.attributeset; 
import android.view.view; 
import android.widget.edittext; 
import android.widget.linearlayout; 
import android.widget.textview; 
 
import com.bwei.gepeng.r; 
 
public class adddeleteview extends linearlayout { 
 private onadddelclicklistener listener; 
 private edittext etnumber; 
 
 //对外提供一个点击的回调接口 
 public interface onadddelclicklistener{ 
 void onaddclick(view v); 
 void ondelclick(view v); 
 } 
 
 public void setonadddelclicklistener(onadddelclicklistener listener){ 
 if(listener!=null){ 
  this.listener=listener; 
 } 
 } 
 
 public adddeleteview(context context) { 
 this(context,null); 
 } 
 
 public adddeleteview(context context, attributeset attrs) { 
 this(context, attrs,0); 
 } 
 
 public adddeleteview(context context, attributeset attrs, int defstyleattr) { 
 super(context, attrs, defstyleattr); 
 initview(context, attrs, defstyleattr); 
 } 
 
 private void initview(context context, attributeset attrs, int defstyleattr) { 
 view.inflate(context, r.layout.layout_add_delete,this); 
 
 //获取控件 
 textview txtdelete=findviewbyid(r.id.txt_delete); 
 textview txtadd=findviewbyid(r.id.txt_add); 
 etnumber = findviewbyid(r.id.et_number); 
 
 
 typedarray typedarray = context.obtainstyledattributes(attrs, r.styleable.adddeleteviewstyle); 
 
 string lefttext = typedarray.getstring(r.styleable.adddeleteviewstyle_left_text); 
 string righttext = typedarray.getstring(r.styleable.adddeleteviewstyle_right_text); 
 string middletext = typedarray.getstring(r.styleable.adddeleteviewstyle_middle_text); 
 int color = typedarray.getcolor(r.styleable.adddeleteviewstyle_left_text_color, color.red); 
 
 txtdelete.settext(lefttext); 
 txtadd.settext(righttext); 
 etnumber.settext(middletext); 
 txtdelete.settextcolor(color); 
 
 //回收 
 typedarray.recycle(); 
 
 
 txtdelete.setonclicklistener(new onclicklistener() { 
  @override 
  public void onclick(view view) { 
  listener.ondelclick(view); 
  } 
 }); 
 
 txtadd.setonclicklistener(new onclicklistener() { 
  @override 
  public void onclick(view view) { 
  listener.onaddclick(view); 
  } 
 }); 
 
 } 
 //对外提供一个修改数字的方法 
 public void setnumber(int number){ 
 if(number>0){ 
  etnumber.settext(number+""); 
 } 
 } 
 //对外提供一个获取当前数字的方法 
 public int getnumber(){ 
 string string = etnumber.gettext().tostring(); 
 int i = integer.parseint(string); 
 return i; 
 } 
 
} 

2.retrofit工具类 retrofitutils

package com.bwei.gepeng.http; 
 
import java.io.ioexception; 
 
import okhttp3.httpurl; 
import okhttp3.interceptor; 
import okhttp3.okhttpclient; 
import okhttp3.request; 
import okhttp3.response; 
import retrofit2.retrofit; 
import retrofit2.adapter.rxjava2.rxjava2calladapterfactory; 
import retrofit2.converter.gson.gsonconverterfactory; 
 
public class retrofitutils { 
 private static volatile retrofitutils instance; 
 private final retrofit retrofit; 
 
 private retrofitutils(){ 
 okhttpclient client = new okhttpclient.builder().addinterceptor(new logger()).build(); 
 
 retrofit = new retrofit.builder() 
  .client(client) 
  .addcalladapterfactory(rxjava2calladapterfactory.create()) 
  .addconverterfactory(gsonconverterfactory.create()) 
  .baseurl("http://120.27.23.105/") 
  .build(); 
 } 
 
 public static retrofitutils getinstance(){ 
 if(instance==null){ 
  synchronized (retrofitutils.class){ 
  if(instance==null){ 
   instance=new retrofitutils(); 
  } 
  } 
 } 
 return instance; 
 } 
 
 public apiservice getapiservice(){ 
 apiservice apiservice = retrofit.create(apiservice.class); 
 return apiservice; 
 } 
 
 class logger implements interceptor { 
 @override 
 public response intercept(chain chain) throws ioexception { 
  request original = chain.request(); 
  httpurl url=original.url().newbuilder() 
   .addqueryparameter("source","android") 
   .build(); 
  //添加请求头 
  request request = original.newbuilder() 
   .url(url) 
   .build(); 
  return chain.proceed(request); 
 } 
 } 
} 

3.m层

package com.bwei.gepeng.model; 
 
import com.bwei.gepeng.entity.cartbean; 
import com.bwei.gepeng.entity.goodsshowbean; 
import com.bwei.gepeng.entity.messagebean; 
import com.bwei.gepeng.http.retrofitutils; 
import com.bwei.gepeng.presenter.newspresenter; 
 
import java.util.list; 
import java.util.map; 
 
import io.reactivex.flowable; 
 
public class model implements imodel{ 
 
 private newspresenter presenter; 
 public model(newspresenter presenter) { 
 this.presenter = presenter; 
 } 
 
 @override 
 public void getdata(map<string, string> map, string tag) { 
 if(tag.equals("cart")){ 
  flowable<messagebean<list<cartbean>>> flowable = retrofitutils.getinstance().getapiservice().getcart(map); 
  presenter.get2(flowable,tag); 
 }else if(tag.equals("goods")){ 
  flowable<messagebean<goodsshowbean>> flowable = retrofitutils.getinstance().getapiservice().getnews(map); 
  presenter.get(flowable,tag); 
 }else if(tag.equals("del")){ 
  flowable<messagebean<list<cartbean>>> flowable = retrofitutils.getinstance().getapiservice().getdel(map); 
  presenter.get3(flowable,tag); 
 }else if(tag.equals("add")){ 
  flowable<messagebean<list<cartbean>>> flowable = retrofitutils.getinstance().getapiservice().getadd(map); 
  presenter.get4(flowable,tag); 
 } 
 
 } 
} 

4.p层

package com.bwei.gepeng.presenter; 
 
import com.bwei.gepeng.entity.cartbean; 
import com.bwei.gepeng.entity.goodsshowbean; 
import com.bwei.gepeng.entity.messagebean; 
import com.bwei.gepeng.model.model; 
import com.bwei.gepeng.view.iview; 
 
import java.util.list; 
import java.util.map; 
 
import io.reactivex.flowable; 
import io.reactivex.android.schedulers.androidschedulers; 
import io.reactivex.schedulers.schedulers; 
import io.reactivex.subscribers.disposablesubscriber; 
 
public class newspresenter implements ipresenter{ 
 
 private iview iview; 
 private disposablesubscriber<messagebean<goodsshowbean>> subscriber; 
 private disposablesubscriber<messagebean<list<cartbean>>> subscriber2; 
 private disposablesubscriber<messagebean<list<cartbean>>> subscriber3; 
 private disposablesubscriber<messagebean<list<cartbean>>> subscriber4; 
 
 public void attachview(iview iview){ 
 this.iview=iview; 
 } 
 
 @override 
 public void getdata(map<string, string> map, string tag) { 
 model model = new model(this); 
 model.getdata(map,tag); 
 } 
 
 public void detachview(){ 
 if(iview!=null){ 
  iview=null; 
 } 
 if(subscriber!=null){ 
  if(!subscriber.isdisposed()){ 
  subscriber.dispose(); 
  } 
 } 
 if(subscriber2!=null){ 
  if(!subscriber2.isdisposed()){ 
  subscriber2.dispose(); 
  } 
 } 
 if(subscriber3!=null){ 
  if(!subscriber3.isdisposed()){ 
  subscriber3.dispose(); 
  } 
 } 
 if(subscriber4!=null){ 
  if(!subscriber4.isdisposed()){ 
  subscriber4.dispose(); 
  } 
 } 
 } 
 
 //查询购物车 
 public void get(flowable<messagebean<goodsshowbean>> flowable , final string tag) { 
 subscriber = flowable.subscribeon(schedulers.io()) 
  .observeon(androidschedulers.mainthread()) 
  .subscribewith(new disposablesubscriber<messagebean<goodsshowbean>>() { 
   @override 
   public void onnext(messagebean<goodsshowbean> listmessagebean) { 
   if (listmessagebean != null) { 
    goodsshowbean data = listmessagebean.getdata(); 
    iview.onsuccess(data,tag); 
   } 
   } 
 
   @override 
   public void onerror(throwable t) { 
   iview.onfailed(new exception(t),tag); 
   } 
 
   @override 
   public void oncomplete() { 
 
   } 
  }); 
 } 
 
 //查询商品详情75 
 public void get2(flowable<messagebean<list<cartbean>>> flowable, final string tag) { 
 subscriber2 = flowable.subscribeon(schedulers.io()) 
  .observeon(androidschedulers.mainthread()) 
  .subscribewith(new disposablesubscriber<messagebean<list<cartbean>>>() { 
   @override 
   public void onnext(messagebean<list<cartbean>> list) { 
   if (list != null) { 
    list<cartbean> data = list.getdata(); 
    if(data!=null){ 
    iview.onsuccess(data,tag); 
    } 
   } 
   } 
 
   @override 
   public void onerror(throwable t) { 
   iview.onfailed(new exception(t),tag); 
   } 
 
   @override 
   public void oncomplete() { 
 
   } 
  }); 
 } 
 
 //删除购物车 
 public void get3(flowable<messagebean<list<cartbean>>> flowable, final string tag) { 
 subscriber3 = flowable.subscribeon(schedulers.io()) 
  .observeon(androidschedulers.mainthread()) 
  .subscribewith(new disposablesubscriber<messagebean<list<cartbean>>>() { 
   @override 
   public void onnext(messagebean<list<cartbean>> list) { 
   if (list != null) { 
    string code = list.getmsg(); 
    iview.onsuccess(code,tag); 
   } 
   } 
 
   @override 
   public void onerror(throwable t) { 
   iview.onfailed(new exception(t),tag); 
   } 
 
   @override 
   public void oncomplete() { 
 
   } 
  }); 
 } 
 
 //添加购物车 
 public void get4(flowable<messagebean<list<cartbean>>> flowable, final string tag) { 
 subscriber4 = flowable.subscribeon(schedulers.io()) 
  .observeon(androidschedulers.mainthread()) 
  .subscribewith(new disposablesubscriber<messagebean<list<cartbean>>>() { 
   @override 
   public void onnext(messagebean<list<cartbean>> list) { 
   if (list != null) { 
    string code = list.getmsg(); 
    iview.onsuccess(code,tag); 
   } 
   } 
 
   @override 
   public void onerror(throwable t) { 
   iview.onfailed(new exception(t),tag); 
   } 
 
   @override 
   public void oncomplete() { 
 
   } 
  }); 
 } 
} 

5.mainactivity 商品详情页面

package com.bwei.gepeng.view; 
 
import android.content.intent; 
import android.os.bundle; 
import android.support.v7.app.appcompatactivity; 
import android.view.view; 
import android.widget.textview; 
import android.widget.toast; 
 
import com.bwei.gepeng.r; 
import com.bwei.gepeng.entity.goodsshowbean; 
import com.bwei.gepeng.presenter.newspresenter; 
import com.facebook.drawee.view.simpledraweeview; 
 
import java.util.hashmap; 
import java.util.map; 
 
import butterknife.bindview; 
import butterknife.butterknife; 
import butterknife.onclick; 
 
public class mainactivity extends appcompatactivity implements iview { 
 
 @bindview(r.id.my_image_view) 
 simpledraweeview myimageview; 
 @bindview(r.id.goods_title) 
 textview goodstitle; 
 @bindview(r.id.goods_price) 
 textview goodsprice; 
 private newspresenter presenter; 
 
 @override 
 protected void oncreate(bundle savedinstancestate) { 
 super.oncreate(savedinstancestate); 
 setcontentview(r.layout.activity_main); 
 butterknife.bind(this); 
 
 presenter = new newspresenter(); 
 presenter.attachview(this); 
 //http://120.27.23.105/product/getproductdetail?pid=75&source=android 
 map<string, string> map = new hashmap<>(); 
 map.put("pid", "75"); 
 presenter.getdata(map, "goods"); 
  
 } 
 
 @override 
 public void onsuccess(object o, string tag) { 
 if (tag.equals("goods")) { 
  if (o != null) { 
  goodsshowbean data = (goodsshowbean) o; 
  //log.e("gp", "onsuccess: " + data.tostring()); 
 
  myimageview.setimageuri(data.getimages().split("\\|")[0]); 
  goodstitle.settext(data.gettitle()); 
  goodsprice.settext("¥ "+data.getprice()); 
  } 
 }else if(tag.equals("add")){ 
  if (o != null) { 
  string msg = (string) o; 
  toast.maketext(mainactivity.this, msg, toast.length_short).show(); 
  } 
 } 
 } 
 
 @override 
 public void onfailed(exception e, string tag) { 
 
 } 
 
 @onclick({r.id.btn_addcart, r.id.btn_shopping}) 
 public void onviewclicked(view view) { 
 switch (view.getid()) { 
  case r.id.btn_addcart: 
  map<string, string> map = new hashmap<>(); 
  map.put("uid", "3802"); 
  map.put("pid", "75"); 
  presenter.getdata(map, "add"); 
  //toast.maketext(mainactivity.this,"添加成功",toast.length_short).show(); 
  break; 
  case r.id.btn_shopping: 
  intent intent=new intent(mainactivity.this,main2activity.class); 
  startactivity(intent); 
  break; 
 } 
 } 
} 

6.二级列表适配器 expandableadapter

package com.bwei.gepeng.adapter; 
 
import android.content.context; 
import android.view.view; 
import android.view.viewgroup; 
import android.widget.baseexpandablelistadapter; 
import android.widget.button; 
import android.widget.checkbox; 
import android.widget.textview; 
import android.widget.toast; 
 
import com.bwei.gepeng.r; 
import com.bwei.gepeng.entity.childbean; 
import com.bwei.gepeng.entity.groupbean; 
import com.bwei.gepeng.myview.adddeleteview; 
import com.bwei.gepeng.view.main2activity; 
import com.facebook.drawee.view.simpledraweeview; 
 
import java.util.arraylist; 
import java.util.list; 
 
 
public class expandableadapter extends baseexpandablelistadapter { 
 private context context; 
 private list<groupbean> groupbeen=new arraylist<>(); 
 private list<list<childbean>> childbeen=new arraylist<>(); 
 
 private main2activity main; 
 
 public expandableadapter(context context, list<groupbean> groupbeen, list<list<childbean>> childbeen) { 
 this.context = context; 
 this.groupbeen = groupbeen; 
 this.childbeen = childbeen; 
 this.main = (main2activity) context; 
 } 
 //接口回调 
 private ondeletegoods ondlegoods; 
 
 public interface ondeletegoods{ 
 void ondelgoods(int i,int i1,string pid); 
 } 
 
 public void setondeletegoods(ondeletegoods ondlegoods){ 
 this.ondlegoods=ondlegoods; 
 } 
 
 @override 
 public int getgroupcount() { 
 return groupbeen.size(); 
 } 
 
 @override 
 public int getchildrencount(int i) { 
 return childbeen.get(i).size(); 
 } 
 
 @override 
 public object getgroup(int i) { 
 return groupbeen.get(i).getsellername(); 
 } 
 
 @override 
 public object getchild(int i, int i1) { 
 return childbeen.get(i).get(i1).gettitle(); 
 } 
 
 @override 
 public long getgroupid(int i) { 
 return i; 
 } 
 
 @override 
 public long getchildid(int i, int i1) { 
 return i1; 
 } 
 
 @override 
 public boolean hasstableids() { 
 return false; 
 } 
 
 //一级组 
 @override 
 public view getgroupview(final int i, boolean b, view view, viewgroup viewgroup) { 
 //加载视图 
 view=view.inflate(context, r.layout.ex_group_item ,null); 
 
 final checkbox groupcb= view.findviewbyid(r.id.group_checkbox); 
 textview shopname= view.findviewbyid(r.id.shop_name); 
 
 
 shopname.settext(groupbeen.get(i).getsellername()); 
 groupcb.setchecked(groupbeen.get(i).isgropucb()); 
 
 //组复选按钮 
 groupcb.setonclicklistener(new view.onclicklistener() { 
  @override 
  public void onclick(view view) { 
  boolean gchecked = groupcb.ischecked(); 
  groupbeen.get(i).setgropucb(gchecked); 
  //main2activity main= (main2activity) context; 
  for(groupbean i: groupbeen){ 
   boolean gropucb = i.isgropucb(); 
   if(!gropucb){ 
   main.allcheckbox.setchecked(false); 
   break; 
   }else{ 
   main.allcheckbox.setchecked(true); 
   } 
  } 
  int size = childbeen.get(i).size(); 
  if(gchecked){ 
   for(int r=0;r<size;r++){ 
   //toast.maketext(context,"group按钮"+ gchecked+""+size, toast.length_short).show(); 
   childbeen.get(i).get(r).setchildcb(true); 
   } 
  }else{ 
   for(int r=0;r<size;r++){ 
   //toast.maketext(context,"group按钮"+ gchecked+""+size, toast.length_short).show(); 
   childbeen.get(i).get(r).setchildcb(false); 
   } 
  } 
  notifydatasetchanged(); 
  main.changesum(childbeen); 
  } 
 }); 
 return view; 
 } 
 
 //二级组 
 @override 
 public view getchildview(final int i, final int i1, boolean b, view v, viewgroup viewgroup) { 
 //加载视图 
 v=view.inflate(context, r.layout.ex_child_item ,null); 
 
 final checkbox childcb = v.findviewbyid(r.id.child_checkbox); 
 textview shoptitle= v.findviewbyid(r.id.shop_title); 
 textview shopprice= v.findviewbyid(r.id.shop_price); 
 //imageview shopimg=v.findviewbyid(r.id.shop_img); 
 //draweeview.setimageuri(uri); 
 simpledraweeview shopimg = v.findviewbyid(r.id.shop_img); 
 final adddeleteview adv = v.findviewbyid(r.id.adv); 
 button shop_delete=v.findviewbyid(r.id.shop_delete); 
 
 childcb.setchecked(childbeen.get(i).get(i1).ischildcb()); 
 string images = childbeen.get(i).get(i1).getimages(); 
 //glide.with(context).load(images).into(shopimg); 
 shopimg.setimageuri(images); 
 shoptitle.settext(childbeen.get(i).get(i1).gettitle()); 
 shopprice.settext(childbeen.get(i).get(i1).getprice()+""); 
 adv.setnumber(childbeen.get(i).get(i1).getnum()); 
 
 //final main2activity main= (main2activity) context; 
 //控制删除按钮的显隐 
 if(childbeen.get(i).get(i1).isbtn()){ 
  shop_delete.setvisibility(view.visible); 
 }else{ 
  shop_delete.setvisibility(view.invisible); 
 } 
 //删除按钮监听 
 shop_delete.setonclicklistener(new view.onclicklistener() { 
  @override 
  public void onclick(view view) { 
 
  ondlegoods.ondelgoods(i,i1,childbeen.get(i).get(i1).getpid()+""); 
  } 
 }); 
 
 //加减器逻辑 
 adv.setonadddelclicklistener(new adddeleteview.onadddelclicklistener() { 
  @override 
  public void onaddclick(view v) { 
  int number = adv.getnumber(); 
  number++; 
  adv.setnumber(number); 
  childbeen.get(i).get(i1).setnum(number); 
  main.changesum(childbeen); 
  } 
 
  @override 
  public void ondelclick(view v) { 
  int number = adv.getnumber(); 
  if(number==1){ 
   toast.maketext(context,"用户最小数量为1",toast.length_short).show(); 
  } 
  number--; 
  adv.setnumber(number); 
  childbeen.get(i).get(i1).setnum(number); 
  main.changesum(childbeen); 
  } 
 }); 
 
 //二级组的复选框监听 
 childcb.setonclicklistener(new view.onclicklistener() { 
  @override 
  public void onclick(view view) { 
  boolean flag=false; 
  boolean cchecked = childcb.ischecked(); 
  childbeen.get(i).get(i1).setchildcb(cchecked); 
  //toast.maketext(context,"child按钮"+ cchecked+""+i1, toast.length_short).show(); 
  // main2activity main= (main2activity) context; 
  for (list<childbean> i1:childbeen){ 
   for(int r=0;r<i1.size();r++) { 
   boolean childcb1 = i1.get(r).ischildcb(); 
   if(!childcb1){ 
    main.allcheckbox.setchecked(false); 
    groupbeen.get(i).setgropucb(false); 
    flag=true; 
    break; 
   }else{ 
    main.allcheckbox.setchecked(true); 
    groupbeen.get(i).setgropucb(true); 
   } 
   } 
   if(flag){ 
   break; 
   } 
  } 
 
  int size = childbeen.get(i).size(); 
  for(int x=0;x<size;x++) { 
   boolean childcb1 = childbeen.get(i).get(x).ischildcb(); 
   if(!childcb1){ 
   groupbeen.get(i).setgropucb(false); 
   break; 
   }else{ 
   groupbeen.get(i).setgropucb(true); 
   } 
  } 
  notifydatasetchanged(); 
  main.changesum(childbeen); 
  } 
 }); 
 
 
 return v; 
 } 
 
 @override 
 public boolean ischildselectable(int i, int i1) { 
 return false; 
 } 
} 

7.购物车页面逻辑 main2activity

package com.bwei.gepeng.view; 
 
import android.os.bundle; 
import android.support.v7.app.appcompatactivity; 
import android.view.view; 
import android.widget.checkbox; 
import android.widget.expandablelistview; 
import android.widget.textview; 
 
import com.bwei.gepeng.r; 
import com.bwei.gepeng.adapter.expandableadapter; 
import com.bwei.gepeng.entity.cartbean; 
import com.bwei.gepeng.entity.childbean; 
import com.bwei.gepeng.entity.groupbean; 
import com.bwei.gepeng.presenter.newspresenter; 
 
import java.util.arraylist; 
import java.util.hashmap; 
import java.util.list; 
import java.util.map; 
 
import butterknife.bindview; 
import butterknife.butterknife; 
import butterknife.onclick; 
 
public class main2activity extends appcompatactivity implements iview { 
 
 @bindview(r.id.exlistview) 
 expandablelistview exlistview; 
 @bindview(r.id.all_chekbox) 
 public checkbox allcheckbox; 
 @bindview(r.id.total_price) 
 textview totalprice; 
 @bindview(r.id.total_number) 
 textview totalnumber; 
 @bindview(r.id.tv_go_to_pay) 
 textview tvgotopay; 
 @bindview(r.id.tv_go_to_del) 
 textview tvgotodel; 
 
 private expandableadapter expandableadapter; 
 
 private boolean flagedit = true; 
 private boolean flagdel = false; 
 
 
 private newspresenter presenter; 
 
 list<groupbean> groupbeen = new arraylist<>(); 
 list<list<childbean>> childbeen = new arraylist<>(); 
 
 int i; 
 int i1; 
 
 @override 
 protected void oncreate(bundle savedinstancestate) { 
 super.oncreate(savedinstancestate); 
 setcontentview(r.layout.activity_main2); 
 butterknife.bind(this); 
 
 //获取二级列表适配器 
 expandableadapter = new expandableadapter(main2activity.this, groupbeen, childbeen); 
 exlistview.setadapter(expandableadapter); 
 
 exlistview.setongroupclicklistener(new expandablelistview.ongroupclicklistener() { 
  @override 
  public boolean ongroupclick(expandablelistview expandablelistview, view view, int i, long l) { 
  return true; 
  } 
 }); 
 
 
 presenter = new newspresenter(); 
 presenter.attachview(this); 
 //http://120.27.23.105/product/getcarts?uid=3802&source=android 
 map<string, string> map = new hashmap<>(); 
 map.put("uid", "3802"); 
 presenter.getdata(map, "cart"); 
 
 
 expandableadapter.setondeletegoods(new expandableadapter.ondeletegoods() { 
  @override 
  public void ondelgoods(int i, int i1, string pid) { 
  main2activity.this.i = i; 
  main2activity.this.i1 = i1; 
 
  map<string, string> map = new hashmap<>(); 
  map.put("uid", "3802"); 
  map.put("pid", pid); 
  presenter.getdata(map, "del"); 
 
  } 
 }); 
 
 } 
 
 
 @override 
 public void onsuccess(object o, string tag) { 
 if (tag.equals("cart")) { 
  if (o != null) { 
  list<cartbean> data = (list<cartbean>) o; 
  for (cartbean i : data) { 
   groupbean groupbean = new groupbean(i.getsellername(), false); 
   this.groupbeen.add(groupbean); 
   list<cartbean.listbean> list = i.getlist(); 
   list<childbean> ls = new arraylist<>(); 
   for (cartbean.listbean w : list) { 
   string[] split = w.getimages().split("\\|"); 
   childbean childbean = new childbean(w.gettitle(), split[0], w.getprice(), 1, false, false, w.getpid()); 
   ls.add(childbean); 
   } 
   this.childbeen.add(ls); 
 
  } 
  for (int i = 0; i < expandableadapter.getgroupcount(); i++) { 
   exlistview.expandgroup(i); 
  } 
  } 
 } else if (tag.equals("del")) { 
  if (o != null) { 
  string msg = (string) o; 
 
  if (this.i != -1 && this.i1 != -1) { 
   int size = childbeen.get(i).size(); 
   if (size == 1) { 
   childbeen.remove(i); 
   groupbeen.remove(i); 
   } else { 
   childbeen.get(i).remove(i1); 
   } 
   
   changesum(childbeen); 
 
   this.i = -1; 
   this.i1 = -1; 
   if(flagdel){ 
   delgoods(); 
   } 
  } 
  } 
 } 
 expandableadapter.notifydatasetchanged(); 
 } 
 
 @override 
 public void onfailed(exception e, string tag) { 
  
 } 
 
 @onclick({r.id.back, r.id.edit, r.id.all_chekbox,r.id.tv_go_to_del}) 
 public void onviewclicked(view view) { 
 switch (view.getid()) { 
  case r.id.back: 
  finish(); 
  break; 
  case r.id.edit: 
  if(flagedit){ 
   tvgotopay.setvisibility(view.gone); 
   tvgotodel.setvisibility(view.visible); 
  }else{ 
   tvgotopay.setvisibility(view.visible); 
   tvgotodel.setvisibility(view.gone); 
  } 
 
  for (list<childbean> i1 : childbeen) { 
   for (int r = 0; r < i1.size(); r++) { 
   i1.get(r).setbtn(flagedit); 
   } 
  } 
  flagedit = !flagedit; 
  expandableadapter.notifydatasetchanged(); 
  break; 
  case r.id.all_chekbox: 
  boolean checked = allcheckbox.ischecked(); 
  //改变一级item复选框 
  for (groupbean i : groupbeen) { 
   i.setgropucb(checked); 
  } 
  //改变二级item复选框 
  for (list<childbean> i1 : childbeen) { 
   for (int r = 0; r < i1.size(); r++) { 
   i1.get(r).setchildcb(checked); 
   } 
  } 
  expandableadapter.notifydatasetchanged(); 
  changesum(childbeen); 
  break; 
  case r.id.tv_go_to_del: 
  if(childbeen.size()!=0){ 
   for (list<childbean> i1 : childbeen) { 
   for (int r = 0; r < i1.size(); r++) { 
    boolean childcb1 = i1.get(r).ischildcb(); 
    if (childcb1) { 
    flagdel=true; 
    delgoods(); 
    if(allcheckbox.ischecked()){ 
     allcheckbox.setchecked(false); 
    } 
    break; 
    } 
   } 
   if(flagdel){ 
    break; 
   } 
   } 
  } 
  //toast.maketext(main2activity.this,flagdel+"",toast.length_short).show(); 
  break; 
 } 
 } 
 
 //递归删除 
 private void delgoods(){ 
 boolean flag=false; 
 for(int p=0;p<childbeen.size();p++) { 
  for (int r = 0; r < childbeen.get(p).size(); r++) { 
  boolean childcb1 = childbeen.get(p).get(r).ischildcb(); 
  if(p==childbeen.size()-1&&r==childbeen.get(p).size()-1){ 
   flagdel=false; 
  } 
  if (childcb1) { 
   int pid = childbeen.get(p).get(r).getpid(); 
   this.i = p; 
   this.i1 = r; 
   map<string, string> map = new hashmap<>(); 
   map.put("uid", "3802"); 
   map.put("pid", pid+""); 
   presenter.getdata(map, "del"); 
   flag=!flag; 
   break; 
  } 
  } 
  if(flag){ 
  break; 
  } 
 } 
 } 
 
 //计算和数量总价 
 public void changesum(list<list<childbean>> childbeen) { 
 int count = 0; 
 double sum = 0; 
 for (list<childbean> i1 : childbeen) { 
  for (int r = 0; r < i1.size(); r++) { 
  boolean childcb1 = i1.get(r).ischildcb(); 
  if (childcb1) { 
   double price = i1.get(r).getprice(); 
   int num = i1.get(r).getnum(); 
   sum += price * num; 
   count++; 
  } 
  } 
 } 
 totalprice.settext("¥" + sum); 
 totalnumber.settext("共有商品:" + count + "件"); 
 } 
} 

6.附

shape图 edit

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
 <solid android:color="#ffffff" /> 
 <!--<corners android:radius="3dip"/>--> 
 <stroke 
 android:width="1dip" 
 android:color="#bdc7d8" /> 
</shape> 

application 初始化fresco

import android.app.application; 
import com.facebook.drawee.backends.pipeline.fresco; 
 
public class app extends application{ 
 @override 
 public void oncreate() { 
 super.oncreate(); 
 fresco.initialize(this); 
 } 
} 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

上一篇:

下一篇: