首先导入依赖
compile 'com.jcodecraeer:xrecyclerview:1.3.2' compile 'com.google.code.gson:gson:2.7' compile 'com.squareup.okhttp3:okhttp:3.2.0' compile 'com.squareup.okio:okio:1.7.0' compile 'com.facebook.fresco:fresco:0.12.0'
然后是布局文件,切记将母布局上的灰色的一行粘贴到使用的布局中
<?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="wrap_content" android:orientation="vertical" tools:context="com.example.administrator.day2_demo.view.MainActivity"> <com.jcodecraeer.xrecyclerview.XRecyclerView android:layout_width="match_parent" android:layout_height="match_parent" android:overScrollMode="never" android:id="@+id/recy"></com.jcodecraeer.xrecyclerview.XRecyclerView> </LinearLayout>
item布局将样式方法前的Android改为fresco
xmlns:fresco="http://schemas.android.com/apk/res-auto"放入母布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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" android:orientation="horizontal"> <com.facebook.drawee.view.SimpleDraweeView android:layout_marginLeft="10dp" android:layout_width="90dp" android:layout_height="90dp" fresco:actualImageScaleType="centerCrop" fresco:roundingBorderColor="#fff3cf44" fresco:roundingBorderWidth="2dp" android:src="@drawable/ic_launcher_background" android:id="@+id/img" /> <LinearLayout android:layout_marginLeft="10dp" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="100dp"> <TextView android:layout_gravity="center" android:layout_marginTop="8dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/price01" android:textSize="22sp" android:text="11111" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/price02" android:layout_marginLeft="10dp" android:textSize="18sp" android:textColor="#FF0000" android:text="11111" /> </LinearLayout> </LinearLayout>
item02与itme一样
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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" android:orientation="horizontal"> <LinearLayout android:layout_marginLeft="10dp" android:layout_width="0dp" android:layout_weight="8" android:layout_height="100dp" android:orientation="vertical"> <TextView android:id="@+id/item02_price01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="8dp" android:text="11111" android:textSize="22sp" /> <TextView android:id="@+id/item02_price02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:text="11111" android:textColor="#FF0000" android:textSize="18sp" /> </LinearLayout> <com.facebook.drawee.view.SimpleDraweeView android:id="@+id/item02_img" android:layout_width="90dp" android:layout_height="90dp" android:src="@drawable/ic_launcher_background" fresco:roundAsCircle="true" fresco:roundingBorderColor="#fff3cf44" fresco:roundingBorderWidth="2dp" /> </LinearLayout>
以下 是OKhttoUtils
package com.example.administrator.day2_demo.http; import java.util.Map; public interface GoodsListener { void GoodsSuccess(String json); void GoodsError(String error); }
package com.example.administrator.day2_demo.http; public class Httpfig { public static String goods_url="https://www.zhaoapi.cn/product/getProducts"; }
package com.example.administrator.day2_demo.http; import android.os.Handler; import android.os.Message; import android.util.Log; import java.io.File; import java.io.IOException; import java.util.Map; import java.util.Set; import okhttp3.Call; import okhttp3.Callback; import okhttp3.FormBody; import okhttp3.Interceptor; import okhttp3.MediaType; import okhttp3.MultipartBody; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.RequestBody; import okhttp3.Response; /** * 单利模式 * 就是一个应用里面只有一个对象 * 好处:节约内存。 * 1.饿汉式 * 开始对象就创建出来了 * 2.懒汉式 * 开始对象是null,什么时候使用,就是什么时候创建。 */ public class OKHttpUtils { private static final String TAG = "OKHttpUtils----"; private static OKHttpUtils okHttpUtils = null; private MyHandler myHandler = new MyHandler(); private LoginListener onLoginListener; public static OKHttpUtils getInstance() { if (okHttpUtils == null) { okHttpUtils = new OKHttpUtils(); } return okHttpUtils; } //get public void okGet(String url, String mobile, String password) { String url1 = url + "?mobile=" + mobile + "&password=" + password; //创建OKHttpClient OkHttpClient client = new OkHttpClient.Builder().addInterceptor(new MyIntercepter()).build(); //用request 请求,将url,和参数惊醒封装。 Request request = new Request.Builder().url(url1).build(); //call是请求队列,ok里面,请求队队列默认是一个。 Call call = client.newCall(request); //一部请求,成功和失败的方法都是在线程,不能进行UI跟新。 //handler synctask call.enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { Log.d(TAG, "onFailure() returned: " + "失败--"); Message message = myHandler.obtainMessage(); message.what = 0; message.obj = e.getMessage(); myHandler.sendMessage(message); } @Override public void onResponse(Call call, Response response) throws IOException { // java.lang.IllegalStateException: closed // Log.d(TAG, "onResponse() returned: " + response.body().string()); Message message = myHandler.obtainMessage(); message.what = 1; message.obj = response.body().string(); myHandler.sendMessage(message); } }); } //post public void okPost(String url, Map<String, String> params) { //创建OKHttpClient OkHttpClient client = new OkHttpClient.Builder().addInterceptor(new MyIntercepter()).build(); FormBody.Builder builder = new FormBody.Builder(); //遍历map集合,将参数放入body即可 遍历的核心思想就是。根据键取值 Set<String> keys = params.keySet(); for (String key : keys) { String value = params.get(key); //放入body builder.add(key, value); } FormBody body = builder.build(); //用request 请求,将url,和参数惊醒封装。 Request request = new Request.Builder().url(url).post(body).build(); //call是请求队列,ok里面,请求队队列默认是一个。 Call call = client.newCall(request); //一部请求,成功和失败的方法都是在线程,不能进行UI跟新。 //handler synctask call.enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { Log.d(TAG, "onFailure() returned: " + "失败--"); Message message = myHandler.obtainMessage(); message.what = 0; message.obj = e.getMessage(); myHandler.sendMessage(message); } @Override public void onResponse(Call call, Response response) throws IOException { // Log.d(TAG, "onResponse() returned: " + response.body().string()); Message message = myHandler.obtainMessage(); message.what = 1; message.obj = response.body().string(); myHandler.sendMessage(message); } }); } //上传 //将输入发送到主线程,处理线程问题 //接口回调 class MyHandler extends Handler { @Override public void handleMessage(Message msg) { int w = msg.what; switch (w) { case 0: //失败 String error = (String) msg.obj; //传出去 onLoginListener.loginError(error); break; case 1: //成功 String json = (String) msg.obj; onLoginListener.loginSuccess(json); break; } } } // public interface LoginListener { //成功 void loginSuccess(String json); //失败 void loginError(String error); } //提供一个外部访问的方法 public void setOnLoginListener(LoginListener onLoginListener) { this.onLoginListener = onLoginListener; } //拦截器。实现公参数的拼接 class MyIntercepter implements Interceptor { //拦截的方法。 // chain链条,获取请求,获取相应 @Override public Response intercept(Chain chain) throws IOException { //获取请求 Request request = chain.request(); RequestBody body = request.body(); //判断如果是formbody if (body instanceof FormBody) { //创建新的body,将原来的参数,放入新的body里面,然后再讲公共参数,添加到新的body里面 FormBody.Builder builder = new FormBody.Builder(); //遍历旧的body for (int i = 0; i < ((FormBody) body).size(); i++) { String key = ((FormBody) body).name(i); String value = ((FormBody) body).value(i); //添加 builder.add(key, value); } builder.add("source", "android"); builder.add("token", "appVersion"); FormBody newBody = builder.build(); //创建新的请求 Request request1 = request.newBuilder().post(newBody).build(); //让request1去重新请求,proceed所有http请求的核心 Response response = chain.proceed(request1); return response; } return null; } } //上传头像 public void upLoadImage(String upload_url, File file) { //创建OKHttpClient OkHttpClient okHttpClient = new OkHttpClient(); //创建MultiPartBody MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM); //设置参数 MediaType mediaType = MediaType.parse("image/png"); builder.addFormDataPart("file", file.getName(), RequestBody.create(mediaType, file)); //添加其他参数 builder.addFormDataPart("uid", "71"); MultipartBody body = builder.build(); //创建请求队列 Request request = new Request.Builder().url(upload_url).post(body).build(); Call newCall = okHttpClient.newCall(request); newCall.enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { Log.d(TAG, "onFailure() returned: " + "shibai---" + e.getMessage()); } @Override public void onResponse(Call call, Response response) throws IOException { Log.d(TAG, "onResponse() returned: " + response.body().string()); } }); } }
以下是View视图层
IMainView
public interface IMainView { void ShowGoods(List<GoodsBean.DataBean> list); int pscid(); }
MyApp
public class MyApp extends Application { private static MyApp mInstance; @Override public void onCreate() { super.onCreate(); mInstance=this; Fresco.initialize(this); } public static MyApp getInstance(){ return mInstance; }; }
以下是model层
public interface IModel { void getDate(String url, Map<String,String> map, GoodsListener goodsListener); }
IModelImpl
public class IModelImpl implements IModel { @Override public void getDate(String url, Map<String, String> map, final GoodsListener goodsListener) { OKHttpUtils okHttpUtils=new OKHttpUtils(); okHttpUtils.okPost(url,map); okHttpUtils.setOnLoginListener(new OKHttpUtils.LoginListener() { @Override public void loginSuccess(String json) { goodsListener.GoodsSuccess(json); } @Override public void loginError(String error) { goodsListener.GoodsError(error); } }); } }
然后是Bean
public class GoodsBean { /** * msg : 请求成功 * code : 0 * data : [{"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","itemtype":1,"pid":1,"price":118,"pscid":1,"salenum":0,"sellerid":17,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"},{"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","itemtype":1,"pid":2,"price":299,"pscid":1,"salenum":999,"sellerid":18,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"},{"bargainPrice":111.99,"createtime":"2017-10-03T23:53:28","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":0,"pid":3,"price":198,"pscid":1,"salenum":1234,"sellerid":19,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"},{"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":4,"price":999,"pscid":1,"salenum":356,"sellerid":20,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"},{"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","itemtype":1,"pid":5,"price":88.99,"pscid":1,"salenum":678,"sellerid":21,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"},{"bargainPrice":111.99,"createtime":"2017-10-03T23:53:28","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":0,"pid":6,"price":7.99,"pscid":1,"salenum":234,"sellerid":22,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"},{"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","itemtype":1,"pid":7,"price":120.01,"pscid":1,"salenum":1266,"sellerid":23,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"},{"bargainPrice":111.99,"createtime":"2017-10-03T23:53:28","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":0,"pid":8,"price":324,"pscid":1,"salenum":3000,"sellerid":1,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"},{"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"},{"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","itemtype":1,"pid":10,"price":555.55,"pscid":1,"salenum":0,"sellerid":3,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"}] * page : 1 */ private String msg; private String code; private String page; 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 String getPage() { return page; } public void setPage(String page) { this.page = page; } public List<DataBean> getData() { return data; } public void setData(List<DataBean> data) { this.data = data; } public static class DataBean { /** * 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 * itemtype : 1 * pid : 1 * price : 118 * pscid : 1 * salenum : 0 * sellerid : 17 * subhead : 每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下 * title : 北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g */ private String bargainPrice; private String createtime; private String detailUrl; private String images; private String itemtype; private String pid; private String price; private String pscid; private String salenum; private String sellerid; private String subhead; private String title; public String getBargainPrice() { return bargainPrice; } public void setBargainPrice(String 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 String getItemtype() { return itemtype; } public void setItemtype(String itemtype) { this.itemtype = itemtype; } public String getPid() { return pid; } public void setPid(String pid) { this.pid = pid; } public String getPrice() { return price; } public void setPrice(String price) { this.price = price; } public String getPscid() { return pscid; } public void setPscid(String pscid) { this.pscid = pscid; } public String getSalenum() { return salenum; } public void setSalenum(String salenum) { this.salenum = salenum; } public String getSellerid() { return sellerid; } public void setSellerid(String 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; } } }
适配器
MyAdapter
public class MyAdapter extends XRecyclerView.Adapter<XRecyclerView.ViewHolder> { private static final int TYPE0 = 0; private static final int TYPE1 = 1; private Context context; private List<GoodsBean.DataBean> list; public MyAdapter(Context context, List<GoodsBean.DataBean> list) { this.context = context; this.list = list; } @NonNull @Override public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { if (viewType == TYPE0) { View view = View.inflate(context, R.layout.item, null); MyViewHolder myHolder = new MyViewHolder(view); return myHolder; } else if (viewType == TYPE1) { View view = View.inflate(context, R.layout.item02, null); MyPicHolder picHolder = new MyPicHolder(view); return picHolder; } return null; } @Override public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) { if (holder instanceof MyViewHolder) { MyViewHolder myViewHolder = (MyViewHolder) holder; String images = list.get(position).getImages(); String img = images.split("\\|")[0]; Uri uri = Uri.parse(img); myViewHolder.img.setImageURI(uri); myViewHolder.price01.setText(list.get(position).getTitle()); myViewHolder.price02.setText("¥:" + list.get(position).getBargainPrice()); } else if (holder instanceof MyPicHolder) { MyPicHolder picHolder = (MyPicHolder) holder; String images = list.get(position).getImages(); String img = images.split("\\|")[0]; Uri uri = Uri.parse(img); picHolder.item02_img.setImageURI(uri); picHolder.item02_price01.setText(list.get(position).getTitle()); picHolder.item02_price02.setText("¥:" + list.get(position).getBargainPrice()); } } @Override public int getItemCount() { return list.size(); } @Override public int getItemViewType(int position) { if(position%2==0){ return TYPE0; }else{ return TYPE1; } } public class MyViewHolder extends XRecyclerView.ViewHolder { private final SimpleDraweeView img; private final TextView price01; private final TextView price02; public MyViewHolder(View itemView) { super(itemView); img = itemView.findViewById(R.id.img); price01 = itemView.findViewById(R.id.price01); price02 = itemView.findViewById(R.id.price02); } public SimpleDraweeView getImg() { return img; } public TextView getPrice01() { return price01; } public TextView getPrice02() { return price02; } } public class MyPicHolder extends XRecyclerView.ViewHolder { private final SimpleDraweeView item02_img; private final TextView item02_price01; private final TextView item02_price02; public MyPicHolder(View itemView) { super(itemView); item02_img = itemView.findViewById(R.id.item02_img); item02_price01 = itemView.findViewById(R.id.item02_price01); item02_price02 = itemView.findViewById(R.id.item02_price02); } public SimpleDraweeView getItem02_img() { return item02_img; } public TextView getItem02_price01() { return item02_price01; } public TextView getItem02_price02() { return item02_price02; } } }
P层
IPresenter
public interface IPresenter { void getGoods(IModel iModel, IMainView iMainView); }
public class IPresenterImpl implements IPresenter { @Override public void getGoods(IModel iModel, final IMainView iMainView) { Map<String,String> map=new HashMap<>(); map.put("pscid",iMainView.pscid()+""); iModel.getDate(Httpfig.goods_url, map, new GoodsListener() { @Override public void GoodsSuccess(String json) { Gson gson=new Gson(); GoodsBean goodsBean = gson.fromJson(json, GoodsBean.class); List<GoodsBean.DataBean> data = goodsBean.getData(); iMainView.ShowGoods(data); } @Override public void GoodsError(String error) { } }); } }
最后主MainActivity方法
public class MainActivity extends AppCompatActivity implements IMainView{ private XRecyclerView xr; private static final String TAG = "MainActivity"; private MyAdapter adapter; private int pscid = 1; private Handler mHandler = new Handler(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); xr = findViewById(R.id.recy); xr.setLayoutManager(new LinearLayoutManager(this)); IPresenterImpl presenter = new IPresenterImpl(); presenter.getGoods(new IModelImpl(),this); xr.setLoadingListener(new XRecyclerView.LoadingListener() { @Override public void onRefresh() { MainActivity.this.pscid++; IPresenterImpl presenter = new IPresenterImpl(); presenter.getGoods(new IModelImpl(),MainActivity.this); xr.refreshComplete(); } @Override public void onLoadMore() { MainActivity.this.pscid++; IPresenterImpl presenter = new IPresenterImpl(); presenter.getGoods(new IModelImpl(),MainActivity.this); } }); } @Override public void ShowGoods(List<GoodsBean.DataBean> list) { adapter = new MyAdapter(MainActivity.this,list); xr.setAdapter(adapter); Log.d(TAG, "ShowGoods: "+list.size()); } @Override public int pscid() { return MainActivity.this.pscid; } }