本篇文章为大家展示了怎么在RxJava中使用Retrofit和Mvp实现购物车,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
框架结构:
1.项目框架:MVP,图片加载用Fresco,网络请求用OKhttp+Retrofit实现(自己封装,加单例模式),
2.完成购物车数据添加(如果接口无数据,可用接口工具添加数据),
3.自定义view实现加减按钮,每次点击加减,item中的总数及总价要做出相应的改变。
4.当数量为1时,点击减号,数量不变,吐司提示用户最小数量为1。
5.底部总数及总价为所有item项中的总价及总数,每个item中数量价格的更改,底部总价总数要与之联动
6.实现单选反选全选功能,首次进入默认全选,item未选中时总数及总价不计入底部数据,改变选中状态时,底部总数及总价能做出正确修改
7.点击删除按钮,删除item,底部总数及总价能做出正确修改,接口数据删除。
添加所需要的依赖和联网权限
compile 'com.squareup.retrofit2:retrofit:2.0.0-beta4'//Retrofit2所需要的包 compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta4'//ConverterFactory的Gson依赖包 compile 'com.squareup.retrofit2:converter-scalars:2.0.0-beta4'//ConverterFactory的String依赖包 compile 'io.reactivex.rxjava2:rxjava:2.1.3' compile 'io.reactivex.rxjava2:rxandroid:2.0.1' compile 'com.squareup.retrofit2:adapter-rxjava2:2.3.0' compile 'de.greenrobot:eventbus:3.0.0-beta1' compile 'com.facebook.fresco:fresco:0.11.0'
<uses-permission android:name="android.permission.INTERNET" />
在使用MVP模式前先分包
net包
API
public class API { public static String HOST="http://120.27.23.105/product/"; }
ServiceAPI
public interface ServiceAPI { @GET("getProductDetail") Flowable<XQBean> getXQ(@Query("pid")int pid, @Query("source")String str); @GET("addCart") Flowable<AddCart> getMsg(@Query("uid")int uid, @Query("pid")int pid, @Query("source")String str); @GET("getCarts") Flowable<CartBean> getCart(@Query("uid")int uid, @Query("source")String str); }
成功的接口
public interface OnNetLisenter<T> { void Success(T t); }
Retrofit的封装
public class RetrofitHepler { public static OkHttpClient okHttpClient; public static ServiceAPI serViceAPI; /** * 优先执行 */ static { getOkHttpClient(); } public static OkHttpClient getOkHttpClient(){ if(okHttpClient==null){ synchronized (OkHttpClient.class){ if(okHttpClient==null){ okHttpClient=new OkHttpClient(); } } } return okHttpClient; } public static ServiceAPI getSerViceAPI(){ if(serViceAPI==null){ synchronized (OkHttpClient.class){ if(serViceAPI==null){ serViceAPI=onCreate(ServiceAPI.class,API.HOST); } } } return serViceAPI; } public static <T> T onCreate(Class<T>tClass,String url){ Retrofit retrofit = new Retrofit.Builder() .baseUrl(url) .addConverterFactory(GsonConverterFactory.create()) .client(okHttpClient) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) //.addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .build(); return retrofit.create(tClass); } }
bean包
详情页的Bean类
public class XQBean { /** * msg : * seller : {"description":"我是商家2","icon":"http://120.27.23.105/images/icon.png","name":"商家2","productNums":999,"score":5,"sellerid":2} * code : 0 * data : {"bargainPrice":111.99,"createtime":"2017-10-14T21:48:08","detailUrl":"https://item.m.jd.com/product/4719303.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t9004/210/1160833155/647627/ad6be059/59b4f4e1N9a2b1532.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7504/338/63721388/491286/f5957f53/598e95f1N7f2adb87.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7441/10/64242474/419246/adb30a7d/598e95fbNd989ba0a.jpg!q70.jpg","itemtype":2,"pid":9,"price":78.99,"pscid":1,"salenum":2356,"sellerid":2,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"} */ private String msg; private SellerBean seller; private String code; private DataBean 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 String getCode() { return code; } public void setCode(String code) { this.code = code; } public DataBean getData() { return data; } public void setData(DataBean data) { this.data = data; } public static class SellerBean { /** * description : 我是商家2 * icon : http://120.27.23.105/images/icon.png * name : 商家2 * productNums : 999 * score : 5.0 * sellerid : 2 */ 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; } } public static class DataBean { /** * bargainPrice : 111.99 * createtime : 2017-10-14T21:48:08 * detailUrl : https://item.m.jd.com/product/4719303.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends * images : https://m.360buyimg.com/n0/jfs/t9004/210/1160833155/647627/ad6be059/59b4f4e1N9a2b1532.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7504/338/63721388/491286/f5957f53/598e95f1N7f2adb87.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7441/10/64242474/419246/adb30a7d/598e95fbNd989ba0a.jpg!q70.jpg * itemtype : 2 * pid : 9 * price : 78.99 * pscid : 1 * salenum : 2356 * sellerid : 2 * subhead : 每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下 * title : 北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g */ 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; } } }
加入购物车的Bean
public class AddCart { private String msg; private String code; public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } }
商品类
public class CartBean { /** * msg : 请求成功 * code : 0 * data : [{"list":[{"bargainPrice":22.9,"createtime":"2017-10-14T21:48:08","detailUrl":"https://item.m.jd.com/product/2542855.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t1930/284/2865629620/390243/e3ade9c4/56f0a08fNbd3a1235.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t2137/336/2802996626/155915/e5e90d7a/56f0a09cN33e01bd0.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t1882/31/2772215910/389956/c8dbf370/56f0a0a2Na0c86ea6.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t2620/166/2703833710/312660/531aa913/57709035N33857877.jpg!q70.jpg","num":1,"pid":24,"price":288,"pscid":2,"selected":0,"sellerid":1,"subhead":"三只松鼠零食特惠,专区满99减50,满199减100,火速抢购》","title":"三只松鼠 坚果炒货 零食奶油味 碧根果225g/袋"},{"bargainPrice":11800,"createtime":"2017-10-10T17:33:37","detailUrl":"https://item.m.jd.com/product/4338107.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t6700/155/2098998076/156185/6cf95035/595dd5a5Nc3a7dab5.jpg!q70.jpg","num":2,"pid":57,"price":5199,"pscid":40,"selected":0,"sellerid":1,"subhead":"【i5 MX150 2G显存】全高清窄边框 8G内存 256固态硬盘 支持指纹识别 预装WIN10系统","title":"小米(MI)Air 13.3英寸全金属轻薄笔记本(i5-7200U 8G 256G PCle SSD MX150 2G独显 FHD 指纹识别 Win10)银\r\n"},{"bargainPrice":99,"createtime":"2017-10-14T21:38:26","detailUrl":"https://item.m.jd.com/product/4345173.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t6037/35/2944615848/95178/6cd6cff0/594a3a10Na4ec7f39.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t6607/258/1025744923/75738/da120a2d/594a3a12Ne3e6bc56.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t6370/292/1057025420/64655/f87644e3/594a3a12N5b900606.jpg!q70.jpg","num":1,"pid":45,"price":2999,"pscid":39,"selected":0,"sellerid":1,"subhead":"高清双摄,就是清晰!2000+1600万高清摄像头,6GB大内存+高通骁龙835处理器,性能怪兽!","title":"一加手机5 (A5000) 6GB+64GB 月岩灰 全网通 双卡双待 移动联通电信4G手机"}],"sellerName":"商家1","sellerid":"1"},{"list":[{"bargainPrice":6666,"createtime":"2017-10-10T16:01:31","detailUrl":"https://item.m.jd.com/product/5089273.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t8284/363/1326459580/71585/6d3e8013/59b857f2N6ca75622.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t9346/182/1406837243/282106/68af5b54/59b8480aNe8af7f5c.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t8434/54/1359766007/56140/579509d9/59b85801Nfea207db.jpg!q70.jpg","num":1,"pid":46,"price":234,"pscid":39,"selected":0,"sellerid":2,"subhead":"【iPhone新品上市】新一代iPhone,让智能看起来更不一样","title":"Apple iPhone 8 Plus (A1864) 64GB 金色 移动联通电信4G手机"}],"sellerName":"商家2","sellerid":"2"},{"list":[{"bargainPrice":111.99,"createtime":"2017-10-03T23:43:53","detailUrl":"https://item.m.jd.com/product/4719303.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t9004/210/1160833155/647627/ad6be059/59b4f4e1N9a2b1532.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7504/338/63721388/491286/f5957f53/598e95f1N7f2adb87.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7441/10/64242474/419246/adb30a7d/598e95fbNd989ba0a.jpg!q70.jpg","num":5,"pid":12,"price":256,"pscid":1,"selected":0,"sellerid":5,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"}],"sellerName":"商家5","sellerid":"5"},{"list":[{"bargainPrice":11800,"createtime":"2017-10-03T23:53:28","detailUrl":"https://mitem.jd.hk/ware/view.action?wareId=1988853309&cachekey=1acb07a701ece8d2434a6ae7fa6870a1","images":"https://m.360buyimg.com/n0/jfs/t6130/97/1370670410/180682/1109582a/593276b1Nd81fe723.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5698/110/2617517836/202970/c9388feb/593276b7Nbd94ef1f.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5698/110/2617517836/202970/c9388feb/593276b7Nbd94ef1f.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5815/178/2614671118/51656/7f52d137/593276c7N107b725a.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5878/60/2557817477/30873/4502b606/593276caN5a7d6357.jpg!q70.jpg","num":1,"pid":62,"price":15999,"pscid":40,"selected":0,"sellerid":6,"subhead":"购买电脑办公部分商品满1元返火车票5元优惠券(返完即止)","title":"全球购 新款Apple MacBook Pro 苹果笔记本电脑 银色VP2新13英寸Bar i5/8G/256G"}],"sellerName":"商家6","sellerid":"6"},{"list":[{"bargainPrice":399,"createtime":"2017-10-03T23:53:28","detailUrl":"https://item.m.jd.com/product/1439822107.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t5887/201/859509257/69994/6bde9bf6/59224c24Ne854e14c.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5641/233/853609022/57374/5c73d281/59224c24N3324d5f4.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5641/233/853609022/57374/5c73d281/59224c24N3324d5f4.jpg!q70.jpg","num":1,"pid":86,"price":777,"pscid":85,"selected":0,"sellerid":7,"subhead":"满2件,总价打6.50折","title":"Gap男装 休闲舒适简约水洗五袋直筒长裤紧身牛仔裤941825 深灰色 33/32(175/84A)"}],"sellerName":"商家7","sellerid":"7"},{"list":[{"bargainPrice":111.99,"createtime":"2017-10-14T21:39:05","detailUrl":"https://item.m.jd.com/product/4719303.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t9004/210/1160833155/647627/ad6be059/59b4f4e1N9a2b1532.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7504/338/63721388/491286/f5957f53/598e95f1N7f2adb87.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7441/10/64242474/419246/adb30a7d/598e95fbNd989ba0a.jpg!q70.jpg","num":12,"pid":1,"price":118,"pscid":1,"selected":0,"sellerid":17,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"}],"sellerName":"商家17","sellerid":"17"},{"list":[{"bargainPrice":111.99,"createtime":"2017-10-14T21:39:05","detailUrl":"https://item.m.jd.com/product/4719303.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t9004/210/1160833155/647627/ad6be059/59b4f4e1N9a2b1532.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7504/338/63721388/491286/f5957f53/598e95f1N7f2adb87.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7441/10/64242474/419246/adb30a7d/598e95fbNd989ba0a.jpg!q70.jpg","num":4,"pid":2,"price":299,"pscid":1,"selected":0,"sellerid":18,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"}],"sellerName":"商家18","sellerid":"18"},{"list":[{"bargainPrice":11800,"createtime":"2017-10-03T23:53:28","detailUrl":"https://mitem.jd.hk/ware/view.action?wareId=1988853309&cachekey=1acb07a701ece8d2434a6ae7fa6870a1","images":"https://m.360buyimg.com/n0/jfs/t6130/97/1370670410/180682/1109582a/593276b1Nd81fe723.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5698/110/2617517836/202970/c9388feb/593276b7Nbd94ef1f.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5698/110/2617517836/202970/c9388feb/593276b7Nbd94ef1f.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5815/178/2614671118/51656/7f52d137/593276c7N107b725a.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5878/60/2557817477/30873/4502b606/593276caN5a7d6357.jpg!q70.jpg","num":1,"pid":76,"price":37999.99,"pscid":40,"selected":0,"sellerid":20,"subhead":"购买电脑办公部分商品满1元返火车票5元优惠券(返完即止)","title":"全球购 新款Apple MacBook Pro 苹果笔记本电脑 银色VP2新13英寸Bar i5/8G/256G"}],"sellerName":"商家20","sellerid":"20"},{"list":[{"bargainPrice":111.99,"createtime":"2017-10-14T21:39:05","detailUrl":"https://item.m.jd.com/product/4719303.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t9004/210/1160833155/647627/ad6be059/59b4f4e1N9a2b1532.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7504/338/63721388/491286/f5957f53/598e95f1N7f2adb87.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7441/10/64242474/419246/adb30a7d/598e95fbNd989ba0a.jpg!q70.jpg","num":1,"pid":5,"price":88.99,"pscid":1,"selected":0,"sellerid":21,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"}],"sellerName":"商家21","sellerid":"21"}] */ private String msg; private String code; private List<DataBean> data; public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public List<DataBean> getData() { return data; } public void setData(List<DataBean> data) { this.data = data; } public static class DataBean { /** * list : [{"bargainPrice":22.9,"createtime":"2017-10-14T21:48:08","detailUrl":"https://item.m.jd.com/product/2542855.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t1930/284/2865629620/390243/e3ade9c4/56f0a08fNbd3a1235.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t2137/336/2802996626/155915/e5e90d7a/56f0a09cN33e01bd0.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t1882/31/2772215910/389956/c8dbf370/56f0a0a2Na0c86ea6.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t2620/166/2703833710/312660/531aa913/57709035N33857877.jpg!q70.jpg","num":1,"pid":24,"price":288,"pscid":2,"selected":0,"sellerid":1,"subhead":"三只松鼠零食特惠,专区满99减50,满199减100,火速抢购》","title":"三只松鼠 坚果炒货 零食奶油味 碧根果225g/袋"},{"bargainPrice":11800,"createtime":"2017-10-10T17:33:37","detailUrl":"https://item.m.jd.com/product/4338107.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t6700/155/2098998076/156185/6cf95035/595dd5a5Nc3a7dab5.jpg!q70.jpg","num":2,"pid":57,"price":5199,"pscid":40,"selected":0,"sellerid":1,"subhead":"【i5 MX150 2G显存】全高清窄边框 8G内存 256固态硬盘 支持指纹识别 预装WIN10系统","title":"小米(MI)Air 13.3英寸全金属轻薄笔记本(i5-7200U 8G 256G PCle SSD MX150 2G独显 FHD 指纹识别 Win10)银\r\n"},{"bargainPrice":99,"createtime":"2017-10-14T21:38:26","detailUrl":"https://item.m.jd.com/product/4345173.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t6037/35/2944615848/95178/6cd6cff0/594a3a10Na4ec7f39.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t6607/258/1025744923/75738/da120a2d/594a3a12Ne3e6bc56.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t6370/292/1057025420/64655/f87644e3/594a3a12N5b900606.jpg!q70.jpg","num":1,"pid":45,"price":2999,"pscid":39,"selected":0,"sellerid":1,"subhead":"高清双摄,就是清晰!2000+1600万高清摄像头,6GB大内存+高通骁龙835处理器,性能怪兽!","title":"一加手机5 (A5000) 6GB+64GB 月岩灰 全网通 双卡双待 移动联通电信4G手机"}] * sellerName : 商家1 * sellerid : 1 */ private String sellerName; private String sellerid; private List<ListBean> list; private boolean check; public boolean isCheck() { return check; } public void setCheck(boolean check) { this.check = check; } public String getSellerName() { return sellerName; } public void setSellerName(String sellerName) { this.sellerName = sellerName; } public String getSellerid() { return sellerid; } public void setSellerid(String sellerid) { this.sellerid = sellerid; } public List<ListBean> getList() { return list; } public void setList(List<ListBean> list) { this.list = list; } public static class ListBean { /** * bargainPrice : 22.9 * createtime : 2017-10-14T21:48:08 * detailUrl : https://item.m.jd.com/product/2542855.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends * images : https://m.360buyimg.com/n0/jfs/t1930/284/2865629620/390243/e3ade9c4/56f0a08fNbd3a1235.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t2137/336/2802996626/155915/e5e90d7a/56f0a09cN33e01bd0.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t1882/31/2772215910/389956/c8dbf370/56f0a0a2Na0c86ea6.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t2620/166/2703833710/312660/531aa913/57709035N33857877.jpg!q70.jpg * num : 1 * pid : 24 * price : 288.0 * pscid : 2 * selected : 0 * sellerid : 1 * subhead : 三只松鼠零食特惠,专区满99减50,满199减100,火速抢购》 * title : 三只松鼠 坚果炒货 零食奶油味 碧根果225g/袋 */ private double bargainPrice; private String createtime; private String detailUrl; private String images; private int num; private int pid; private double price; private int pscid; private int selected; private int sellerid; private String subhead; private String title; private int cou=1; public int getCou() { return cou; } public void setCou(int cou) { this.cou = cou; } private boolean check; public boolean isCheck() { return check; } public void setCheck(boolean check) { this.check = check; } 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; } } } }
app
public class MyApp extends Application{ @Override public void onCreate() { super.onCreate(); Fresco.initialize(this); } }
view层
先写两个接口
public interface ICart { void showlist(List<CartBean.DataBean> grouplist, List<List<CartBean.DataBean.ListBean>> childlist); }
public interface IView<T> { void showBean(T t); void showStr(String s); }
主页面布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.bwie.zhangzhen20170108.MainActivity"> <ExpandableListView android:id="@+id/elv" android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <LinearLayout android:layout_weight="9" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <CheckBox android:id="@+id/quanxuan" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:text="全选" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <LinearLayout android:layout_marginLeft="50dp" android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:id="@+id/zongjia" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="总价:0.0" /> <TextView android:text="共0件商品" android:id="@+id/tv_count" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> <TextView android:text="去结算" android:background="#FFD82020" android:layout_marginLeft="100dp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> </LinearLayout>
主页面
public class MainActivity extends AppCompatActivity implements ICart { private ExpandableListView mElv; private CheckBox mQuanxuan; private TextView mZongjia; private TextView mTvCount; private MyExpandableAdapter myExpandableAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); new CartPresenter(this).getCart(); EventBus.getDefault().register(this); mQuanxuan.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { myExpandableAdapter.qx(mQuanxuan.isChecked()); } }); } private void initView() { mElv = (ExpandableListView) findViewById(R.id.elv); mQuanxuan = (CheckBox) findViewById(R.id.quanxuan); mZongjia = (TextView) findViewById(R.id.zongjia); mTvCount = (TextView) findViewById(R.id.tv_count); } @Override public void showlist(List<CartBean.DataBean> grouplist, List<List<CartBean.DataBean.ListBean>> childlist) { myExpandableAdapter = new MyExpandableAdapter(grouplist, this,childlist); mElv.setAdapter(myExpandableAdapter); for (int i = 0; i <grouplist.size() ; i++) { mElv.expandGroup(i); } } //接收传过来的值 @Subscribe public void onPriceAndCount(CountAndPrice cp){ mZongjia.setText("共"+cp.getCount()+"件商品"); mTvCount.setText("总计:"+cp.getPrice()); } //接收传过来的值 @Subscribe public void onPriceAndCount1(MessgeEvent event){ //改变全选的状态 mQuanxuan.setChecked(event.isCheck()); } @Override protected void onDestroy() { super.onDestroy(); EventBus.getDefault().unregister(this); } }
第二个页面布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:id="@+id/activity_add" tools:context="com.bwie.zhangzhen20170108.AddActivity"> <com.facebook.drawee.view.SimpleDraweeView android:layout_width="300dp" android:id="@+id/xqsdv1" android:layout_gravity="center" android:layout_height="300dp" /> <TextView android:id="@+id/tvtitle1" android:layout_width="match_parent" android:layout_height="wrap_content" /> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:id="@+id/tiaozhuan1" android:layout_weight="1" android:text="跳转到购物车" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/tianjia1" android:layout_weight="1" android:text="添加到购物车" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout> </LinearLayout>
实现购物车的代码
public class AddActivity extends AppCompatActivity implements View.OnClickListener,IView { private SimpleDraweeView mXqsdv; private TextView mTvtitle; private Button mTiaozhuan; private Button mTianjia; private LinearLayout mAddActivity; private XQPresenter xqPresenter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_add); initView(); xqPresenter=new XQPresenter(this); xqPresenter.getXQ(); } private void initView() { mXqsdv = (SimpleDraweeView) findViewById(R.id.xqsdv1); mTvtitle = (TextView) findViewById(R.id.tvtitle1); mTiaozhuan = (Button) findViewById(R.id.tiaozhuan1); mTiaozhuan.setOnClickListener(this); mTianjia = (Button) findViewById(R.id.tianjia1); mTianjia.setOnClickListener(this); mAddActivity = (LinearLayout) findViewById(R.id.activity_add); } @Override public void onClick(View v) { switch (v.getId()) { default: break; //跳到MainActivity (购物车) case R.id.tiaozhuan1: startActivity(new Intent(this,MainActivity.class)); break; case R.id.tianjia1: xqPresenter.getAdd(); break; } } @Override public void showBean(Object o) { XQBean xqBean = (XQBean) o; XQBean.DataBean data = xqBean.getData(); String s = data.getImages().split("\\|")[0]; Uri uri =Uri.parse(s);//地址转换成URI mXqsdv.setImageURI(uri); mTvtitle.setText(data.getTitle()+"\n"+data.getPrice()); } @Override public void showStr(String s) { Toast.makeText(this, s, Toast.LENGTH_SHORT).show(); } }
eventbus包
设置一个数量和价格的类
public class CountAndPrice { private int count ; private int price; public int getCount() { return count; } public void setCount(int count) { this.count = count; } public int getPrice() { return price; } public void setPrice(int price) { this.price = price; } }
EventBus
public class MessgeEvent { private boolean check; public boolean isCheck() { return check; } public void setCheck(boolean check) { this.check = check; } }
model
实现详情页model层的接口
public interface IXQModel { public void getXQ(int pid, String str, final OnNetLisenter<XQBean> cgsb); }
实现详情页model
public class XQModel implements IXQModel{ @Override public void getXQ(int pid, String str, final OnNetLisenter<XQBean> onNetLisenter) { Flowable<XQBean> flowable = RetrofitHepler.getSerViceAPI().getXQ(pid, str); flowable.observeOn(AndroidSchedulers.mainThread()) .subscribeOn(Schedulers.io()) .subscribe(new Consumer<XQBean>() { @Override public void accept(XQBean xqBean) throws Exception { onNetLisenter.Success(xqBean); } }); } public void getAdd(int uid, int pid, String str, final OnNetLisenter<AddCart> onNetLisenter){ Flowable<AddCart> flowable = RetrofitHepler.getSerViceAPI().getMsg(uid, pid, str); flowable.observeOn(AndroidSchedulers.mainThread()) .subscribeOn(Schedulers.io()) .subscribe(new Consumer<AddCart>() { @Override public void accept(AddCart addCart) throws Exception { onNetLisenter.Success(addCart); } }); } }
购物车的model
public class CartModel { public void getCart(int uid, String str, final OnNetLisenter<CartBean> onNetLisenter){ Flowable<CartBean> flowable = RetrofitHepler.getSerViceAPI().getCart(uid,str); flowable.observeOn(AndroidSchedulers.mainThread()) .subscribeOn(Schedulers.io()) .subscribe(new Consumer<CartBean>() { @Override public void accept(CartBean cartBean) throws Exception { onNetLisenter.Success(cartBean); } }); } }
presenter层
详情页的P层
public class XQPresenter { private final XQModel xqModel; private IView iView; public XQPresenter(IView iView) { xqModel = new XQModel(); this.iView=iView; } public void getXQ(){ xqModel.getXQ(12, "android", new OnNetLisenter<XQBean>() { @Override public void Success(XQBean xqBean) { iView.showBean(xqBean); } }); } public void getAdd(){ xqModel.getAdd(3043, 12, "android", new OnNetLisenter<AddCart>() { @Override public void Success(AddCart addCart) { iView.showStr(addCart.getMsg()); } }); } }
购物车的P层
public class CartPresenter { private ICart iCart; private final CartModel cartModel; public CartPresenter(ICart iCart) { this.iCart = iCart; cartModel = new CartModel(); } public void getCart(){ cartModel.getCart(3043,"android",new OnNetLisenter<CartBean>(){ @Override public void Success(CartBean cartBean) { List<CartBean.DataBean> grouplist = cartBean.getData(); List<List<CartBean.DataBean.ListBean>> childlist = new ArrayList<>(); for (int i = 0; i < grouplist.size(); i++) { CartBean.DataBean dataBean = grouplist.get(i); List<CartBean.DataBean.ListBean> list = dataBean.getList(); childlist.add(list); } iCart.showlist(grouplist,childlist); } }); } }
adapter
public class MyExpandableAdapter extends BaseExpandableListAdapter { private List<CartBean.DataBean> groupdata; private Context context; private List<List<CartBean.DataBean.ListBean>> childlist; public MyExpandableAdapter( List<CartBean.DataBean> groupdata, Context context, List<List<CartBean.DataBean.ListBean>> childlist) { this.groupdata = groupdata; this.context = context; this.childlist = childlist; } @Override public int getGroupCount() { return groupdata.size(); } @Override public int getChildrenCount(int groupPosition) { return childlist.get(groupPosition).size(); } @Override public Object getGroup(int groupPosition) { return groupdata.get(groupPosition); } @Override public Object getChild(int groupPosition, int childPosition) { return null; } @Override public long getGroupId(int groupPosition) { return groupPosition; } @Override public long getChildId(int groupPosition, int childPosition) { return childPosition; } @Override public boolean hasStableIds() { return false; } @Override public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { final GroupViewHolder holder; if (convertView == null) { convertView = View.inflate(context, R.layout.group_item, null); holder = new GroupViewHolder(); holder.tv_group = (TextView) convertView.findViewById(R.id.tv_group); holder.group_cb = (CheckBox) convertView.findViewById(R.id.gx_group); convertView.setTag(holder); } else { holder = (GroupViewHolder) convertView.getTag(); } final CartBean.DataBean dataBean = groupdata.get(groupPosition); String sellerName = dataBean.getSellerName(); holder.group_cb.setChecked(dataBean.isCheck()); //点击一级checkbox的状态 holder.group_cb.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { dataBean.setCheck(holder.group_cb.isChecked()); //改变所有孩子的状态 changeChildState(groupPosition,holder.group_cb.isChecked()); EventBus.getDefault().post(jS()); //通过判断一级的checkbox判断全选的状态 changeMianQXstatus(checkGroupAll()); notifyDataSetChanged(); } }); return convertView; } @Override public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { final ChildViewHolder holder; if (convertView == null) { convertView = View.inflate(context, R.layout.child_item, null); holder = new ChildViewHolder(); holder.tv_title= (TextView) convertView.findViewById(R.id.title); holder.price= (TextView) convertView.findViewById(R.id.price); holder.child_cb= (CheckBox) convertView.findViewById(R.id.gouxuan_child); holder.add= (TextView) convertView.findViewById(R.id.jia); holder.del= (TextView) convertView.findViewById(R.id.del); holder.jian= (TextView) convertView.findViewById(R.id.jian); holder.sdv= (SimpleDraweeView) convertView.findViewById(R.id.title_img); holder.tv_num= (TextView) convertView.findViewById(R.id.tv_num); holder.yanse= (TextView) convertView.findViewById(R.id.yanse); convertView.setTag(holder); } else { holder = (ChildViewHolder) convertView.getTag(); } final CartBean.DataBean.ListBean listBean = childlist.get(groupPosition).get(childPosition); int price = (int) listBean.getPrice(); holder.child_cb.setChecked(listBean.isCheck()); holder.price.setText("¥:"+price+""); String title = listBean.getTitle(); holder.tv_title.setText(title); String subhead = listBean.getSubhead(); String substring = subhead.substring(1, 10)+"....."; holder.yanse.setText(substring); String images = listBean.getImages(); String[] split = images.split("\\|"); String s = split[0]; Uri uri=Uri.parse(s); holder.sdv.setImageURI(uri); // listBean.setCou(Integer.parseInt(holder.tv_num.getText().toString())); //给子条目的checkBox点击事件 holder.child_cb.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { listBean.setCheck(holder.child_cb.isChecked()); EventBus.getDefault().post(jS()); //如果二级的checkBox选中,就让一级的checkBox勾选 if(holder.child_cb.isChecked()) { if (ischeckAllchild(groupPosition)) { //改变一级checkBox的状态 changeGroupstatus(groupPosition,true); //通过判断一级的CheckBox的状态来改变全选的状态 changeMianQXstatus(checkGroupAll()); } }else{ changeGroupstatus(groupPosition,false); changeMianQXstatus(checkGroupAll()); } notifyDataSetChanged(); } }); //点击加加的功能 holder.add.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int cou = listBean.getCou(); holder.tv_num.setText(++cou+""); listBean.setCou(cou); if(holder.child_cb.isChecked()){ EventBus.getDefault().post(jS()); } } }); //点击减减的功能 holder.jian.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int cou = listBean.getCou(); if(cou==1) { Toast.makeText(context, "最小数量为1", Toast.LENGTH_SHORT).show(); return;} holder.tv_num.setText(--cou+""); listBean.setCou(cou); if (holder.child_cb.isChecked()) { EventBus.getDefault().post(jS()); } } }); //点击删除 holder.del.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { List<CartBean.DataBean.ListBean> listBeen = childlist.get(groupPosition); CartBean.DataBean.ListBean remove = listBeen.remove(childPosition); if(listBeen.size()==0){ childlist.remove(childPosition); groupdata.remove(groupPosition); } EventBus.getDefault().post(jS()); notifyDataSetChanged(); } }); return convertView; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } class GroupViewHolder { CheckBox group_cb; TextView tv_group; } class ChildViewHolder { CheckBox child_cb; TextView tv_title,price,yanse,tv_num; TextView del,add,jian; SimpleDraweeView sdv; } /** * 判断全部的二级是CheckBox否选中 * */ private boolean ischeckAllchild(int g){ List<CartBean.DataBean.ListBean> listBeen = childlist.get(g); for (int i = 0; i <listBeen.size() ; i++) { CartBean.DataBean.ListBean listBean = listBeen.get(i); if(!listBean.isCheck()){ return false; } } return true; } /** * 计算数量和价格 * 通过遍历一级的得到二级的CheckBox,如果是选择的选中状态的话,就统计它们的总计的数量和价格 */ private CountAndPrice jS(){ int cou=0; int jiage=0; for (int i = 0; i <groupdata.size() ; i++) { for (int j = 0; j <childlist.get(i).size() ; j++) { CartBean.DataBean.ListBean listBean = childlist.get(i).get(j); if(listBean.isCheck()){ cou+=listBean.getCou(); jiage+=(int)listBean.getPrice()*listBean.getCou(); } } } CountAndPrice countAndPrice = new CountAndPrice(); countAndPrice.setCount(cou); countAndPrice.setPrice(jiage); return countAndPrice; } /** * 改变一级列表的状态 */ private void changeGroupstatus(int groupPosition,boolean flag ){ CartBean.DataBean dataBean = groupdata.get(groupPosition); dataBean.setCheck(flag); } /** * 改变全选的状态 * */ private void changeMianQXstatus(boolean flag){ MessgeEvent messgeEvent = new MessgeEvent(); messgeEvent.setCheck(flag); EventBus.getDefault().post(messgeEvent); } /** * 判断一级的是否全部选中 */ private boolean checkGroupAll(){ for (int i = 0; i < groupdata.size(); i++) { if(!groupdata.get(i).isCheck()){ return false; } } return true; } /** * 改变所有孩子的状态 */ private void changeChildState(int grouPistion,boolean flag){ List<CartBean.DataBean.ListBean> listBeen = childlist.get(grouPistion); for (int i = 0; i < listBeen.size(); i++) { listBeen.get(i).setCheck(flag); } } /** * 改变全选的 */ public void qx(boolean flag){ for (int i = 0; i < groupdata.size(); i++) { changeGroupstatus(i, flag); changeChildState(i, flag); } EventBus.getDefault().post(jS()); notifyDataSetChanged(); } }
group_item.xml
<?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"> <CheckBox android:id="@+id/gx_group" android:focusable="false" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/tv_group" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
child_item.xml
<?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"> <TextView android:id="@+id/title" android:layout_marginLeft="20dp" android:layout_width="match_parent" android:layout_height="wrap_content" /> <LinearLayout android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content"> <CheckBox android:layout_marginLeft="20dp" android:gravity="center_vertical" android:id="@+id/gouxuan_child" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <com.facebook.drawee.view.SimpleDraweeView android:gravity="center_vertical" android:id="@+id/title_img" android:layout_width="50dp" android:layout_height="50dp" /> <LinearLayout android:orientation="vertical" android:layout_marginLeft="30dp" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:id="@+id/price" android:layout_marginLeft="10dp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/yanse" android:layout_marginLeft="10dp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <LinearLayout android:layout_marginLeft="100dp" android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:id="@+id/jian" android:layout_width="20dp" android:layout_height="20dp" android:textSize="15sp" android:text="一"/> <TextView android:id="@+id/tv_num" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:paddingBottom="2dp" android:paddingLeft="20dp" android:paddingRight="20dp" android:paddingTop="2dp" android:text="1" /> <TextView android:id="@+id/jia" android:layout_width="20dp" android:layout_height="20dp" android:layout_marginLeft="5dp" android:text="+" android:textSize="15sp"/> </LinearLayout> </LinearLayout> <TextView android:id="@+id/del" android:layout_marginLeft="20dp" android:text="删除" android:textColor="#FFFFFF" android:background="#ff3660" android:layout_width="32dp" android:layout_height="29dp" /> </LinearLayout> </LinearLayout>
上述内容就是怎么在RxJava中使用Retrofit和Mvp实现购物车,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。