1. 如上图,使用 MVP+xRecyclerView+Retrofit+OkHttp+RxJava
接口:APIKEY=‘71e58b5b2f930eaf1f937407acde08fe’
http://api.tianapi.com/nba/?key=APIKEY&num=10
1. 主页面显示一个xRecycleveiw实现分页加载
2. Retrofit访问网络接口获取数据
3. 添加动态代理
4. Rxjava异步处理数据
5. 设置适配器,将数据列表传入适配器
6. 重写适配器的4个方法,并在显示视图的方法中做优化
7. 在构造方法中初始化Fresco并做配置
8. 配置图片的显示方式为圆形显示
9. 在适配器显示视图的方法中通过Fresco来显示图片
添加依赖:
compile 'io.reactivex:rxjava:1.3.4'
compile 'io.reactivex:rxandroid:1.2.1'
compile 'com.squareup.retrofit2:retrofit:2.3.0'
compile 'com.squareup.retrofit2:converter-gson:2.3.0'
compile 'com.squareup.retrofit2:adapter-rxjava:2.3.0'
compile 'com.squareup.retrofit2:adapter-rxjava:2.3.0-beta4'
compile 'com.squareup.okhttp3:okhttp:3.9.0'
//compile project(':xrecyclerview')
compile 'com.facebook.fresco:fresco:0.12.0'//fresco
compile 'com.jcodecraeer:xrecyclerview:1.3.2'//xrecyclerview
配置权限:
代码:
service接口类import retrofit2.http.GET;
import retrofit2.http.Query;
import rx.Observable;
/**
* Created by Wangrx on 2017/12/5.
*/
public interface BlogService {
@GET("nba/")
Observable
}
RetrofitUtils(封装retrofit网络请求)
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
/**
* Created by Wangrx on 2017/12/5.
*/
public class RetrofitUtils {
private static volatile RetrofitUtils instance;
private Retrofit retrofit;
private RetrofitUtils(){
}
private RetrofitUtils(String baseUrl){
OkHttpClient client = new OkHttpClient();
retrofit = new Retrofit.Builder()
.client(client)
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.baseUrl(baseUrl)
.build();
}
public static RetrofitUtils getInstance(String baseUrl){
if (instance==null){
synchronized (RetrofitUtils.class){
if (null==instance){
instance = new RetrofitUtils(baseUrl);
}
}
}
return instance;
}
public static RetrofitUtils getInstance(){
if (null == instance){
return getInstance("http://api.tianapi.com/");
}
return instance;
}
public Retrofit getRetrofit(){
return retrofit;
}
}
NewsPresenter(逻辑层)
import java.util.List;
import retrofit2.Retrofit;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;
/**
* Created by Wangrx on 2017/12/5.
*/
public class NewsPresenter {
private NewsView inv;
private Subscription subscribe;
public void attachView(NewsView inv){
this.inv = inv;
}
public void getNews(String key,int num){
Retrofit retrofit = RetrofitUtils.getInstance().getRetrofit();
BlogService service = retrofit.create(BlogService.class);
subscribe = service.getUrl(key, num)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Action1() {
@Override
public void call(JavaBean javaBean) {
List list = javaBean.getNewslist();
inv.success(list);
}
}, new Action1() {
@Override
public void call(Throwable throwable) {
inv.failed(throwable.getMessage());
}
});
}
public void detachView(){
// 当Activity销毁的时候取消订阅时间,防止内存泄漏
if (subscribe != null) {
if (subscribe.isUnsubscribed()) {
subscribe.unsubscribe();
}
}
if (inv!=null){
inv = null;
}
}
}
NewsView(数据接口 成功/失败)
import java.util.List;
/**
* Created by Wangrx on 2017/12/5.
*/
public interface NewsView {
void success(List data);
void failed(String e);
}
MainActivity
public class MainActivity extends AppCompatActivity {
private List list = new ArrayList<>();
private int num=10;
private NewsPresenter presenter;
private MyAdapter adapter;
private XRecyclerView xRecyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
xRecyclerView = (XRecyclerView) findViewById(R.id.recyclerview);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
xRecyclerView.setLayoutManager(layoutManager);
Fresco.initialize(this);
getData(num);
xRecyclerView.setPullRefreshEnabled(true);
xRecyclerView.setLoadingMoreEnabled(true);
xRecyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
xRecyclerView.setLoadingMoreProgressStyle(ProgressStyle.Pacman);
xRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
@Override
public void onRefresh() {
new Handler().postDelayed(new Runnable(){
public void run() {
num=10;
getData(num);
xRecyclerView.refreshComplete();
}
}, 2000);
}
@Override
public void onLoadMore() {
new Handler().postDelayed(new Runnable(){
public void run() {
num+=10;
getData(num);
xRecyclerView.refreshComplete();
}
}, 2000);
}
});
}
public void getData(int count){
presenter = new NewsPresenter();
presenter.getNews("71e58b5b2f930eaf1f937407acde08fe", count);
presenter.attachView(new NewsView() {
@Override
public void success(List data) {
list.clear();
list.addAll(data);
Log.i("zzz", "success: " + list.toString());
if (adapter==null){
adapter = new MyAdapter(list, MainActivity.this);
xRecyclerView.setAdapter(adapter);
}else {
adapter.notifyDataSetChanged();
}
}
@Override
public void failed(String e) {
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
if (presenter!=null){
presenter.detachView();
}
}
}
MyAdapter(适配器)
/**
* Created by Wangrx on 2017/12/5.
*/
public class MyAdapter extends RecyclerView.Adapter
private List
private Context context;
public MyAdapter(List
this.datas = datas;
this.context = context;
}
//创建新View,被LayoutManager所调用
@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
View view = View.inflate(context, R.layout.item, null);
ViewHolder vh = new ViewHolder(view);
return vh;
}
//将数据与界面进行绑定的操作
@Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {
viewHolder.item_img.setImageURI(datas.get(position).getPicUrl());
viewHolder.mTextView.setText(datas.get(position).getTitle());
}
//获取数据的数量
@Override
public int getItemCount() {
return datas.size();
}
//自定义的ViewHolder,持有每个Item的的所有界面元素
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView mTextView;
private SimpleDraweeView item_img;
public ViewHolder(View view) {
super(view);
mTextView = (TextView) view.findViewById(R.id.text);
item_img = view.findViewById(R.id.item_img);
}
}
}
fresco