易学MVP 个络加载数据+XRecyclerView+Retrofit

第一步:配置权限和导包:

<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.VIBRATE" />
compile 'com.jcodecraeer:xrecyclerview:1.3.2'
compile 'com.squareup.retrofit2:retrofit:2.0.1'
compile 'com.squareup.retrofit2:converter-gson:2.0.1'
compile 'com.squareup.retrofit2:adapter-rxjava:2.0.1'
compile 'io.reactivex:rxandroid:1.1.0'
compile 'com.squareup.okhttp3:logging-interceptor:3.4.1'
compile 'com.facebook.fresco:fresco:0.9.0+'
compile 'com.android.support:mediarouter-v7:25.0.0'
compile 'com.android.support:appcompat-v7:25.0.0'
compile 'com.android.support:recyclerview-v7:25.0.0'

第二步:创建三个包,创建两个接口,分别是model,view俩个接口:

public interface Iview {
    void  getdata(List<Mysupercalss.DataBean> data);
}
public interface Imodel {
    void login(String mm);
}

第三步:model实现接口:

package model;


import java.util.List;

import bean.API;
import bean.Iservers;
import bean.Mysupercalss;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Observable;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2017/11/5 0005.
 */

public class Mymodel implements Imodel {

    private OnFinishLisenter lisenter;
    //定义接口
    public interface OnFinishLisenter{
        void onFinish(List<Mysupercalss.DataBean> userBean);
    }
    public void setOnFinishLisenter(OnFinishLisenter lisenter){
        this.lisenter = lisenter;
    }
    @Override
    public void login(String mm) {
        Retrofit retrofit = new Retrofit.Builder().baseUrl(API.MYAPI).addConverterFactory(GsonConverterFactory.create()).addCallAdapterFactory(RxJavaCallAdapterFactory.create()).build();
        Iservers apiser = retrofit.create(Iservers.class);
        Observable<Mysupercalss> gethom = apiser.getdata();
        gethom.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Mysupercalss>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(Mysupercalss mysup) {
                        lisenter.onFinish(mysup.getData());

                    }


                });
    }
}

第四步:Persenter层:

package presenter;

import java.util.List;

import bean.Mysupercalss;
import model.Mymodel;
import view.Iview;

/**
 * Created by Administrator on 2017/11/5 0005.
 */

public class Mypre implements Mymodel.OnFinishLisenter {
    private Iview iview;
    private  Mymodel mymodel;

    public Mypre(Iview iview) {
        this.iview = iview;
        this.mymodel = new Mymodel();
    }
    public   void  setadapter(String path){
        mymodel.setOnFinishLisenter(this);
        mymodel.login(path);

    }
    @Override
    public void onFinish(List<Mysupercalss.DataBean> userBean) {
        iview.getdata(userBean);
    }
}

需要注意的是这里使用了接口回调,再调用时需要注意顺序的问题

第五步:MainActivity:

package com.example.mvp_retrofit;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;

import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.List;

import bean.API;
import bean.MyAdapter;
import bean.Mysupercalss;
import presenter.Mypre;
import view.Iview;

public class MainActivity extends AppCompatActivity implements Iview{

    XRecyclerView xrecyclerview;
    List<Mysupercalss.DataBean> list;
    MyAdapter adapter;
    Mypre mypre;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        xrecyclerview = (XRecyclerView)findViewById(R.id.xrecyclerview);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
/*
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
*/
        xrecyclerview.setLayoutManager(layoutManager);
        mypre=new Mypre(this);

        mypre.setadapter(API.MYAPI);


    }


    @Override
    public void getdata(final List<Mysupercalss.DataBean> data) {
        adapter = new MyAdapter(MainActivity.this, data);
        xrecyclerview.setAdapter(adapter);
            xrecyclerview.setLoadingListener(new XRecyclerView.LoadingListener() {
                @Override
                public void onRefresh() {
                    list=data;
                    adapter.notifyDataSetChanged();
                    xrecyclerview.refreshComplete();
                }

                @Override
                public void onLoadMore() {
                    list.addAll(data);
                    adapter.notifyDataSetChanged();
                    xrecyclerview.loadMoreComplete();

                }
            });

    }
}

还有url和适配器我就不贴了,可以根据自己的json串来做响应的改变.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值