recyclerView和retrofit的简单使用
第一步:导入相关包
//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 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'第二步:对retrofit进行简单封装,不懂的可以看下官网http://square.github.io/retrofit/
public class OkHttpManager { /** * OkHttpClient的封装 */ public OkHttpClient okHttpClient() { // 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(); } }第三步:对详细信息进行封装(AppInfo):
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); }