//依赖
implementation files('libs/universal-image-loader-1.9.3-with-sources.jar') compile 'com.android.support:recyclerview-v7:23.4.0' compile 'com.squareup.okhttp3:okhttp:3.9.1' implementation files('libs/gson-2.2.4.jar')
//清单文件
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />android:name=".MyApplication"//activity_main
<?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="com.example.mvvp.MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="10dp" android:text="搜索商品" android:textSize="24sp" /> <ImageView android:id="@+id/imgView" android:layout_width="30dp" android:layout_height="30dp" android:layout_alignParentRight="true" android:layout_marginRight="10dp" android:layout_marginTop="10dp" android:src="@drawable/grid_icon" /> <View android:layout_width="match_parent" android:layout_height="2dp" android:layout_marginTop="50dp" android:background="#666666" /> <EditText android:layout_width="300dp" android:layout_height="40dp" android:layout_marginLeft="30dp" android:layout_marginTop="65dp" android:background="@drawable/wenben" android:drawableLeft="@drawable/sear" android:hint="请输入关键字" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_marginRight="10dp" android:layout_marginTop="60dp" android:id="@+id/btn_sou" android:text="搜索" /> <android.support.v7.widget.RecyclerView android:id="@+id/rv" android:layout_marginTop="130dp" android:layout_width="wrap_content" android:layout_height="wrap_content"></android.support.v7.widget.RecyclerView> </RelativeLayout>//book_layout
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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/book_img" android:layout_width="100dp" android:layout_height="100dp" android:layout_marginLeft="10dp" android:layout_marginTop="20dp" android:src="@mipmap/ic_launcher" /> <TextView android:id="@+id/book_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="120dp" android:layout_marginTop="20dp" android:text="123" /> <TextView android:id="@+id/text_oldprice" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="120dp" android:layout_marginTop="70dp" android:text="456" /> <TextView android:id="@+id/text_newprice" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="300dp" android:layout_marginTop="70dp" android:textColor="#F23707" android:text="234555" /> </RelativeLayout>//var_layout
<?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/book_img" android:layout_width="100dp" android:layout_height="100dp" android:layout_marginLeft="10dp" android:layout_marginTop="20dp" android:src="@mipmap/ic_launcher" /> <TextView android:id="@+id/book_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:layout_marginTop="5dp" android:text="123" /> <TextView android:id="@+id/text_oldprice" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:layout_marginTop="5dp" android:text="456" /> <TextView android:id="@+id/text_newprice" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:layout_marginTop="5dp" android:textColor="#F23707" android:text="234555" /> </LinearLayout>//MainActivity
package com.example.mvvp; import android.os.Build; import android.os.Bundle; import android.support.annotation.RequiresApi; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.GridLayoutManager; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.View; import android.widget.ImageView; import com.example.mvvp.adapter.MyAdapter; import com.example.mvvp.bean.Bijiben; import com.example.mvvp.presenter.BookPre; import com.example.mvvp.view.LoginView; import java.util.List; public class MainActivity extends AppCompatActivity implements LoginView { private RecyclerView rv; private int q = 1; private boolean flag = true; private ImageView imgView; private MyAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); rv = (RecyclerView) findViewById(R.id.rv); imgView = (ImageView) findViewById(R.id.imgView); BookPre bookPre = new BookPre(this); bookPre.getBijiuben(); // rv.setAdapter(new MyAdapter(MainActivity.this, list, q)); } @Override public void onSuccess(final List<Bijiben> list) { //Log.i("111",list.toString()+""); //rv.setLayoutManager(new LinearLayoutManager(MainActivity.this, LinearLayoutManager.VERTICAL, false)); // rv.setAdapter(new MyAdapter(MainActivity.this, list, q)); //设置分割线 // rv.addItemDecoration(new ); imgView.setOnClickListener(new View.OnClickListener() { @RequiresApi(api = Build.VERSION_CODES.GINGERBREAD) @Override public void onClick(View view) { if (flag) { LinearLayoutManager manager = new LinearLayoutManager(MainActivity.this, LinearLayoutManager.VERTICAL, false); rv.setLayoutManager(manager); adapter = new MyAdapter(MainActivity.this, list, 2); rv.setAdapter(adapter); imgView.setImageDrawable(getResources().getDrawable(R.drawable.lv_icon)); flag = false; } else { GridLayoutManager manager = new GridLayoutManager(MainActivity.this, 2, LinearLayoutManager.VERTICAL, false); rv.setLayoutManager(manager); adapter = new MyAdapter(MainActivity.this, list, 1); rv.setAdapter(adapter); imgView.setImageDrawable(getResources().getDrawable(R.drawable.grid_icon)); flag = true; } } }); } @Override public void onFailed() { } }//MyApplication
package com.example.mvvp; import android.app.Application; /** * Created by 哥哥 on 2017/12/11 0011. */ public class MyApplication extends Application { @Override public void onCreate() { super.onCreate(); //进行全局初使化 ImageloaderUtil.initConfig(this); } }
//ImageloaderUtil
package com.example.mvvp; import android.content.Context; import android.graphics.Bitmap; import android.os.Environment; import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiscCache; import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator; import com.nostra13.universalimageloader.core.DisplayImageOptions; import com.nostra13.universalimageloader.core.ImageLoader; import com.nostra13.universalimageloader.core.ImageLoaderConfiguration; import java.io.File; public class ImageloaderUtil { /** * ImageLoader的配置 * * @param context */ public static void initConfig(Context context) { //配置 // File cacheFile=context.getExternalCacheDir(); File cacheFile = new File(Environment.getExternalStorageDirectory() + "/" + "imgages"); ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context) .memoryCacheExtraOptions(480, 800)//缓存图片最大的长和宽 .threadPoolSize(2)//线程池的数量 .threadPriority(4) .memoryCacheSize(2 * 1024 * 1024)//设置内存缓存区大小 .diskCacheSize(20 * 1024 * 1024)//设置sd卡缓存区大小 .diskCache(new UnlimitedDiscCache(cacheFile))//自定义缓存目录 .writeDebugLogs()//打印日志内容 .diskCacheFileNameGenerator(new Md5FileNameGenerator())//给缓存的文件名进行md5加密处理 .build(); ImageLoader.getInstance().init(config); } /** * 获取图片设置类 * * @return */ public static DisplayImageOptions getImageOptions() { DisplayImageOptions optionsoptions = new DisplayImageOptions.Builder() .cacheInMemory(true)//使用内存缓存 .cacheOnDisk(true)//使用磁盘缓存 .bitmapConfig(Bitmap.Config.RGB_565)//设置图片格式 .build(); return optionsoptions; } }//view//LoginView
package com.example.mvvp.view; import com.example.mvvp.bean.Bijiben; import java.util.List; public interface LoginView { void onSuccess(List<Bijiben> list); void onFailed(); }
//presenter
//BookPresenter
package com.example.mvvp.presenter; import com.example.mvvp.bean.Bijiben; import java.util.List; public interface BookPresenter { void onSuccess(List<Bijiben> list); void onFailed(); }
//BookPre
package com.example.mvvp.presenter; import com.example.mvvp.bean.Bijiben; import com.example.mvvp.model.BookModel; import com.example.mvvp.view.LoginView; import java.util.List; public class BookPre implements BookPresenter { private LoginView loginView; private BookModel bookModel; public BookPre(LoginView loginView) { this.loginView = loginView; bookModel = new BookModel(); } public void getBijiuben() { bookModel.getBijiuben(this); } @Override public void onSuccess(List<Bijiben> list) { loginView.onSuccess(list); } @Override public void onFailed() { } }
//model
//BookModel
package com.example.mvvp.model; import android.os.Handler; import com.example.mvvp.bean.Bijiben; import com.example.mvvp.bean.Books; import com.example.mvvp.presenter.BookPresenter; import com.google.gson.Gson; import java.io.IOException; import java.util.ArrayList; import java.util.List; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; public class BookModel { //定义一个数据集合 private static List<Bijiben> list = new ArrayList<>(); //定义一个handler private static Handler handler = new Handler(); //请求数据的方法 public void getBijiuben(final BookPresenter bookPresenter) { new Thread() { @Override public void run() { super.run(); try { //OK请求数据 OkHttpClient okHttpClient = new OkHttpClient(); Request request = new Request.Builder() .get() .url("http://120.27.23.105/product/searchProducts?source=android&keywords=笔记本&page=1") .build(); //准备发送网络请求 okhttp3.Call call = okHttpClient.newCall(request); //执行请求 Response response = call.execute(); //得到请求的结果 String result = response.body().string(); //然后解析数据 Gson gson = new Gson(); Books books = gson.fromJson(result.toString(), Books.class); List<Books.DataBean> data = books.getData(); for (int i = 0; i < data.size(); i++) { String title = data.get(i).getTitle(); double newprice = data.get(i).getPrice(); double bargainPrice = data.get(i).getBargainPrice(); String images = data.get(i).getImages(); list.add(new Bijiben(images, title, bargainPrice, newprice)); } handler.post(new Runnable() { @Override public void run() { bookPresenter.onSuccess(list); } }); } catch (IOException e) { e.printStackTrace(); } } }.start(); } }//bean//Bijiben
package com.example.mvvp.bean; public class Bijiben { private String img; private String title; private double oldprice; private double newprice; public String getImg() { return img; } public void setImg(String img) { this.img = img; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public double getOldprice() { return oldprice; } public void setOldprice(double oldprice) { this.oldprice = oldprice; } public double getNewprice() { return newprice; } public void setNewprice(double newprice) { this.newprice = newprice; } @Override public String toString() { return "Bijiben{" + "img='" + img + '\'' + ", title='" + title + '\'' + ", oldprice=" + oldprice + ", newprice=" + newprice + '}'; } public Bijiben(String img, String title, double oldprice, double newprice) { this.img = img; this.title = title; this.oldprice = oldprice; this.newprice = newprice; } }
//Books
package com.example.mvvp.bean; import java.util.List; public class Books { /** * msg : 查询成功 * code : 0 * data : [{"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":77,"price":38999.99,"pscid":40,"salenum":7757,"sellerid":21,"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"},{"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":76,"price":37999.99,"pscid":40,"salenum":6868,"sellerid":20,"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":69,"price":16999,"pscid":40,"salenum":6645,"sellerid":13,"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-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":75,"price":36999,"pscid":40,"salenum":5454,"sellerid":19,"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":79,"price":888,"pscid":40,"salenum":5454,"sellerid":23,"subhead":"购买电脑办公部分商品满1元返火车票5元优惠券(返完即止)","title":"全球购 新款Apple MacBook Pro 苹果笔记本电脑 银色VP2新13英寸Bar i5/8G/256G"},{"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-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":71,"price":32999,"pscid":40,"salenum":4242,"sellerid":15,"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"}] * 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-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 : 77 * price : 38999.99 * pscid : 40 * salenum : 7757 * sellerid : 21 * subhead : 购买电脑办公部分商品满1元返火车票5元优惠券(返完即止) * title : 全球购 新款Apple MacBook Pro 苹果笔记本电脑 银色VP2新13英寸Bar i5/8G/256G */ 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//MyAdapter
package com.example.mvvp.adapter; import android.content.Context; import android.support.v7.widget.RecyclerView; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; import com.example.mvvp.R; import com.example.mvvp.bean.Bijiben; import com.nostra13.universalimageloader.core.ImageLoader; import java.util.List; public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> { private Context context; private List<Bijiben> list; private int q; public MyAdapter(Context context, List<Bijiben> list, int q) { this.context = context; this.list = list; this.q = q; } @Override public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { if (q == 1) { View view = LayoutInflater.from(context).inflate(R.layout.book_layout, null); ViewHolder viewHolder = new ViewHolder(view); return viewHolder; } else { View view = LayoutInflater.from(context).inflate(R.layout.var_layout, null); ViewHolder viewHolder = new ViewHolder(view); return viewHolder; } } @Override public void onBindViewHolder(MyAdapter.ViewHolder holder, int position) { holder.text.setText(list.get(position).getTitle()); holder.newprice.setText(list.get(position).getNewprice() + ""); holder.oldprice.setText(list.get(position).getOldprice() + ""); String img = list.get(position).getImg(); String[] split = img.split("\\|"); ImageLoader.getInstance().displayImage(split[0], holder.img); } @Override public int getItemCount() { if (list != null) { return list.size(); } else { Log.i("12", list.size() + "放松放松"); } return 0; } class ViewHolder extends RecyclerView.ViewHolder { ImageView img; TextView text; TextView oldprice; TextView newprice; public ViewHolder(View itemView) { super(itemView); img = (ImageView) itemView.findViewById(R.id.book_img); text = (TextView) itemView.findViewById(R.id.book_text); oldprice = (TextView) itemView.findViewById(R.id.text_oldprice); newprice = (TextView) itemView.findViewById(R.id.text_newprice); } } }