//导入依赖
compile 'com.squareup.okio:okio:1.5.0' compile 'com.squareup.okhttp3:okhttp:3.2.0' compile 'com.squareup.okhttp3:logging-interceptor:3.4.1' compile 'com.google.code.gson:gson:2.8.2' compile 'com.nineoldandroids:library:2.4.0' compile 'com.github.bumptech.glide:glide:3.7.0' testCompile 'junit:junit:4.12' compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5' compile 'com.jcodecraeer:xrecyclerview:1.3.2'//添加权限
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
android:name=".Utils.Myapp"
activity_main布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.bawei.wss.zk3.MainActivity" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="50dp" android:orientation="horizontal" > <EditText android:id="@+id/ed" android:layout_width="300dp" android:layout_height="50dp" /> <Button android:id="@+id/bt" android:layout_width="wrap_content" android:layout_height="50dp" android:text="搜索" /> <Button android:id="@+id/bu1" android:layout_width="wrap_content" android:layout_height="50dp" android:background="@drawable/grid_icon" android:layout_marginLeft="24dp" /> </LinearLayout> <com.jcodecraeer.xrecyclerview.XRecyclerView android:id="@+id/xre" android:layout_width="match_parent" android:layout_height="match_parent"></com.jcodecraeer.xrecyclerview.XRecyclerView> </LinearLayout>
item1布局
<?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:layout_width="match_parent" android:layout_height="80dp" android:orientation="horizontal"> <ImageView android:id="@+id/ivv" android:layout_width="100dp" android:layout_height="80dp" android:src="@mipmap/ic_launcher" /> <TextView android:id="@+id/tvv" android:layout_width="match_parent" android:layout_height="80dp" android:text="哇哈哈哈" android:layout_marginTop="15dp" /> </LinearLayout> </LinearLayout>
item2布局
<?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" > <ImageView android:id="@+id/ivImage" android:layout_width="wrap_content" android:layout_height="200dp" android:scaleType="centerCrop" /> <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="哈哈哈" android:textSize="20sp" /> </LinearLayout>
Utils包下面的Myapp
public class Myapp extends Application { ImageLoader imageLoader; @Override public void onCreate() { super.onCreate(); File file = this.getCacheDir(); DisplayImageOptions options = new DisplayImageOptions.Builder() .showImageOnLoading(R.mipmap.ic_launcher) // 加载未完成时显示的自定义图片 .showImageForEmptyUri(R.mipmap.ic_launcher) // 链接为空时的占位图 .showImageOnFail(R.mipmap.ic_launcher_round) // 加载失败时显示自定义图片 .resetViewBeforeLoading(false) // 在加载前是否重置 view ,默认为false .delayBeforeLoading(1000) //设置在开始加载前的延迟时间,单位为毫秒 .cacheInMemory(true) // 是否启用内存缓存,默认为false .cacheOnDisk(true) // 是否启用磁盘缓存,默认为false .imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2) // 图片的缩放类型 .bitmapConfig(Bitmap.Config.ARGB_8888) // 图片的色彩格式 .handler(new Handler()) // handler 对象,消息处理 .build(); ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder( this) // max width, max height,即保存的每个缓存文件的最大长宽 .memoryCacheExtraOptions(480, 800) // 线程池内加载的数量 .threadPoolSize(3) // 线程优先级 .threadPriority(Thread.NORM_PRIORITY - 2) .defaultDisplayImageOptions(options) // You can pass your own memory cache implementation你可以通过自己的内存缓存实现 // .memoryCache(new UsingFreqLimitedMemoryCache(2 * 1024 * 1024)) // .memoryCacheSize(2 * 1024 * 1024) //硬盘缓存50MB .diskCacheSize(50 * 1024 * 1024) //将保存的时候的URI名称用MD5 .diskCacheFileNameGenerator(new Md5FileNameGenerator()) // 加密 .diskCacheFileNameGenerator(new HashCodeFileNameGenerator())//将保存的时候的URI名称用HASHCODE加密 .tasksProcessingOrder(QueueProcessingType.LIFO) .diskCacheFileCount(100) //缓存的File数量 // .diskCache(new UnlimitedDiscCache(file))// 自定义缓存路径 // .defaultDisplayImageOptions(DisplayImageOptions.createSimple()) // .imageDownloader(new BaseImageDownloader(context, 5 * 1000, // 30 * 1000)) // connectTimeout (5 s), readTimeout (30 s)超时时间 .writeDebugLogs() // Remove for release app .build(); ImageLoader.getInstance().init(config); } }//View层的一个接口
public interface IShowdataView { public void showdatare(List<RecyclerBean.DataBean> been); }
//Model层
IShowRecyclerModel方法public interface IShowRecyclerModel { public void showdata(String keywords, String page, final OnNetListener<RecyclerBean> onNetListener); }ShowRecyclerModel方法
public class ShowRecyclerModel implements IShowRecyclerModel { Handler handler=new Handler(); public void showdata(String keywords, String page, final OnNetListener<RecyclerBean> onNetListener){ OkHttpUtils.getOkHttpUtils().doGet("http://120.27.23.105/product/searchProducts?keywords=" + keywords + "&page=" + page + "", new Callback() { @Override public void onFailure(Call call, IOException e) { } @Override public void onResponse(Call call, Response response) throws IOException { final String string = response.body().string(); handler.post(new Runnable() { @Override public void run() { Gson gson=new Gson(); RecyclerBean recycykerBean = gson.fromJson(string, RecyclerBean.class); onNetListener.onSuccess(recycykerBean); } }); } }); } }P层public class Showdatapersenter { IShowRecyclerModel model; IShowdataView view; public Showdatapersenter(IShowdataView view) { this.view = view; model = new ShowRecyclerModel(); } public void showdata(String keywords, String page) { model.showdata(keywords, page, new OnNetListener<RecyclerBean>() { @Override public void onSuccess(RecyclerBean recycykerBean) { view.showdatare(recycykerBean.getData()); } @Override public void onFailure(Exception e) { } }); } }
//OkHttp包网络请求OkHttpUtils
public class OkHttpUtils { private static OkHttpUtils okHttpUtils; private final OkHttpClient client; private OkHttpUtils() { client = new OkHttpClient.Builder() .addInterceptor(new MyInterceptor()) .build(); } public static OkHttpUtils getOkHttpUtils() { if (okHttpUtils == null) { synchronized (OkHttpUtils.class) { if (okHttpUtils == null) { okHttpUtils = new OkHttpUtils(); } } } return okHttpUtils; } /** * GET请求 * * @param url * @param callback */ public void doGet(String url, Callback callback) { Request request = new Request.Builder().url(url).build(); client.newCall(request).enqueue(callback); } /** * POST请求 * * @param url * @param params * @param callback */ public void doPost(String url, Map<String, String> params, Callback callback) { if (params == null) { throw new RuntimeException("参数为空了"); } FormBody.Builder builder = new FormBody.Builder(); for (Map.Entry<String, String> entry : params.entrySet()) { builder.add(entry.getKey(), entry.getValue()); } FormBody formBody = builder.build(); Request request = new Request.Builder().url(url).post(formBody).build(); client.newCall(request).enqueue(callback); } }//MyInterceptor类的方法
public class MyInterceptor implements Interceptor { @Override public Response intercept(Chain chain) throws IOException { Request request = chain.request(); if (request.method().equals("GET")) { String url = request.url().url().toString(); url += "&source=android"; Request newRequset = request.newBuilder().url(url).build(); return chain.proceed(newRequset); } else { FormBody formBody = (FormBody) request.body(); FormBody.Builder builder = new FormBody.Builder(); for (int i = 0; i < formBody.size(); i++) { builder.add(formBody.name(i), formBody.value(i)); } builder.add("source", "android"); FormBody newFormBody = builder.build(); Request newRequest = request.newBuilder().url(request.url().url().toString()).post(newFormBody).build(); return chain.proceed(newRequest); } } }OnNetListener类
public interface OnNetListener<T> { public void onSuccess(T t); public void onFailure(Exception e); }
Bean类
public class RecyclerBean { /** * msg : 查询成功 * code : 0 * data : [{"bargainPrice":11800,"createtime":"2017-10-10T17:33:37","detailUrl":"https://item.m.jd.com/product/4338107.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t6700/155/2098998076/156185/6cf95035/595dd5a5Nc3a7dab5.jpg!q70.jpg","itemtype":0,"pid":57,"price":5199,"pscid":40,"salenum":4343,"sellerid":1,"subhead":"【i5 MX150 2G显存】全高清窄边框 8G内存 256固态硬盘 支持指纹识别 预装WIN10系统","title":"小米(MI)Air 13.3英寸全金属轻薄笔记本(i5-7200U 8G 256G PCle SSD MX150 2G独显 FHD 指纹识别 Win10)银\r\n"},{"bargainPrice":11800,"createtime":"2017-10-14T21:38:26","detailUrl":"https://item.m.jd.com/product/5025518.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t8830/106/1760940277/195595/5cf9412f/59bf2ef5N5ab7dc16.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5428/70/1520969931/274676/b644dd0d/591128e7Nd2f70da0.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5566/365/1519564203/36911/620c750c/591128eaN54ac3363.jpg!q70.jpg","itemtype":1,"pid":58,"price":6399,"pscid":40,"salenum":545,"sellerid":2,"subhead":"升级4G大显存!Nvme协议Pcie SSD,速度快人一步】GTX1050Ti就选拯救者!专业游戏键盘&新模具全新设计!","title":"联想(Lenovo)拯救者R720 15.6英寸游戏笔记本电脑(i5-7300HQ 8G 1T+128G SSD GTX1050Ti 4G IPS 黑)"},{"bargainPrice":5599,"createtime":"2017-10-10T17:30:32","detailUrl":"https://item.m.jd.com/product/4824715.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term=QQfriends","images":"https://m.360buyimg.com/n12/jfs/t7768/184/1153704394/148460/f42e1432/599a930fN8a85626b.jpg!q70.jpg","itemtype":0,"pid":59,"price":5599,"pscid":40,"salenum":675,"sellerid":3,"subhead":"游戏本选择4G独显,拒绝掉帧】升级版IPS全高清防眩光显示屏,WASD方向键颜色加持,三大出风口立体散热!","title":"戴尔DELL灵越游匣15PR-6648B GTX1050 15.6英寸游戏笔记本电脑(i5-7300HQ 8G 128GSSD+1T 4G独显 IPS)黑"},{"bargainPrice":11800,"createtime":"2017-10-14T21:48:08","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","itemtype":2,"pid":60,"price":13888,"pscid":40,"salenum":466,"sellerid":4,"subhead":"购买电脑办公部分商品满1元返火车票5元优惠券(返完即止)","title":"全球购 新款Apple MacBook Pro 苹果笔记本电脑 银色VP2新13英寸Bar i5/8G/256G"},{"bargainPrice":11800,"createtime":"2017-10-14T21:38:26","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","itemtype":1,"pid":61,"price":14999,"pscid":40,"salenum":5535,"sellerid":5,"subhead":"购买电脑办公部分商品满1元返火车票5元优惠券(返完即止)","title":"全球购 新款Apple MacBook Pro 苹果笔记本电脑 银色VP2新13英寸Bar i5/8G/256G"},{"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","itemtype":0,"pid":62,"price":15999,"pscid":40,"salenum":43,"sellerid":6,"subhead":"购买电脑办公部分商品满1元返火车票5元优惠券(返完即止)","title":"全球购 新款Apple MacBook Pro 苹果笔记本电脑 银色VP2新13英寸Bar i5/8G/256G"},{"bargainPrice":11800,"createtime":"2017-10-14T21:38:26","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","itemtype":1,"pid":63,"price":10000,"pscid":40,"salenum":3232,"sellerid":7,"subhead":"购买电脑办公部分商品满1元返火车票5元优惠券(返完即止)","title":"全球购 新款Apple MacBook Pro 苹果笔记本电脑 银色VP2新13英寸Bar i5/8G/256G"},{"bargainPrice":11800,"createtime":"2017-10-03T23:43:53","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","itemtype":0,"pid":64,"price":11000,"pscid":40,"salenum":0,"sellerid":8,"subhead":"购买电脑办公部分商品满1元返火车票5元优惠券(返完即止)","title":"全球购 新款Apple MacBook Pro 苹果笔记本电脑 银色VP2新13英寸Bar i5/8G/256G"},{"bargainPrice":11800,"createtime":"2017-10-14T21:48:08","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","itemtype":2,"pid":65,"price":12000,"pscid":40,"salenum":868,"sellerid":9,"subhead":"购买电脑办公部分商品满1元返火车票5元优惠券(返完即止)","title":"全球购 新款Apple MacBook Pro 苹果笔记本电脑 银色VP2新13英寸Bar i5/8G/256G"},{"bargainPrice":11800,"createtime":"2017-10-14T21:38:26","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","itemtype":1,"pid":66,"price":13000,"pscid":40,"salenum":7676,"sellerid":10,"subhead":"购买电脑办公部分商品满1元返火车票5元优惠券(返完即止)","title":"全球购 新款Apple MacBook Pro 苹果笔记本电脑 银色VP2新13英寸Bar i5/8G/256G"}] * 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 : 11800.0 * createtime : 2017-10-10T17:33:37 * detailUrl : https://item.m.jd.com/product/4338107.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term=QQfriends * images : https://m.360buyimg.com/n0/jfs/t6700/155/2098998076/156185/6cf95035/595dd5a5Nc3a7dab5.jpg!q70.jpg * itemtype : 0 * pid : 57 * price : 5199.0 * pscid : 40 * salenum : 4343 * sellerid : 1 * subhead : 【i5 MX150 2G显存】全高清窄边框 8G内存 256固态硬盘 支持指纹识别 预装WIN10系统 * title : 小米(MI)Air 13.3英寸全金属轻薄笔记本(i5-7200U 8G 256G PCle SSD MX150 2G独显 FHD 指纹识别 Win10)银 */ private double bargainPrice; private String createtime; private String detailUrl; private String images; private int itemtype; private int pid; private double price; private int pscid; private int salenum; private int sellerid; private String subhead; private String title; public double getBargainPrice() { return bargainPrice; } public void setBargainPrice(double bargainPrice) { this.bargainPrice = bargainPrice; } public String getCreatetime() { return createtime; } public void setCreatetime(String createtime) { this.createtime = createtime; } public String getDetailUrl() { return detailUrl; } public void setDetailUrl(String detailUrl) { this.detailUrl = detailUrl; } public String getImages() { return images; } public void setImages(String images) { this.images = images; } public int getItemtype() { return itemtype; } public void setItemtype(int itemtype) { this.itemtype = itemtype; } public int getPid() { return pid; } public void setPid(int pid) { this.pid = pid; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public int getPscid() { return pscid; } public void setPscid(int pscid) { this.pscid = pscid; } public int getSalenum() { return salenum; } public void setSalenum(int salenum) { this.salenum = salenum; } public int getSellerid() { return sellerid; } public void setSellerid(int sellerid) { this.sellerid = sellerid; } public String getSubhead() { return subhead; } public void setSubhead(String subhead) { this.subhead = subhead; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } } }
Adapter方法
public class Myadaper extends RecyclerView.Adapter { private Context context; private List<RecyclerBean.DataBean> list; boolean flag=true; public Myadaper(Context context, List<RecyclerBean.DataBean> list, boolean flag) { this.context = context; this.list = list; this.flag=flag; } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { if(flag){ View view = View.inflate(context, R.layout.adaperone, null); Myviewhollder1 myviewhollder1=new Myviewhollder1(view); return myviewhollder1; }else { View view = View.inflate(context, R.layout.adapertwo, null); Myviewholder2 myviewhollder2=new Myviewholder2(view); return myviewhollder2; } } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { if(flag) { if(holder instanceof Myviewhollder1){ Myviewhollder1 myviewhollder1= (Myviewhollder1) holder; String[] split = list.get(position).getImages().split("\\|"); ImageLoader.getInstance().displayImage(split[0], myviewhollder1.ivv); myviewhollder1.tvv.setText(list.get(position).getTitle()); } }else { if(holder instanceof Myviewholder2){ Myviewholder2 myviewholder2= (Myviewholder2) holder; String[] split = list.get(position).getImages().split("\\|"); ImageLoader.getInstance().displayImage(split[0], myviewholder2.iv); myviewholder2.tv.setText(list.get(position).getTitle()); } } } @Override public int getItemCount() { return list==null ? 0 : list.size(); } class Myviewhollder1 extends RecyclerView.ViewHolder{ ImageView ivv; TextView tvv; public Myviewhollder1(View itemView) { super(itemView); ivv = itemView.findViewById(R.id.ivv); tvv = itemView.findViewById(R.id.tvv); } } class Myviewholder2 extends RecyclerView.ViewHolder{ ImageView iv; TextView tv; public Myviewholder2(View itemView) { super(itemView); iv=itemView.findViewById(R.id.ivImage); tv=itemView.findViewById(R.id.tv); } } }
MainActivity方法
public class MainActivity extends Activity implements IShowdataView { Showdatapersenter presenter; XRecyclerView xre; int shuxin=1;//定义的变量,用来刷新加载用 EditText ed; Button bt,bu1; Handler handler=new Handler(); List<RecyclerBean.DataBean> list=new ArrayList<>(); String name="笔记本"; int num=1; Myadaper adaper; boolean flag=true; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initview(); presenter=new Showdatapersenter(this); String s = String.valueOf(num); presenter.showdata(name,s); setadaper(); //点击切换布局的方法 bu1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if(flag==false){ flag=true; bu1.setBackgroundResource(R.drawable.grid_icon); xre.setLayoutManager(new LinearLayoutManager(MainActivity.this,LinearLayoutManager.VERTICAL,false)); adaper = new Myadaper(MainActivity.this, list,flag); xre.setAdapter(adaper); adaper.notifyDataSetChanged(); }else{ flag =false; bu1.setBackgroundResource(R.drawable.lv_icon); xre.setLayoutManager(new GridLayoutManager(MainActivity.this,2)); adaper = new Myadaper(MainActivity.this, list,flag); xre.setAdapter(adaper); adaper.notifyDataSetChanged(); } } }); //刷新和加载的方法 xre.setLoadingListener(new XRecyclerView.LoadingListener() { @Override public void onRefresh() { handler.postDelayed(new Runnable() { @Override public void run() { num=1; presenter.showdata(name,num+""); setadaper(); xre.refreshComplete(); } },2000); } @Override public void onLoadMore() { handler.postDelayed(new Runnable() { @Override public void run() { num++; presenter.showdata(name,num+""); setadaper(); xre.loadMoreComplete(); } },2000); } }); //搜索的方法 bt.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String s1 = ed.getText().toString().trim(); //判断得到输入框的值 //不为空的时候走第一个 //为空的时候走第二个 if(!TextUtils.isEmpty(s1)) { list.clear(); presenter.showdata(s1,1+""); setadaper(); }else { list.clear(); presenter.showdata(name,1+""); setadaper(); } } }); } private void setadaper() { //适配器 if(adaper==null) { xre.setLayoutManager(new LinearLayoutManager(MainActivity.this,LinearLayoutManager.VERTICAL,false)); adaper = new Myadaper(MainActivity.this, list,flag); xre.setAdapter(adaper); }else{ adaper.notifyDataSetChanged(); } } private void initview() { xre=findViewById(R.id.xre); ed=findViewById(R.id.ed); bt=findViewById(R.id.bt); bu1=findViewById(R.id.bu1); } @Override public void showdatare(List<RecyclerBean.DataBean> been) { list.addAll(been); setadaper(); } }