写代码前要先加上权限和依赖:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.INTERNET" />依赖:
compile 'com.android.support:design:26.+' //tablayout依赖 compile 'com.google.code.gson:gson:2.8.2' compile 'com.github.bumptech.glide:glide:3.6.0' compile 'com.squareup.okhttp3:okhttp:3.6.0' compile 'com.squareup.okio:okio:1.11.0' compile 'com.android.support:recyclerview-v7:26.+'因为购物车的逻辑性很强,所以写购物车之前要想清楚要写哪一步,不然很容易蒙圈...
各种需要的工具类中的东西
封装的OkHttp(我是用的OkHttp3请求数据);
因为是封装的,在这里就不多说了,见接口:
一些可能用到的接口:
//加入购物车 public static final String ADDCART_API = "https://www.zhaoapi.cn/product/addCart?uid=2845"; //购物车 public static final String SELECTART_API = "https://www.zhaoapi.cn/product/getCarts?uid=2845"; //更新购物车 public static final String UpDateCart_API = "https://www.zhaoapi.cn/product/updateCarts"; //删除购物车 public static final String DELETECART_API = "https://www.zhaoapi.cn/product/deleteCart";用MVP的模式写,很清晰也很快;
MVP模式:
到了model层就不要忘了bean包,有些bean包可能要自己手动加入一些东西;
这个在购物车的bean包会让自己手动加入全选的判断(因为我用的接口没有)
public static class DataBean { //自己写几个方法(3个) private boolean allChildInGroupCheck; public boolean isGroupCheck() { return allChildInGroupCheck; } public void setGroupCheck(boolean allChildInGroupCheck) { this.allChildInGroupCheck = allChildInGroupCheck; } /** * list : [{"bargainPrice":399,"createtime":"2017-10-02T15:20:02","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":2,"pid":81,"price":699,"pscid":85,"selected":0,"sellerid":2,"subhead":"满2件,总价打6.50折","title":"Gap男装 休闲舒适简约水洗五袋直筒长裤紧身牛仔裤941825 深灰色 33/32(175/84A)"}] * sellerName : 商家2 * sellerid : 2 */CountPriceBean,这个bean包是计算价钱的包;
public class CountPriceBean { private String priceString; private int count; public CountPriceBean(String priceString, int count) { this.priceString = priceString; this.count = count; } public String getPriceString() { return priceString; } public void setPriceString(String priceString) { this.priceString = priceString; } public int getCount() { return count; } public void setCount(int count) { this.count = count; } }
Model层:
public class CarModel { private final ICartPre iCartPre; public CarModel(ICartPre iCartPre){ this.iCartPre = iCartPre; } public void getData(String url){ OkHttp3Util.doGet(url, new Callback() { @Override public void onFailure(Call call, IOException e) { } @Override public void onResponse(Call call, Response response) throws IOException { if (response.isSuccessful()){ String string = response.body().string(); CartBean cartBean = new Gson().fromJson(string, CartBean.class); iCartPre.onSuccess(cartBean); } } }); } }Presenter层:
//p层的一个接口:
public interface ICartPre { void onSuccess(CartBean cartBean); }//p层接口的实现类:
public class CartPresenter implements ICartPre { private CarModel carModel; private ICartView iCartView; public CartPresenter(ICartView iCartView){ this.iCartView = iCartView; carModel = new CarModel(this); } public void getData(String url){ carModel.getData(url); } @Override public void onSuccess(CartBean cartBean) { iCartView.onSuccess(cartBean); } }//View层:
//view层接口:
public interface ICartView { void onSuccess(CartBean cartBean); }//view层接口的实现类就是Activity或fragment:
//实现接口先不着急,先完成主页面的布局:
要注意,购物车的列表是二级列表,所以自定义一个二级列表:
/** * Created by Administrator * 购物车的二级列表 */ public class CartTwoListView extends ExpandableListView { public CartTwoListView(Context context) { super(context); } public CartTwoListView(Context context, AttributeSet attrs) { super(context, attrs); } public CartTwoListView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } //重写测量的方法 @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int height = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, height); } }
购物车的主布局:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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" tools:context="CartActivity"> <RelativeLayout android:id="@+id/rl1" android:layout_width="match_parent" android:layout_height="50dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="购物车" android:textSize="26sp" /> </RelativeLayout> <ScrollView android:id="@+id/sl1" android:layout_below="@+id/rl1" android:layout_width="match_parent" android:layout_height="wrap_content"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <!--二级购物车列表--> <wwx.util.CartTwoListView android:id="@+id/cartTwoListView" android:layout_width="match_parent" android:layout_height="wrap_content"> </wwx.util.CartTwoListView> <!--为你推荐--> <TextView android:layout_width="match_parent" android:layout_height="300dp" android:text="为你推荐" android:background="#0f0"/> </LinearLayout> </ScrollView> <RelativeLayout android:id="@+id/progressBar" android:visibility="gone" android:layout_centerInParent="true" android:layout_width="wrap_content" android:layout_height="wrap_content"> <ProgressBar android:layout_width="wrap_content"