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

时间:2021-05-02

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

效果图:

1.依赖

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 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

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 <?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

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 <?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

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 <?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

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 <?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

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 <?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

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 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;
}

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章