recyclerView和retrofit的簡單使用
阿新 • • 發佈:2019-01-02
第一步:匯入相關包
//butterknife compile 'com.jakewharton:butterknife:8.4.0' annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0' //retrofit compile 'com.squareup.retrofit2:retrofit:2.1.0' compile 'com.squareup.retrofit2:adapter-rxjava:2.1.0' compile 'com.squareup.retrofit2:converter-gson:2.1.0' // okHttp第二步:對retrofit進行簡單封裝,不懂的可以看下官網http://square.github.io/retrofit/compile 'com.squareup.okhttp3:okhttp:3.4.1' compile 'com.squareup.okhttp3:logging-interceptor:3.4.1' //picasso compile 'com.squareup.picasso:picasso:2.5.2'
public class OkHttpManager { /** * OkHttpClient的封裝 */ public OkHttpClient okHttpClient第三步:對詳細資訊進行封裝(AppInfo):() { // log用攔截器 HttpLoggingInterceptor logger = new HttpLoggingInterceptor(); // 開發模式記錄整個body,否則只記錄基本資訊如返回200,http協議版本等 // 如果使用到HTTPS,我們需要建立SSLSocketFactory,並設定到client // SSLSocketFactory sslSocketFactory = null; logger.setLevel(HttpLoggingInterceptor.Level.BODY); return new OkHttpClient.Builder() // HeadInterceptor實現了Interceptor,用來往Request Header新增一些業務相關資料,如APP版本,token資訊//.addInterceptor(new HeadInterceptor()) .addInterceptor(logger)//新增攔截器 .connectTimeout(10, TimeUnit.SECONDS) // 連線超時時間設定 .readTimeout(10, TimeUnit.SECONDS) // 讀取超時時間設定 .build(); } /** * Retrofit的封裝 */ public Retrofit getRetrofit(OkHttpClient okHttpClient) { Retrofit.Builder builder = new Retrofit.Builder() .baseUrl(ApiServer.BASE_URL) .addConverterFactory(GsonConverterFactory.create()) .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) .client(okHttpClient);//不設定則使用的是預設的OKHttpClient return builder.build(); } }
private String icon; private int appendSize; private String displayName;第四步:對所有資訊進行封裝(PageBean):
private boolean hasMore; private int status; private String message; private List<T> datas;第五步:定義Retrofit的介面
public interface ApiServer { public static final String BASE_URL = "http://112.124.22.238:8081/course_api/cniaoplay/"; //完整URL:http://112.124.22.238:8081/course_api/cniaoplay/featured?p={"page":0} @GET("featured") Call<PageBean<AppInfo>> getApps(@Query("p") String jsonParms); }第六步:執行Retrofit:
private void initRetrofit() { OkHttpManager httpManager = new OkHttpManager(); ApiServer apiServer = httpManager.getRetrofit(httpManager.okHttpClient()) .create(ApiServer.class); apiServer.getApps("{'page':0}").enqueue(new Callback<PageBean<AppInfo>>() { @Override public void onResponse(Call<PageBean<AppInfo>> call, Response<PageBean<AppInfo>> response) { PageBean<AppInfo> body = response.body(); List<AppInfo> datas = body.getDatas(); Log.e(TAG,datas.toString()); //設定介面卡 initData(datas); } @Override public void onFailure(Call<PageBean<AppInfo>> call, Throwable t) { } }); }第七步:定義RecyclerVeiw的介面卡
public class GameAdapter extends RecyclerView.Adapter<GameAdapter.ViewHolder> { private Context mContext; private List<AppInfo> appInfos; private LayoutInflater mLayoutInflater; public GameAdapter(Context context, List<AppInfo> appInfos) { this.mContext = context; this.appInfos = appInfos; mLayoutInflater = LayoutInflater.from(context); } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { return new ViewHolder(mLayoutInflater.inflate(R.layout.item_game, null)); } @Override public void onBindViewHolder(ViewHolder holder, int position) { AppInfo appInfo = appInfos.get(position); String baseImgUrl ="http://file.market.xiaomi.com/mfc/thumbnail/png/w150q80/"; Picasso.with(mContext).load(baseImgUrl +appInfo.getIcon()).into(holder.imgIcon); holder.textTitle.setText(appInfo.getDisplayName()); holder.textSize.setText((appInfo.getApkSize() / 1024 /1024) +" MB"); } @Override public int getItemCount() { return appInfos.size(); } class ViewHolder extends RecyclerView.ViewHolder { @BindView(R.id.img_icon) ImageView imgIcon; @BindView(R.id.text_title) TextView textTitle; @BindView(R.id.text_size) TextView textSize; @BindView(R.id.btn_dl) Button btnDl; public ViewHolder(View itemView) { super(itemView); ButterKnife.bind(this, itemView); } } }第八步:MainActivity中recycleview設定配置器
private void initData(List<AppInfo> datas) { recylerView.setLayoutManager(new LinearLayoutManager(MainActivity.this)); recylerView.setItemAnimator(new DefaultItemAnimator()); adapter=new GameAdapter(MainActivity.this,datas); recylerView.setAdapter(adapter); }