package com.example.lenvov.myapplication;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.facebook.drawee.view.SimpleDraweeView;
import java.util.List;
/**
* Created by lenvov on 2018/4/21.
*/
public class MyAdapter extends RecyclerView.Adapter{
private List mData;
private Context context;
public MyAdapter(List mData, Context context) {
this.mData = mData;
this.context = context;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(context).inflate(R.layout.view_rv_item,parent, false);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
// 绑定数据
holder.mTv.setText(mData.get(position).getTitle());
holder.mImg.setImageURI(mData.get(position).getImage());
}
@Override
public int getItemCount() {
return mData.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
TextView mTv;
SimpleDraweeView mImg;
public ViewHolder(View itemView) {
super(itemView);
mTv = itemView.findViewById(R.id.item_tv);
mImg = itemView.findViewById(R.id.item_img);
}
}
}
package com.example.lenvov.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class MainActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
private MyAdapter myAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRecyclerView = findViewById(R.id.rv);
Retrofit.Builder builder = new Retrofit.Builder();
Retrofit retrofit = builder.baseUrl("http://app.vmoiver.com/apiv3/post/")
.addConverterFactory(GsonConverterFactory.create())
.build();
MyInterface myInterface = retrofit.create(MyInterface.class);
Call call = myInterface.getList();
call.enqueue(new Callback() {
@Override
public void onResponse(Call call, Response response) {
List data = response.body().getData();
myAdapter = new MyAdapter(data,MainActivity.this);
mRecyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));
mRecyclerView.setAdapter(myAdapter);
}
@Override
public void onFailure(Call call, Throwable t) {
}
});
}
}
Fresco.initialize(this);
@GET("getPostInCate?cateid=0&p=1")
Call getList();
compile 'com.squareup.retrofit2:converter-gson:2.3.0'
compile 'com.android.support:recyclerview-v7:23.4.0'
compile 'com.facebook.fresco:animated-base-support:0.12.0'
compile 'com.facebook.fresco:animated-webp:0.12.0'
compile 'com.facebook.fresco:webpsupport:0.12.0'
compile 'com.facebook.fresco:fresco:0.14.1'