先看效果,效果有点丑,但注重功能
第一步,导依赖
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'
第二步,添加网络权限
<uses-permission android:name="android.permission.INTERNET"/>
下面就是代码
显示布局
activity_main
<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="bwie.com.retrofit.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/rv"
android:layout_width="match_parent"
android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>
</RelativeLayout>
list_item
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/sv"
android:layout_width="120dp"
android:layout_height="80dp"
app:placeholderImage="@mipmap/ic_launcher"
/>
<TextView
android:id="@+id/tv1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/sv"
android:layout_margin="10dp"
android:text="..............."/>
<TextView
android:id="@+id/tv2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/sv"
android:layout_margin="10dp"
android:layout_below="@id/tv1"
android:textColor="#FF0000"
android:text="..............."/>
</RelativeLayout>
Api类
public class Api { public final static String A="http://120.27.23.105/"; public final static String B = "ad/getAd"; }
Service接口
public interface Service { @GET(Api.B) Call<Datas> getDatas(); }
MyApp类用于Fresco的初始化
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适配器类
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<Apadper.MyViewHolder> { 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 retrofit2.Call; import retrofit2.Callback; import retrofit2.GsonConverterFactory; import retrofit2.Response; import retrofit2.Retrofit; public class MainActivity extends AppCompatActivity { private RecyclerView mRv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); init(); Retrofit retrofit = new Retrofit.Builder() .baseUrl(Api.A) .addConverterFactory(GsonConverterFactory.create()) .build(); Service service = retrofit.create(Service.class); Call<Datas> datas = service.getDatas(); datas.enqueue(new Callback<Datas>() { @Override public void onResponse(Call<Datas> call, Response<Datas> response) { Datas body = response.body(); Toast.makeText(MainActivity.this, "白超凡", Toast.LENGTH_SHORT).show(); Apadper apadper=new Apadper(MainActivity.this,body); mRv.setAdapter(apadper); } @Override public void onFailure(Call<Datas> call, Throwable t) { } }); } private void init() { //LinearLayoutManager布局样式 LinearLayoutManager manager=new LinearLayoutManager(this); // GridLayoutManager布局样式 //GridLayoutManager manager = new GridLayoutManager(this, 2); //设置样式 mRv.setLayoutManager(manager); //设置方向 VERTICAL垂直方向 //想要添加分割线,必须要写上这行代码 } private void initView() { mRv = (RecyclerView) findViewById(R.id.rv); } }
Datas Bean类
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<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 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<DataBean> getData() { return data; } public void setData(List<DataBean> data) { this.data = data; } public static class TuijianBean { private String name; private List<ListBean> list; public String getName() { return name; } public void setName(String name) { this.name = name; } public List<ListBean> getList() { return list; } public void setList(List<ListBean> 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<ListBeanX> 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<ListBeanX> getList() { return list; } public void setList(List<ListBeanX> 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 : 第十三界瑞丽模特大赛 * type : 0 * url : http://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; } } }