Retrofit+OkHttpClient+Fresco+XRecyclerView+RxJava实现数据展示

需要的依赖包
compile 'com.squareup.retrofit2:retrofit:2.0.2'
compile 'com.squareup.retrofit2:converter-gson:2.0.2'
compile 'com.squareup.retrofit2:adapter-rxjava:2.1.0'

compile 'io.reactivex:rxjava:1.0.14'
compile 'io.reactivex:rxandroid:1.0.1'

// Okhttp库
compile 'com.squareup.okhttp3:okhttp:3.1.2'
compile 'com.facebook.fresco:fresco:1.5.0'
compile 'com.android.support:recyclerview-v7:26.1.0'
compile 'com.jcodecraeer:xrecyclerview:1.3.2'

初始化App-加载图片并注册(清单文件)
public class MyApp extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        Fresco.initialize(this);
    }
}
失败和成功回调的接口
public interface OnNetListener<T> {
    public void onSuccess(T t);
    public void onFailure(Exception e);
}
Api-网络请求地址
public class Api {
    public static final String HOST = "http://api.tianapi.com/nba/";
    public static final String URL = "?key=71e58b5b2f930eaf1f937407acde08fe&num=10";
}
ServerApi-Retrofit需要用到的接口
public interface ServerApi {
    @GET(Api.URL)
    Observable<ClassBean> call();
}
Retrofit的工具类-创建OkHttpClient和ServerApi请求
public class RetrofitHelper {
    public static OkHttpClient client;
    public static ServerApi serverApi;
    static {
        getClient();
    }

    public static OkHttpClient getClient() {
        if (client == null){
            synchronized (OkHttpClient.class){
                if (client == null){
                    client = new OkHttpClient();
                }
            }
        }
        return client;
    }
    public static ServerApi getServerApi(){
        if (serverApi == null){
            synchronized (ServerApi.class){
                if (serverApi == null){
                    serverApi = OnCreate(ServerApi.class,Api.HOST);
                }
            }
        }
        return serverApi;
    }
    public static <T> T OnCreate(Class<T> tClass,String url){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(url)
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        return retrofit.create(tClass);
    }
}
bean类--网络请求接口的数据
public class ClassBean {
    private int code;
    private String msg;
    private List<NewslistBean> newslist;

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public List<NewslistBean> getNewslist() {
        return newslist;
    }

    public void setNewslist(List<NewslistBean> newslist) {
        this.newslist = newslist;
    }

    public static class NewslistBean {
        private String ctime;
        private String title;
        private String description;
        private String picUrl;
        private String url;

        public String getCtime() {
            return ctime;
        }

        public void setCtime(String ctime) {
            this.ctime = ctime;
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public String getDescription() {
            return description;
        }

        public void setDescription(String description) {
            this.description = description;
        }

        public String getPicUrl() {
            return picUrl;
        }

        public void setPicUrl(String picUrl) {
            this.picUrl = picUrl;
        }

        public String getUrl() {
            return url;
        }

        public void setUrl(String url) {
            this.url = url;
        }
    }
}
Model层的数据,请求数据
public class MainModel implements IMainModel {
    @Override
    public void recy(final OnNetListener<ClassBean> bean) {
        ServerApi serverApi = RetrofitHelper.getServerApi();
        serverApi.call()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<ClassBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(ClassBean classBean) {
                        bean.onSuccess(classBean);
                    }
                });
    }
}
model层对应的接口
public interface IMainModel {
    public void recy(OnNetListener<ClassBean> bean);
}
presenter层--view和model的交互层
public class MainPresenter {
    public IMainModel iMainModel;
    public IMainActivity iMainActivity;

    public MainPresenter(IMainActivity iMainActivity) {
        this.iMainActivity = iMainActivity;
        iMainModel = new MainModel();
    }
    public void getData(){
        iMainModel.recy(new OnNetListener<ClassBean>() {
            @Override
            public void onSuccess(ClassBean classBean) {
                iMainActivity.showrecy(classBean);
            }

            @Override
            public void onFailure(Exception e) {

            }
        });
    }
    public void getMore(){
        iMainModel.recy(new OnNetListener<ClassBean>() {
            @Override
            public void onSuccess(ClassBean classBean) {
                iMainActivity.showrecys(classBean);
            }

            @Override
            public void onFailure(Exception e) {

            }
        });
    }
}
view层--展示数据的Activity
public class MainActivity extends AppCompatActivity implements IMainActivity {

    private MainPresenter mainPresenter = new MainPresenter(this);
    private XRecyclerView mXrv;
    private List<ClassBean.NewslistBean> list;
    private RvAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        ActionBar bar = getSupportActionBar();
        bar.hide();
        mainPresenter.getData();
        mXrv.setLayoutManager(new LinearLayoutManager(this));
        //下拉刷新上拉加载
        mXrv.setLoadingListener(new XRecyclerView.LoadingListener() {
            //刷新
            @Override
            public void onRefresh() {
                mainPresenter.getData();
                mXrv.refreshComplete();
            }
            //加载更多
            @Override
            public void onLoadMore() {
                mainPresenter.getMore();
                mXrv.refreshComplete();
            }
        });
    }
    private void initView() {
        mXrv = findViewById(R.id.xrv);
    }
    //展示数据
    @Override
    public void showrecy(final ClassBean bean) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                list = bean.getNewslist();
                adapter = new RvAdapter(MainActivity.this, list);
                mXrv.setAdapter(adapter);
            }
        });
    }
    //加载更多
    @Override
    public void showrecys(ClassBean bean) {
        list.addAll(bean.getNewslist());
        adapter.notifyDataSetChanged();
    }
}
view层的Activity的接口
public interface IMainActivity {
    public void  showrecy(ClassBean bean);
    public void  showrecys(ClassBean bean);
}
RecyclerView的适配器
public class RvAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context context;
    private List<ClassBean.NewslistBean> list;

    public RvAdapter(Context context, List<ClassBean.NewslistBean> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(context, R.layout.item, null);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        MyViewHolder holder1 = (MyViewHolder) holder;
        ClassBean.NewslistBean bean = list.get(position);
        holder1.sdv.setImageURI(bean.getPicUrl());
        holder1.tv.setText(bean.getTitle());
    }

    @Override
    public int getItemCount() {
        return list.size();
    }
    class MyViewHolder extends RecyclerView.ViewHolder{
        private SimpleDraweeView sdv;
        private TextView tv;
        public MyViewHolder(View itemView) {
            super(itemView);
            sdv = itemView.findViewById(R.id.sdv);
            tv = itemView.findViewById(R.id.tv1);
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值