Descrifit + Fresco requests network data and displays it to RecyclerView,
Pull fit + Fresco requests network data and displays it to RecyclerView. First look at the effect, the effect is a bit ugly, but focus on Functions
Step 1: import dependencies
compile 'com.android.support:recyclerview-v7:26.1.0' compile 'com.facebook.fresco:fresco:0.9.0+' compile 'com.squareup.retrofit2:retrofit:2.0.0-beta4' compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta3'
Step 2: Add network Permissions
Below is the code
Display Layout
activity_main
list_item
Api
public class Api { public final static String A="https://120.27.23.105/"; public final static String B = "ad/getAd"; }
Service Interface
public interface Service { @GET(Api.B) Call
getDatas(); }
The MyApp class is used for Fresco initialization.
import com.facebook.drawee.backends.pipeline.Fresco; /** * Created by BAIPEI on 2018/1/1. */ public class MyApp extends Application { @Override public void onCreate() { super.onCreate(); Fresco.initialize(this); } }
Apadper adapter class
import android.content.Context; import android.net.Uri; import android.support.v7.widget.RecyclerView; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import com.facebook.drawee.view.SimpleDraweeView; /** * Created by BAIPEI on 2018/1/1. */ public class Apadper extends RecyclerView.Adapter { Context context; Datas datas; public Apadper(Context context, Datas datas) { this.context = context; this.datas = datas; } @Override public Apadper.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view=View.inflate(context,R.layout.list_item,null); return new MyViewHolder(view); } @Override public void onBindViewHolder(Apadper.MyViewHolder holder, int position) { holder.tv1.setText(datas.getData().get(position).getTitle()); holder.tv2.setText(datas.getData().get(position).getType()+""); holder.sv.setImageURI(Uri.parse(datas.getData().get(position).getIcon())); } @Override public int getItemCount() { return datas.getData().size(); } public class MyViewHolder extends RecyclerView.ViewHolder{ SimpleDraweeView sv; TextView tv1,tv2; public MyViewHolder(View itemView) { super(itemView); tv1=itemView.findViewById(R.id.tv1); tv2=itemView.findViewById(R.id.tv2); sv=itemView.findViewById(R.id.sv); } } }
MainActivity
Import android. OS. bundle; import android. support. v7.app. appCompatActivity; import android. support. v7.widget. linearLayoutManager; import android. support. v7.widget. recyclerView; import android. widget. toast; import using fit2.call; import using fit2.response; import using fit2.0000fit; public class MainActivity extends AppCompatActivity {private RecyclerView mRv; @ Override protected void onCreate (Bundle failed) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); initView (); init (); fit = new fit. builder (). baseUrl (Api. A ). addConverterFactory (GsonConverterFactory. create ()). build (); Service service = fit. create (Service. class); Call
Datas = service. getDatas (); datas. enqueue (new Callback
() {@ Override public void onResponse (Call
Call, Response
Response) {Datas body = response. body (); Toast. makeText (MainActivity. this, "White!", Toast. LENGTH_SHORT ). show (); Apadper apadper = new Apadper (MainActivity. this, body); mRv. setAdapter (apadper);} @ Override public void onFailure (Call
Call, Throwable t) {}});} private void init () {// LinearLayoutManager layout style LinearLayoutManager manager = new LinearLayoutManager (this ); // GridLayoutManager layout style // GridLayoutManager manager = new GridLayoutManager (this, 2); // sets the style mRv. setLayoutManager (manager); // set the VERTICAL direction // to add a split line, you must write this line of code} private void initView () {mRv = (RecyclerView) findViewById (R. id. rv );}}
Datas Bean class
Import java. util. list;/*** Created by BAIPEI on 2018/1/1. */public class Datas {private String msg; private String code; private TuijianBean tuijian; private MiaoshaBean miaosha; private List
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 TuijianBean getTuijian () {return tuijian;} public void setTuijian (TuijianBean tuijian) {this. tuijian = tuijian;} public MiaoshaBean getMiaosha () {return miaosha;} public void setMiaosha (MiaoshaBean miaosha) {this. miaosha = miaosha;} public List
GetData () {return data;} public void setData (List
Data) {this. data = data;} public static class TuijianBean {private String name; private List
List; public String getName () {return name;} public void setName (String name) {this. name = name;} public List
GetList () {return list;} public void setList (List
List) {this. list = list;} public static class ListBean {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 ;}} public static class MiaoshaBean {private String name; private int time; private List
List; public String getName () {return name;} public void setName (String name) {this. name = name;} public int getTime () {return time;} public void setTime (int time) {this. time = time;} public List
GetList () {return list;} public void setList (List
List) {this. list = list;} public static class ListBeanX {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 ;}} public static class DataBean {/*** aid: 1 * createtime: 2017-12-26T21: 49: 44 * icon: https://www.zhaoapi.cn/images/quarter/ad1.png * ProductId: null * title: 13th World's Ruili model competition * type: 0 * url: https://m.mv14449315.icoc.bz/index.jsp */Private int aid; private String createtime; private String icon; private Object productId; private String title; private int type; private String url; public int getAid () {return aid ;} public void setAid (int aid) {this. aid = aid;} public String getCreatetime () {return createtime;} public void setCreatetime (String createtime) {this. createtime = createtime;} public String getIcon () {return icon;} public void setIcon (String icon) {this. icon = icon;} public Object getProductId () {return productId;} public void setProductId (Object productId) {this. productId = productId;} public String getTitle () {return title;} public void setTitle (String title) {this. title = title;} public int getType () {return type;} public void setType (int type) {this. type = type;} public String getUrl () {return url;} public void setUrl (String url) {this. url = url ;}}}