OK工具类
import android.content.Context; import android.os.Handler; import android.os.Message; import java.io.File; import java.io.IOException; import java.util.Map; import java.util.concurrent.TimeUnit; import okhttp3.Call; import okhttp3.Callback; import okhttp3.FormBody; import okhttp3.MediaType; import okhttp3.MultipartBody; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.RequestBody; import okhttp3.Response; /** * Author:kson * E-mail:[email protected] * Time:2018/05/08 * Description:okhttp封装 */ public class OkHttpUtils { //单例设计模式/其他比如builder模式 //封装常用的请求方式,get、post //扩展:封装拦截器以及其他的扩展功能 private static OkHttpUtils okHttpUtils; private Context context; private OkHttpClient okHttpClient; static String result = ""; private static Handler handler; private ICallback iCallback; private OkHttpUtils(Context context) { this.context = context; handler = new Handler() { @Override public void handleMessage(Message msg) { if (iCallback != null) { iCallback.getData(msg.obj.toString()); } } }; initOK(); } public void setiCallback(ICallback iCallback) { this.iCallback = iCallback; } /** * 创建ok */ private void initOK() { okHttpClient = new OkHttpClient.Builder() .readTimeout(10, TimeUnit.SECONDS) .writeTimeout(1000, TimeUnit.MILLISECONDS) .connectTimeout(5, TimeUnit.SECONDS).build(); } public static OkHttpUtils getInstance(Context context) { if (okHttpUtils == null) { synchronized (OkHttpUtils.class) { if (okHttpUtils == null) { okHttpUtils = new OkHttpUtils(context); } } } return okHttpUtils; } /** * get请求 * * @return */ public void getData(String url) { Request request = new Request.Builder() .url(url) .get().build(); okHttpClient.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { } @Override public void onResponse(Call call, Response response) throws IOException { if (response.isSuccessful()) { if (response.code() == 200) { result = response.body().string(); Message message = new Message(); message.obj = result; handler.sendMessage(message); } } } }); } /** * post请求:1原生表单 2.支持文件的,或者两个方式封装到一个方法里 * * @return */ public void post(String url, Map<String, String> params) { //1.FORM原生表单,<form></form>(单纯提交字符串类型的键值对)2.multipart/form-data(1.字符串类型键值对和文件) 3.raw(json、xml) 4.binary(文件流) //1.普通表单 //普通表单 FormBody.Builder builder = new FormBody.Builder(); for (Map.Entry<String, String> stringStringEntry : params.entrySet()) { builder.add(stringStringEntry.getKey(), stringStringEntry.getValue()); } Request request = new Request.Builder() .url(url) .post(builder.build()).build(); okHttpClient.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { } @Override public void onResponse(Call call, Response response) throws IOException { if (response.isSuccessful()) { if (response.code() == 200) { result = response.body().string(); Message message = new Message(); message.obj = result; handler.sendMessage(message); } } } }); } /** * post请求:2.支持文件的,或者两个方式封装到一个方法里 * * @return */ public void postFile(String url, Map<String, Object> params) { //1.FORM原生表单,<form></form>(单纯提交字符串类型的键值对)2.multipart/form-data(1.字符串类型键值对和文件) 3.raw(json、xml) 4.binary(文件流) //1.普通表单 //multipart/form-data MultipartBody.Builder builder = new MultipartBody.Builder(); builder.setType(MultipartBody.FORM); for (Map.Entry<String, Object> stringObjectEntry : params.entrySet()) { String key = stringObjectEntry.getKey(); Object value = stringObjectEntry.getValue(); if (value instanceof File){ File file = (File) value; builder.addFormDataPart(key,file.getName(),RequestBody.create(MediaType.parse("image/*"),file)); }else{ builder.addFormDataPart(key,value.toString()); } } Request request = new Request.Builder() .url(url) .post(builder.build()).build(); okHttpClient.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { } @Override public void onResponse(Call call, Response response) throws IOException { if (response.isSuccessful()) { if (response.code() == 200) { result = response.body().string(); Message message = new Message(); message.obj = result; handler.sendMessage(message); } } } }); } public interface ICallback { void getData(String result); } }
主布局
<?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:id="@+id/activity_add_car" 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: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="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_gravity="center_vertical" android:padding="12dp" android:src="@mipmap/leftjiantou" /> <TextView android:id="@+id/title" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:minHeight="48dp" android:text="购物车" android:textColor="#1a1a1a" android:textSize="16sp" /> </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_width="0dp" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginLeft="10dp" android:layout_marginRight="4dp" android:layout_weight="1" android:checkMark="?android:attr/listChoiceIndicatorMultiple" android:gravity="center" android:minHeight="64dp" android:text="全选" android:textAppearance="?android:attr/textAppearanceLarge" /> <LinearLayout android:id="@+id/ll_info" android:layout_width="0dp" android:layout_height="50dp" android:layout_weight="4"> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_weight="2" android:text="合计:" android:textSize="18sp" android:textStyle="bold" /> <TextView android:id="@+id/tv_total_price" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="4" android:gravity="center_vertical" android:text="¥0.00" android:textColor="@color/orangered" android:textSize="16sp" android:textStyle="bold" /> <TextView android:id="@+id/tv_go_to_pay" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="4" android:background="@color/orange" android:clickable="true" android:gravity="center" android:text="结算(0)" android:textColor="#FAFAFA" /> </LinearLayout> </LinearLayout> </LinearLayout>
二级列表父布局
<?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="wrap_content" android:background="@color/white" android:orientation="vertical" > <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@android:color/white" > <CheckBox android:id="@+id/determine_chekbox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="10dp" android:layout_marginRight="4dp" android:checkMark="?android:attr/listChoiceIndicatorMultiple" android:gravity="center" android:minHeight="38dp" android:minWidth="32dp" android:textAppearance="?android:attr/textAppearanceLarge" android:visibility="visible" /> <TextView android:id="@+id/tv_source_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginBottom="10dp" android:layout_marginTop="10dp" android:layout_toRightOf="@id/determine_chekbox" android:background="@android:color/white" android:drawablePadding="10dp" android:text="第八号当铺" android:textColor="@color/grey_color2" android:textSize="14sp" /> </RelativeLayout> </LinearLayout>
二级列表子布局
<?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="wrap_content" android:orientation="vertical"> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="#CCCCCC" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/page_backgroup" android:orientation="horizontal"> <CheckBox android:id="@+id/check_box" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginLeft="10dp" android:layout_marginRight="4dp" android:checkMark="?android:attr/listChoiceIndicatorMultiple" android:gravity="center" android:minHeight="64dp" android:minWidth="32dp" android:textAppearance="?android:attr/textAppearanceLarge" android:visibility="visible" /> <ImageView android:id="@+id/iv_adapter_list_pic" android:layout_width="100dp" android:layout_height="100dp" android:layout_marginBottom="15dp" android:layout_marginTop="13dp" android:scaleType="centerCrop" android:src="@mipmap/ic_launcher" /> <RelativeLayout android:id="@+id/rl_no_edtor" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_marginLeft="13dp"> <TextView android:id="@+id/tv_intro" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginRight="10dp" android:layout_marginTop="20dp" android:ellipsize="end" android:maxLines="2" android:text="第八号当铺美女一枚" android:textColor="@color/grey_color1" android:textSize="14sp" /> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_marginBottom="20dp" android:orientation="horizontal"> <TextView android:id="@+id/tv_discount_price" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:text="xxx" android:textColor="@color/gray" android:textSize="10sp" /> <TextView android:id="@+id/tv_price" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:paddingLeft="10dp" android:layout_toRightOf="@+id/tv_discount_price" android:singleLine="true" android:text="¥ 308.00" android:textColor="@color/orange_color" android:textSize="14sp" android:textStyle="bold" /> <TextView android:id="@+id/tv_buy_num" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_toRightOf="@+id/tv_price" android:paddingLeft="15dp" android:text="X 1" android:textColor="@color/gray" android:textSize="10sp" /> <LinearLayout android:id="@+id/ll_change_num" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:orientation="horizontal" android:layout_alignParentRight="true" > <TextView android:id="@+id/tv_reduce" android:layout_width="35dp" android:layout_height="35dp" android:background="@drawable/text_angle_gray" android:gravity="center" android:text="一" android:textColor="@color/grey_color1" android:textSize="12sp" /> <TextView android:id="@+id/tv_num" android:layout_width="35dp" android:layout_height="35dp" android:background="@drawable/text_angle" android:gravity="center" android:singleLine="true" android:text="1" android:textColor="@color/grey_color1" android:textSize="12sp" /> <TextView android:id="@+id/tv_add" android:layout_width="35dp" android:layout_height="35dp" android:background="@drawable/text_angle_right" android:gravity="center" android:text="+" android:textColor="@color/grey_color1" android:textSize="12sp" /> <TextView android:id="@+id/tv_goods_delete" android:layout_width="40dp" android:layout_height="match_parent" android:layout_gravity="center" android:background="@color/orange" android:gravity="center" android:text="删除" android:textColor="@color/white" /> </LinearLayout> </RelativeLayout> </RelativeLayout> </LinearLayout> </LinearLayout>
drawable文件里
第一个
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <!-- 连框颜色值 --> <item> <shape> <solid android:color="#CCCCCC" /> </shape> </item> <!-- 主体背景颜色值 --> <item android:bottom="1dp" android:top="1dp" android:right="1dp" > <!-- 边框里面背景颜色 白色 --> <shape> <solid android:color="@color/page_backgroup" /> </shape> </item> </layer-list>
第二个
<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <!-- 连框颜色值 --> <item> <shape> <solid android:color="#CCCCCC" /> </shape> </item> <!-- 主体背景颜色值 --> <item android:bottom="1dp" android:top="1dp" android:left="1dp" > <!-- 边框里面背景颜色 白色 --> <shape> <solid android:color="@color/page_backgroup" /> </shape> </item> </layer-list>
第三个
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="#FFFFFFFF"/> <corners android:radius="0dp"/> <stroke android:color="#CCCCCC" android:width="0.01dp" /> </shape>
第四个
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@color/menu_item_press" android:state_focused="true"/> <item android:drawable="@color/menu_item_press" android:state_selected="true"/> <item android:drawable="@color/menu_item_press" android:state_pressed="true"/> <item android:drawable="@android:color/transparent"/> </selector>
values文件夹colors
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="colorPrimary">#3F51B5</color> <color name="colorPrimaryDark">#303F9F</color> <color name="colorAccent">#FF4081</color> <!-- 列表分割线 色值#999999 --> <color name="list_divider">#999999</color> <!-- 选中态色块 色值#D7D7D7 --> <color name="list_item_pressed">#D7D7D7</color> <color name="white">#ffffff</color> <!-- 白色 --> <color name="ivory">#fffff0</color> <!-- 象牙色 --> <color name="lightyellow">#ffffe0</color> <color name="capture_text_cover_bg">#3060a4e3</color> <!-- 亮黄色 --> <color name="yellow">#ffff00</color> <!-- 黄色 --> <color name="snow">#fffafa</color> <!-- 雪白色 --> <color name="floralwhite">#fffaf0</color> <!-- 花白色 --> <color name="lemonchiffon">#fffacd</color> <!-- 柠檬绸色 --> <color name="cornsilk">#fff8dc</color> <!-- 米绸色 --> <color name="seaShell">#fff5ee</color> <!-- 海贝色 --> <color name="lavenderblush">#fff0f5</color> <!-- 淡紫红 --> <color name="papayawhip">#ffefd5</color> <!-- 番木色 --> <color name="blanchedalmond">#ffebcd</color> <!-- 白杏色 --> <color name="mistyrose">#ffe4e1</color> <!-- 浅玫瑰色 --> <color name="bisque">#ffe4c4</color> <!-- 桔黄色 --> <color name="moccasin">#ffe4b5</color> <!-- 鹿皮色 --> <color name="navajowhite">#ffdead</color> <!-- 纳瓦白 --> <color name="peachpuff">#ffdab9</color> <!-- 桃色 --> <color name="gold">#ffd700</color> <!-- 金色 --> <color name="pink">#ffc0cb</color> <!-- 粉红色 --> <color name="lightpink">#ffb6c1</color> <!-- 亮粉红色 --> <color name="orange">#fd7903</color> <!-- 橙色 --> <color name="lightsalmon">#ffa07a</color> <!-- 亮肉色 --> <color name="darkorange">#ff8c00</color> <!-- 暗桔黄色 --> <color name="coral">#ff7f50</color> <!-- 珊瑚色 --> <color name="hotpink">#ff69b4</color> <!-- 热粉红色 --> <color name="tomato">#ff6347</color> <!-- 西红柿色 --> <color name="orangered">#ff4500</color> <!-- 红橙色 --> <color name="deeppink">#ff1493</color> <!-- 深粉红色 --> <color name="fuchsia">#ff00ff</color> <!-- 紫红色 --> <color name="magenta">#ff00ff</color> <!-- 红紫色 --> <color name="red">#ff0000</color> <!-- 红色 --> <color name="oldlace">#fdf5e6</color> <!-- 老花色 --> <color name="lightgoldenrodyellow">#fafad2</color> <!-- 亮金黄色 --> <color name="linen">#faf0e6</color> <!-- 亚麻色 --> <color name="antiquewhite">#faebd7</color> <!-- 古董白 --> <color name="salmon">#fa8072</color> <!-- 鲜肉色 --> <color name="ghostwhite">#f8f8ff</color> <!-- 幽灵白 --> <color name="mintcream">#f5fffa</color> <!-- 薄荷色 --> <color name="whitesmoke">#f5f5f5</color> <!-- 烟白色 --> <color name="beige">#f5f5dc</color> <!-- 米色 --> <color name="wheat">#f5deb3</color> <!-- 浅黄色 --> <color name="sandybrown">#f4a460</color> <!-- 沙褐色 --> <color name="azure">#f0ffff</color> <!-- 天蓝色 --> <color name="honeydew">#f0fff0</color> <!-- 蜜色 --> <color name="aliceblue">#f0f8ff</color> <!-- 艾利斯兰 --> <color name="khaki">#f0e68c</color> <!-- 黄褐色 --> <color name="lightcoral">#f08080</color> <!-- 亮珊瑚色 --> <color name="palegoldenrod">#eee8aa</color> <!-- 苍麒麟色 --> <color name="violet">#ee82ee</color> <!-- 紫罗兰色 --> <color name="darksalmon">#e9967a</color> <!-- 暗肉色 --> <color name="lavender">#e6e6fa</color> <!-- 淡紫色 --> <color name="lightcyan">#e0ffff</color> <!-- 亮青色 --> <color name="burlywood">#deb887</color> <!-- 实木色 --> <color name="plum">#dda0dd</color> <!-- 洋李色 --> <color name="gainsboro">#dcdcdc</color> <!-- 淡灰色 --> <color name="crimson">#dc143c</color> <!-- 暗深红色 --> <color name="palevioletred">#db7093</color> <!-- 苍紫罗兰色 --> <color name="goldenrod">#daa520</color> <!-- 金麒麟色 --> <color name="orchid">#da70d6</color> <!-- 淡紫色 --> <color name="thistle">#d8bfd8</color> <!-- 蓟色 --> <color name="lightgray">#d3d3d3</color> <!-- 亮灰色 --> <color name="lightgrey">#d3d3d3</color> <!-- 亮灰色 --> <color name="tan">#d2b48c</color> <!-- 茶色 --> <color name="chocolate">#d2691e</color> <!-- 巧可力色 --> <color name="peru">#cd853f</color> <!-- 秘鲁色 --> <color name="indianred">#cd5c5c</color> <!-- 印第安红 --> <color name="mediumvioletred">#c71585</color> <!-- 中紫罗兰色 --> <color name="silver">#c0c0c0</color> <!-- 银色 --> <color name="darkkhaki">#bdb76b</color> <!-- 暗黄褐色 --> <color name="rosybrown">#bc8f8f</color> <!-- 褐玫瑰红 --> <color name="mediumorchid">#ba55d3</color> <!-- 中粉紫色 --> <color name="darkgoldenrod">#b8860b</color> <!-- 暗金黄色 --> <color name="firebrick">#b22222</color> <!-- 火砖色 --> <color name="powderblue">#b0e0e6</color> <!-- 粉蓝色 --> <color name="lightsteelblue">#b0c4de</color> <!-- 亮钢兰色 --> <color name="paleturquoise">#afeeee</color> <!-- 苍宝石绿 --> <color name="greenyellow">#adff2f</color> <!-- 黄绿色 --> <color name="lightblue">#add8e6</color> <!-- 亮蓝色 --> <color name="darkgray">#a9a9a9</color> <!-- 暗灰色 --> <color name="darkgrey">#a9a9a9</color> <!-- 暗灰色 --> <color name="brown">#a52a2a</color> <!-- 褐色 --> <color name="sienna">#a0522d</color> <!-- 赭色 --> <color name="darkorchid">#9932cc</color> <!-- 暗紫色 --> <color name="palegreen">#98fb98</color> <!-- 苍绿色 --> <color name="darkviolet">#9400d3</color> <!-- 暗紫罗兰色 --> <color name="mediumpurple">#9370db</color> <!-- 中紫色 --> <color name="lightgreen">#90ee90</color> <!-- 亮绿色 --> <color name="darkseagreen">#8fbc8f</color> <!-- 暗海兰色 --> <color name="saddlebrown">#8b4513</color> <!-- 重褐色 --> <color name="darkmagenta">#8b008b</color> <!-- 暗洋红 --> <color name="darkred">#8b0000</color> <!-- 暗红色 --> <color name="blueviolet">#8a2be2</color> <!-- 紫罗兰蓝色 --> <color name="lightskyblue">#87cefa</color> <!-- 亮天蓝色 --> <color name="skyblue">#87ceeb</color> <!-- 天蓝色 --> <color name="gray">#808080</color> <!-- 灰色 --> <color name="grey">#7c7b7b</color> <!-- 灰色 --> <color name="olive">#808000</color> <!-- 橄榄色 --> <color name="purple">#800080</color> <!-- 紫色 --> <color name="maroon">#800000</color> <!-- 粟色 --> <color name="aquamarine">#7fffd4</color> <!-- 碧绿色 --> <color name="chartreuse">#7fff00</color> <!-- 黄绿色 --> <color name="lawngreen">#7cfc00</color> <!-- 草绿色 --> <color name="mediumslateblue">#7b68ee</color> <!-- 中暗蓝色 --> <color name="lightslategray">#778899</color> <!-- 亮蓝灰 --> <color name="lightslategrey">#778899</color> <!-- 亮蓝灰 --> <color name="slategray">#708090</color> <!-- 灰石色 --> <color name="slategrey">#708090</color> <!-- 灰石色 --> <color name="olivedrab">#6b8e23</color> <!-- 深绿褐色 --> <color name="slateblue">#6a5acd</color> <!-- 石蓝色 --> <color name="dimgray">#696969</color> <!-- 暗灰色 --> <color name="dimgrey">#696969</color> <!-- 暗灰色 --> <color name="mediumaquamarine">#66cdaa</color> <!-- 中绿色 --> <color name="cornflowerblue">#6495ed</color> <!-- 菊兰色 --> <color name="cadetblue">#5f9ea0</color> <!-- 军兰色 --> <color name="darkolivegreen">#556b2f</color> <!-- 暗橄榄绿 --> <color name="indigo">#4b0082</color> <!-- 靛青色 --> <color name="mediumturquoise">#48d1cc</color> <!-- 中绿宝石 --> <color name="darkslateblue">#483d8b</color> <!-- 暗灰蓝色 --> <color name="steelblue">#4682b4</color> <!-- 钢兰色 --> <color name="royalblue">#4169e1</color> <!-- 皇家蓝 --> <color name="turquoise">#40e0d0</color> <!-- 青绿色 --> <color name="mediumseagreen">#3cb371</color> <!-- 中海蓝 --> <color name="limegreen">#32cd32</color> <!-- 橙绿色 --> <color name="darkslategray">#2f4f4f</color> <!-- 暗瓦灰色 --> <color name="darkslategrey">#2f4f4f</color> <!-- 暗瓦灰色 --> <color name="seagreen">#2e8b57</color> <!-- 海绿色 --> <color name="forestgreen">#228b22</color> <!-- 森林绿 --> <color name="lightseagreen">#20b2aa</color> <!-- 亮海蓝色 --> <color name="dodgerblue">#1e90ff</color> <!-- 闪兰色 --> <color name="midnightblue">#191970</color> <!-- 中灰兰色 --> <color name="aqua">#00ffff</color> <!-- 浅绿色 --> <color name="cyan">#00ffff</color> <!-- 青色 --> <color name="springgreen">#00ff7f</color> <!-- 春绿色 --> <color name="lime">#00ff00</color> <!-- 酸橙色 --> <color name="mediumspringgreen">#00fa9a</color> <!-- 中春绿色 --> <color name="darkturquoise">#00ced1</color> <!-- 暗宝石绿 --> <color name="deepskyblue">#00bfff</color> <!-- 深天蓝色 --> <color name="darkcyan">#008b8b</color> <!-- 暗青色 --> <color name="teal">#008080</color> <!-- 水鸭色 --> <color name="green">#008000</color> <!-- 绿色 --> <color name="darkgreen">#006400</color> <!-- 暗绿色 --> <color name="blue">#005dc1</color> <!-- 蓝色 --> <color name="mediumblue">#0000cd</color> <!-- 中兰色 --> <color name="darkblue">#00008b</color> <!-- 暗蓝色 --> <color name="navy">#000080</color> <!-- 海军色 --> <color name="black">#3B3B3B</color> <!-- 黑色 --> <!-- #2b4f6d 退改签 、选择配送信息中地址 文字的颜色值 --> <!-- 通用颜色统一风格 --> <color name="text_click">#0174E1</color> <color name="table_background">#cbcbcb</color> <color name="background">#eaeaea</color> <color name="light_white">#fcfcfc</color> <color name="tab_main_color">#1e1d1d</color> <color name="comm_bg">#eaeaea</color> <!-- 加黑 --> <color name="comm_text_black">#464646</color> <!-- 常用左侧文字颜色 --> <color name="comm_text_left">#3c3c3c</color> <!-- 常用右侧文字颜色 --> <color name="comm_text_right">#3c3c3c</color> <color name="comm_text_red">#da1609</color> <color name="comm_text_blue">#0f90e3</color> <color name="comm_text_green">#66c058</color> <color name="comm_text_yellow">#DAE532</color> <color name="comm_button_blue">#0074E1</color> <!-- 常用提示文字颜色 --> <color name="comm_text_tips">#949494</color> <!-- 分割线颜色 --> <color name="comm_cutline">#c4c4c4</color> <!-- 内容条目按下时的样式 --> <color name="pressed_bg">#f2f2f2</color> <color name="nopressed_bg">#fff</color> <!-- 助手内容颜色 --> <color name="title_content_color">#484848</color> <color name="content_color">#7d7d7d</color> <!-- 理财 --> <color name="new_red">#D21A3E</color> <color name="new_red_press">#B41131</color> <color name="text_black">#515151</color> <color name="text_left">#646464</color> <color name="text_right">#a1a1a1</color> <color name="text_gray">#b5b5b5</color> <color name="cutline_gray">#c4c4c4</color> <color name="text_yellow">#b18500</color> <color name="btn_bg_gray">#f7f7f7</color> <color name="new_green">#00FF99</color> <!-- 首页快捷菜单 --> <color name="menu_item_bg">#f6f6f6</color> <color name="text_tips">#929292</color> <!-- 账户总览 --> <color name="text_left_account">#464646</color> <color name="text_bom_account">#464646</color> <!-- 品质生活 商品列表 --> <color name="text_price_red">#ed3b3b</color> <color name="text_sold_greay">#b0b0b0</color> <color name="text_title_black">#232323</color> <!-- 帮助中心-常见问题 --> <color name="text_question">#333333</color> <color name="text_answer">#777777</color> <color name="result_view">#b0000000</color> <color name="viewfinder_mask">#60000000</color> <color name="possible_result_points">#c0ffff00</color> <color name="transparent">#00000000</color> <color name="comm_card_bg">#ffffff</color> <color name="grey_50">#fafafa</color> <color name="grey_200">#eeeeee</color> <color name="btn_blue">#0067db</color> <!-- 右边菜单点击颜色 --> <color name="right_menu_unpressed">#CDCEC9</color> <color name="right_menu_pressed">#C3C3C3</color> <color name="menu_item_press">#2e000000</color> <color name="page_backgroup">#f2f2f2</color> <!-- 灰色 --> <color name="grey_color1">#333333</color> <color name="grey_color2">#666666</color> <color name="grey_color3">#999999</color> <!-- 橙色 --> <color name="orange_color">#de6838</color> </resources>
bean类
public class GoodsInfo { protected String Id; protected String name; protected boolean isChoosed; private String imageUrl; private String desc; private double price; private int count; private int position;// 绝对位置,只在ListView构造的购物车中,在删除时有效 private String color; private String size; private String goodsImg; private double discountPrice; public double getDiscountPrice() { return discountPrice; } public void setDiscountPrice(double discountPrice) { this.discountPrice = discountPrice; } public String getGoodsImg() { return goodsImg; } public void setGoodsImg(String goodsImg) { this.goodsImg = goodsImg; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public String getSize() { return size; } public void setSize(String size) { this.size = size; } public GoodsInfo(String id, String name, String desc, double price, int count, String color, String size, String goodsImg, double discountPrice) { this.Id = id; this.name = name; this.desc = desc; this.price = price; this.count = count; this.color = color; this.size = size; this.goodsImg = goodsImg; this.discountPrice = discountPrice; } public String getId() { return Id; } public void setId(String id) { Id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public boolean isChoosed() { return isChoosed; } public void setChoosed(boolean isChoosed) { this.isChoosed = isChoosed; } public String getImageUrl() { return imageUrl; } public void setImageUrl(String imageUrl) { this.imageUrl = imageUrl; } public String getDesc() { return desc; } public void setDesc(String desc) { this.desc = desc; } public int getCount() { return count; } public void setCount(int count) { this.count = count; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public int getPosition() { return position; } public void setPosition(int position) { this.position = position; } }
购物车数据生成的bean类
import java.util.List; public class Selectshop { /** * msg : 请求成功 * code : 0 * data : [{"list":[{"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":4,"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":1599,"createtime":"2017-10-14T21:48:08","detailUrl":"https://item.m.jd.com/product/1993026402.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t5863/302/8961270302/97126/41feade1/5981c81cNc1b1fbef.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7003/250/1488538438/195825/53bf31ba/5981c57eN51e95176.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5665/100/8954482513/43454/418611a9/5981c57eNd5fc97ba.jpg!q70.jpg","num":1,"pid":47,"price":111,"pscid":39,"selected":0,"sellerid":3,"subhead":"碳黑色 32GB 全网通 官方标配 1件","title":"锤子 坚果Pro 特别版 巧克力色 酒红色 全网通 移动联通电信4G手机 双卡双待 碳黑色 32GB 全网通"}],"sellerName":"商家3","sellerid":"3"},{"list":[{"bargainPrice":22.9,"createtime":"2017-10-03T23:53:28","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":6,"pid":28,"price":599,"pscid":2,"selected":0,"sellerid":5,"subhead":"三只松鼠零食特惠,专区满99减50,满199减100,火速抢购》","title":"三只松鼠 坚果炒货 零食奶油味 碧根果225g/袋"}],"sellerName":"商家5","sellerid":"5"},{"list":[{"bargainPrice":3455,"createtime":"2017-10-03T23:53:28","detailUrl":"https://item.m.jd.com/product/12224420750.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t9106/106/1785172479/537280/253bc0ab/59bf78a7N057e5ff7.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t9106/106/1785172479/537280/253bc0ab/59bf78a7N057e5ff7.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t8461/5/1492479653/68388/7255e013/59ba5e84N91091843.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t8461/5/1492479653/68388/7255e013/59ba5e84N91091843.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t8803/356/1478945529/489755/2a163ace/59ba5e84N7bb9a666.jpg!q70.jpg","num":1,"pid":52,"price":666,"pscid":39,"selected":0,"sellerid":8,"subhead":"【现货新品抢购】全面屏2.0震撼来袭,骁龙835处理器,四曲面陶瓷机","title":"小米(MI) 小米MIX2 手机 黑色 全网通 (6GB+64GB)【标配版】"}],"sellerName":"商家8","sellerid":"8"},{"list":[{"bargainPrice":22.9,"createtime":"2017-10-03T23:43:53","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":3,"pid":33,"price":988,"pscid":2,"selected":0,"sellerid":10,"subhead":"三只松鼠零食特惠,专区满99减50,满199减100,火速抢购》","title":"三只松鼠 坚果炒货 零食奶油味 碧根果225g/袋"},{"bargainPrice":3455,"createtime":"2017-10-03T23:53:28","detailUrl":"https://item.m.jd.com/product/12224420750.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t9106/106/1785172479/537280/253bc0ab/59bf78a7N057e5ff7.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t9106/106/1785172479/537280/253bc0ab/59bf78a7N057e5ff7.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t8461/5/1492479653/68388/7255e013/59ba5e84N91091843.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t8461/5/1492479653/68388/7255e013/59ba5e84N91091843.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t8803/356/1478945529/489755/2a163ace/59ba5e84N7bb9a666.jpg!q70.jpg","num":1,"pid":54,"price":888,"pscid":39,"selected":0,"sellerid":10,"subhead":"【现货新品抢购】全面屏2.0震撼来袭,骁龙835处理器,四曲面陶瓷机","title":"小米(MI) 小米MIX2 手机 黑色 全网通 (6GB+64GB)【标配版】"}],"sellerName":"商家10","sellerid":"10"},{"list":[{"bargainPrice":159,"createtime":"2017-10-14T21:49:15","detailUrl":"https://item.m.jd.com/product/5061723.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t8716/197/1271594444/173291/2f40bb4f/59b743bcN8509428e.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t8347/264/1286771527/92188/5cf5ec04/59b7420fN65378e9e.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7363/165/3000956253/190883/179a372/59b743bfNd0c79d93.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7399/112/2935531768/183594/b77c7d4a/59b7441aNc3d40133.jpg!q70.jpg","num":2,"pid":100,"price":2200,"pscid":112,"selected":0,"sellerid":11,"subhead":"针织针织闪闪闪亮你的眼","title":"维迩旎 2017秋冬新款长袖针织连衣裙韩版气质中长款名媛包臀A字裙 zx179709 黑色 XL"}],"sellerName":"商家11","sellerid":"11"},{"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":3,"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":1,"pid":5,"price":88.99,"pscid":1,"selected":0,"sellerid":21,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"}],"sellerName":"商家21","sellerid":"21"},{"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":7,"price":120.01,"pscid":1,"selected":0,"sellerid":23,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"},{"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":2,"pid":79,"price":888,"pscid":40,"selected":0,"sellerid":23,"subhead":"购买电脑办公部分商品满1元返火车票5元优惠券(返完即止)","title":"全球购 新款Apple MacBook Pro 苹果笔记本电脑 银色VP2新13英寸Bar i5/8G/256G"}],"sellerName":"商家23","sellerid":"23"}] */ 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":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":4,"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; 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 : 99.0 * 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 : 4 * pid : 45 * price : 2999.0 * pscid : 39 * selected : 0 * sellerid : 1 * subhead : 高清双摄,就是清晰!2000+1600万高清摄像头,6GB大内存+高通骁龙835处理器,性能怪兽! * title : 一加手机5 (A5000) 6GB+64GB 月岩灰 全网通 双卡双待 移动联通电信4G手机 */ 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; } } } }
StoreInfo -----> bean类
public class StoreInfo { protected String Id; protected String name; protected boolean isChoosed; private boolean isEdtor; public boolean isEdtor() { return isEdtor; } public void setIsEdtor(boolean isEdtor) { this.isEdtor = isEdtor; } public StoreInfo(String id, String name) { Id = id; this.name = name; } public String getId() { return Id; } public void setId(String id) { Id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public boolean isChoosed() { return isChoosed; } public void setChoosed(boolean isChoosed) { this.isChoosed = isChoosed; } }
mvp view层接口
import java.util.List; public interface ShopcarView { void getshopcar(List<Selectshop.DataBean> data); }
model层
import com.google.gson.Gson; import java.util.HashMap; import java.util.List; import java.util.Map; public class GetCartsModel { private IGetCartsModel iGetCartsModel; public void getCar(int uid, final IGetCartsModel iGetCartsModel) { OkHttpUtils okHttpUtils = OkHttpUtils.getInstance(MyApp.context); String url = "https://www.zhaoapi.cn/product/getCarts?uid=" + uid; okHttpUtils.getData(url); okHttpUtils.setiCallback(new OkHttpUtils.ICallback() { @Override public void getData(String result) { Selectshop selectshop = new Gson().fromJson(result, Selectshop.class); iGetCartsModel.issucceed(selectshop.getData()); } }); } public interface IGetCartsModel { void issucceed(List<Selectshop.DataBean> data); } }
presenter层
import java.util.List; public class GetCartsPresenter { private GetCartsModel getCartsModel; private ShopcarView shopcarView; public GetCartsPresenter(ShopcarView shopcarView) { this.shopcarView = shopcarView; getCartsModel = new GetCartsModel(); } public void getCar(int uid) { getCartsModel.getCar(uid, new GetCartsModel.IGetCartsModel() { @Override public void issucceed(List<Selectshop.DataBean> data) { shopcarView.getshopcar(data); } }); } //解绑 public void detach() { if (shopcarView != null) { shopcarView = null; } } }
主Activity
import android.content.DialogInterface; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.CheckBox; import android.widget.ExpandableListView; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class ShopCarActivity extends AppCompatActivity implements ShopcartAdapter.CheckInterface, ShopcartAdapter.ModifyCountInterface, ShopcarView, View.OnClickListener { private int uid = 71; /** * 后退键 */ private ImageView mBack; /** * 购物车 */ private TextView mTitle; /** * 二级列表 */ private ExpandableListView mExListView; /** * 全选 */ private CheckBox mAllChekbox; /** * ¥0.00 */ private TextView mTvTotalPrice; /** * 结算(0) */ private TextView mTvGoToPay; // 购买的商品总价 private double totalPrice = 0.00; // 购买的商品总数量 private int totalCount = 0; private ShopcartAdapter selva; // 组元素数据列表 private List<StoreInfo> groups = new ArrayList<StoreInfo>(); // 子元素数据列表 private Map<String, List<GoodsInfo>> children = new HashMap<String, List<GoodsInfo>>(); private List<GoodsInfo> products; private AlertDialog alert; private GetCartsPresenter getCartsPresenter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_shop_car); initView(); //绑定 getCartsPresenter = new GetCartsPresenter(this); getCartsPresenter.getCar(uid); } /** * 初始化控件 */ private void initView() { mBack = (ImageView) findViewById(R.id.back); mTitle = (TextView) findViewById(R.id.title); mExListView = (ExpandableListView) findViewById(R.id.exListView); mAllChekbox = (CheckBox) findViewById(R.id.all_chekbox); mTvTotalPrice = (TextView) findViewById(R.id.tv_total_price); mTvGoToPay = (TextView) findViewById(R.id.tv_go_to_pay); mTvGoToPay.setOnClickListener(this); mAllChekbox.setOnClickListener(this); } /** * 组选框状态改变触发的事件 * * @param groupPosition 组元素位置 * @param isChecked 组元素选中与否 */ @Override public void checkGroup(int groupPosition, boolean isChecked) { StoreInfo group = groups.get(groupPosition); List<GoodsInfo> childs = children.get(group.getId()); for (int i = 0; i < childs.size(); i++) { childs.get(i).setChoosed(isChecked); } if (isAllCheck()) mAllChekbox.setChecked(true); else mAllChekbox.setChecked(false); selva.notifyDataSetChanged(); calculate(); } /** * 计算总数量与总价 */ private void calculate() { totalCount = 0; totalPrice = 0.00; for (int i = 0; i < groups.size(); i++) { StoreInfo group = groups.get(i); List<GoodsInfo> childs = children.get(group.getId()); for (int j = 0; j < childs.size(); j++) { GoodsInfo product = childs.get(j); if (product.isChoosed()) { totalCount++; totalPrice += product.getPrice() * product.getCount(); } } } mTvTotalPrice.setText("¥" + totalPrice); mTvGoToPay.setText("去支付(" + totalCount + ")"); } /** * 判断所有组选框状态是否全部选中 * * @return */ private boolean isAllCheck() { for (StoreInfo group : groups) { if (!group.isChoosed()) return false; } return true; } /** * 子选框状态改变时触发的事件 * * @param groupPosition 组元素位置 * @param childPosition 子元素位置 * @param isChecked 子元素选中与否 */ @Override public void checkChild(int groupPosition, int childPosition, boolean isChecked) { // 判断改组下面的所有子元素是否是同一种状态 boolean allChildSameState = true; StoreInfo group = groups.get(groupPosition); List<GoodsInfo> childs = children.get(group.getId()); for (int i = 0; i < childs.size(); i++) { // 不全选中 if (childs.get(i).isChoosed() != isChecked) { allChildSameState = false; break; } } //获取店铺选中商品的总金额 if (allChildSameState) { // 如果所有子元素状态相同,那么对应的组元素被设为这种统一状态 group.setChoosed(isChecked); } else { // 否则,组元素一律设置为未选中状态 group.setChoosed(false); } if (isAllCheck()) { // 全选 mAllChekbox.setChecked(true); } else { // 反选 mAllChekbox.setChecked(false); } selva.notifyDataSetChanged(); calculate(); } /** * 增加操作 * * @param groupPosition 组元素位置 * @param childPosition 子元素位置 * @param showCountView 用于展示变化后数量的View * @param isChecked 子元素选中与否 */ @Override public void doIncrease(int groupPosition, int childPosition, View showCountView, boolean isChecked) { GoodsInfo product = (GoodsInfo) selva.getChild(groupPosition, childPosition); int currentCount = product.getCount(); currentCount++; product.setCount(currentCount); ((TextView) showCountView).setText(currentCount + ""); calculate(); selva.notifyDataSetChanged(); } /** * 删减操作 * * @param groupPosition 组元素位置 * @param childPosition 子元素位置 * @param showCountView 用于展示变化后数量的View * @param isChecked 子元素选中与否 */ @Override public void doDecrease(int groupPosition, int childPosition, View showCountView, boolean isChecked) { GoodsInfo product = (GoodsInfo) selva.getChild(groupPosition, childPosition); int currentCount = product.getCount(); if (currentCount == 1) return; currentCount--; product.setCount(currentCount); ((TextView) showCountView).setText(currentCount + ""); calculate(); selva.notifyDataSetChanged(); } /** * 删除子item * * @param groupPosition * @param childPosition */ @Override public void childDelete(int groupPosition, int childPosition) { children.get(groups.get(groupPosition).getId()).remove(childPosition); StoreInfo group = groups.get(groupPosition); List<GoodsInfo> childs = children.get(group.getId()); if (childs.size() == 0) { groups.remove(groupPosition); } selva.notifyDataSetChanged(); } /** * 获取购物车数据 * * @param data */ @Override public void getshopcar(List<Selectshop.DataBean> data) { for (int i = 0; i < data.size(); i++) { groups.add(new StoreInfo(data.get(i).getSellerid(), data.get(i).getSellerName())); products = new ArrayList<GoodsInfo>(); for (int j = 0; j < data.get(i).getList().size(); j++) { String images = data.get(i).getList().get(j).getImages(); String[] split = images.split("[|]"); String[] split1 = split[0].split("[!]"); List<Selectshop.DataBean.ListBean> list = data.get(i).getList(); products.add(new GoodsInfo(list.get(j).getPid() + "", "商品", list.get(j).getTitle(), Double.valueOf(list.get(j).getPrice() + ""), Integer.parseInt(list.get(j).getNum() + ""), "", "1", split1[0], Double.valueOf(list.get(j).getBargainPrice()))); } // 将组元素的一个唯一值,这里取Id,作为子元素List的Key children.put(groups.get(i).getId(), products); } initEvents(); } private void initEvents() { selva = new ShopcartAdapter(groups, children, this); selva.setCheckInterface(this);// 关键步骤1,设置复选框接口 selva.setModifyCountInterface(this);// 关键步骤2,设置数量增减接口 mExListView.setAdapter(selva); //默认展开 for (int i = 0; i < selva.getGroupCount(); i++) { // 关键步骤3,初始化时,将ExpandableListView以展开的方式呈现 mExListView.expandGroup(i); } } /** * 全选反选 */ private void doCheckAll() { for (int i = 0; i < groups.size(); i++) { groups.get(i).setChoosed(mAllChekbox.isChecked()); StoreInfo group = groups.get(i); List<GoodsInfo> childs = children.get(group.getId()); for (int j = 0; j < childs.size(); j++) { childs.get(j).setChoosed(mAllChekbox.isChecked()); } } //调用计算总数量与总价方法 calculate(); //刷新 selva.notifyDataSetChanged(); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.all_chekbox: doCheckAll(); break; case R.id.tv_go_to_pay: if (totalCount == 0) { Toast.makeText(this, "请选择要支付的商品", Toast.LENGTH_LONG).show(); return; } alert = new AlertDialog.Builder(this).create(); alert.setTitle("操作提示"); alert.setMessage("总计:\n" + totalCount + "种商品\n" + totalPrice + "元"); alert.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { return; } }); alert.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }); alert.show(); break; } } }
适配器
import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.text.Spannable; import android.text.SpannableString; import android.text.style.StrikethroughSpan; import android.view.View; import android.view.ViewGroup; import android.widget.BaseExpandableListAdapter; import android.widget.CheckBox; import android.widget.ImageView; import android.widget.RelativeLayout; import android.widget.TextView; import com.bumptech.glide.Glide; import java.util.List; import java.util.Map; public class ShopcartAdapter extends BaseExpandableListAdapter { private List<StoreInfo> groups; private Map<String, List<GoodsInfo>> children; private Context context; private CheckInterface checkInterface; private ModifyCountInterface modifyCountInterface; /** * 构造函数 * @param groups 组元素列表 * @param children 子元素列表 * @param context */ public ShopcartAdapter(List<StoreInfo> groups, Map<String, List<GoodsInfo>> children, Context context) { this.groups = groups; this.children = children; this.context = context; } public void setCheckInterface(CheckInterface checkInterface) { this.checkInterface = checkInterface; } public void setModifyCountInterface(ModifyCountInterface modifyCountInterface) { this.modifyCountInterface = modifyCountInterface; } @Override public int getGroupCount() { return groups.size(); } @Override public int getChildrenCount(int groupPosition) { String groupId = groups.get(groupPosition).getId(); return children.get(groupId).size(); } @Override public Object getGroup(int groupPosition) { return groups.get(groupPosition); } @Override public Object getChild(int groupPosition, int childPosition) { List<GoodsInfo> childs = children.get(groups.get(groupPosition).getId()); return childs.get(childPosition); } @Override public long getGroupId(int groupPosition) { return groupPosition; } @Override public long getChildId(int groupPosition, int childPosition) { return childPosition; } @Override public boolean hasStableIds() { return false; } @Override public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { final GroupViewHolder gholder; if (convertView == null) { gholder = new GroupViewHolder(); //父列表 convertView = View.inflate(context, R.layout.item_shopcart_group, null); gholder.cb_check = (CheckBox) convertView.findViewById(R.id.determine_chekbox); gholder.tv_group_name = (TextView) convertView.findViewById(R.id.tv_source_name); convertView.setTag(gholder); } else { gholder = (GroupViewHolder) convertView.getTag(); } final StoreInfo group = (StoreInfo) getGroup(groupPosition); if (group != null) { //商家名称 gholder.tv_group_name.setText(group.getName()); //商家复选框 gholder.cb_check.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v){ //复选框状态 group.setChoosed(((CheckBox) v).isChecked()); // 暴露组选接口 checkInterface.checkGroup(groupPosition, ((CheckBox) v).isChecked()); } }); gholder.cb_check.setChecked(group.isChoosed()); } else { groups.remove(groupPosition); } return convertView; } @Override public View getChildView(final int groupPosition, final int childPosition, final boolean isLastChild, View convertView, final ViewGroup parent) { final ChildViewHolder cholder; if (convertView == null) { cholder = new ChildViewHolder(); //子列表 convertView = View.inflate(context, R.layout.item_shopcart_child, null); cholder.cb_check = (CheckBox) convertView.findViewById(R.id.check_box); cholder.tv_product_desc = (TextView) convertView.findViewById(R.id.tv_intro); cholder.tv_price = (TextView) convertView.findViewById(R.id.tv_price); cholder.iv_increase = (TextView) convertView.findViewById(R.id.tv_add); cholder.iv_decrease = (TextView) convertView.findViewById(R.id.tv_reduce); cholder.tv_count = (TextView) convertView.findViewById(R.id.tv_num); cholder.rl_no_edtor = (RelativeLayout) convertView.findViewById(R.id.rl_no_edtor); cholder.tv_discount_price = (TextView) convertView.findViewById(R.id.tv_discount_price); cholder.tv_buy_num = (TextView) convertView.findViewById(R.id.tv_buy_num); cholder.tv_goods_delete = (TextView) convertView.findViewById(R.id.tv_goods_delete); cholder.iv_adapter_list_pic = (ImageView) convertView.findViewById(R.id.iv_adapter_list_pic); convertView.setTag(cholder); } else { cholder = (ChildViewHolder) convertView.getTag(); } final GoodsInfo goodsInfo = (GoodsInfo) getChild(groupPosition, childPosition); if (goodsInfo != null) { cholder.tv_product_desc.setText(goodsInfo.getDesc()); cholder.tv_price.setText("¥" + goodsInfo.getDiscountPrice() + ""); cholder.tv_count.setText(goodsInfo.getCount() + ""); Glide.with(context).load(goodsInfo.getGoodsImg()).into(cholder.iv_adapter_list_pic); //给原价加横线 SpannableString spanString = new SpannableString("¥" + String.valueOf(goodsInfo.getPrice())); StrikethroughSpan span = new StrikethroughSpan(); spanString.setSpan(span, 0, String.valueOf(goodsInfo.getPrice()).length() + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); cholder.tv_discount_price.setText(spanString); cholder.tv_buy_num.setText("x" + goodsInfo.getCount()); cholder.cb_check.setChecked(goodsInfo.isChoosed()); //商品复选框点击事件 cholder.cb_check.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //复选框状态 goodsInfo.setChoosed(((CheckBox) v).isChecked()); cholder.cb_check.setChecked(((CheckBox) v).isChecked()); // 暴露子选接口 checkInterface.checkChild(groupPosition, childPosition, ((CheckBox) v).isChecked()); } }); //增加 cholder.iv_increase.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 暴露增加接口 modifyCountInterface.doIncrease(groupPosition, childPosition, cholder.tv_count, cholder.cb_check.isChecked()); } }); //删减 cholder.iv_decrease.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 暴露删减接口 modifyCountInterface.doDecrease(groupPosition, childPosition, cholder.tv_count, cholder.cb_check.isChecked()); } }); //删除 购物车 cholder.tv_goods_delete.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //对话框提示 AlertDialog alert = new AlertDialog.Builder(context).create(); alert.setTitle("操作提示"); alert.setMessage("您确定要将这些商品从购物车中移除吗?"); alert.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { return; } }); alert.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { modifyCountInterface.childDelete(groupPosition, childPosition); } }); alert.show(); } }); } return convertView; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return false; } /** * 组元素绑定器 */ private class GroupViewHolder { CheckBox cb_check; TextView tv_group_name; } /** * 子元素绑定器 */ private class ChildViewHolder { CheckBox cb_check; ImageView iv_adapter_list_pic; TextView tv_product_desc; TextView tv_price; TextView iv_increase; TextView tv_count; TextView iv_decrease; RelativeLayout rl_no_edtor; TextView tv_discount_price; TextView tv_buy_num; TextView tv_goods_delete; } /** * 复选框接口 */ public interface CheckInterface { /** * 组选框状态改变触发的事件 * * @param groupPosition 组元素位置 * @param isChecked 组元素选中与否 */ public void checkGroup(int groupPosition, boolean isChecked); /** * 子选框状态改变时触发的事件 * * @param groupPosition 组元素位置 * @param childPosition 子元素位置 * @param isChecked 子元素选中与否 */ public void checkChild(int groupPosition, int childPosition, boolean isChecked); } /** * 改变数量的接口 */ public interface ModifyCountInterface { /** * 增加操作 * * @param groupPosition 组元素位置 * @param childPosition 子元素位置 * @param showCountView 用于展示变化后数量的View * @param isChecked 子元素选中与否 */ public void doIncrease(int groupPosition, int childPosition, View showCountView, boolean isChecked); /** * 删减操作 * * @param groupPosition 组元素位置 * @param childPosition 子元素位置 * @param showCountView 用于展示变化后数量的View * @param isChecked 子元素选中与否 */ public void doDecrease(int groupPosition, int childPosition, View showCountView, boolean isChecked); /** * 删除子item * * @param groupPosition * @param childPosition */ public void childDelete(int groupPosition, int childPosition); } }