MVP(基本实现代码,简单明了)

一、倒依赖

implementation 'com.squareup.okhttp3:okhttp:3.8.1'
    implementation 'com.squareup.okhttp3:logging-interceptor:3.4.1'
    implementation 'io.reactivex:rxjava:1.1.6'
    // Retrofit的rx解析库
    implementation 'com.squareup.retrofit2:retrofit:2.1.0'
    implementation 'com.squareup.retrofit2:adapter-rxjava:2.1.0'
    // Retrofit的gson库字符串库
    implementation 'com.squareup.retrofit2:converter-gson:2.1.0'
    //implementation 'com.squareup.retrofit2:converter-scalars:2.1.0
    implementation 'com.orhanobut:logger:1.8'
    //解决Android3.0以后不能butterknife问题
    implementation 'com.jakewharton:butterknife:8.4.0'
    annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0'
    compile 'io.reactivex:rxandroid:1.2.1'

    implementation 'com.android.support:recyclerview-v7:27.+'
    implementation 'com.github.bumptech.glide:glide:3.8.0'
    compile 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.30'

基本所有解析所需要的依赖都包含了

二、CallBacks接口

public interface CallBacks{
    void loginSucc(Bean str);
}

三、ApiService接口

public interface ApiService {
    @GET()
    rx.Observable<Bean> loginPre(@Url String url);
}

接下来我们要开始MVP了

四、M层

(一)M层接口

public interface IModel {

    void loginModel(CallBacks callbacks);

}

(二)M层实现类

public class MyModel implements IModel {
    @Override
    public void loginModel(final CallBacks callbacks) {
        Retrofit retrofit = new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .baseUrl("http://c.m.163.com/nc/")
                .build();
        retrofit.create(ApiService.class)
                .loginPre("article/headline/T1348647909107/0-20.html")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<Bean>() {
                    @Override
                    public void call(Bean bean) {
                        callbacks.loginSucc(bean);
                    }
                });

    }
}

五、V层

public interface IView {
    void loginSucc(Bean bean);
}

六、P层

(一)P层接口

扫描二维码关注公众号,回复: 3158040 查看本文章
public interface IPresenter {
    void loginPre();
}

(二)P层实现类

public class MyPresenter implements IPresenter,CallBacks {
    private IModel mModel ;
    private IView mView ;

    public MyPresenter(IView mView) {
        mModel = new MyModel();
        this.mView = mView;
    }

    @Override
    public void loginPre() {
        if (mView!=null) {
            mModel.loginModel(this);
        }
    }

    @Override
    public void loginSucc(Bean str) {
        if (mView!=null) {
            mView.loginSucc(str);
        }
    }
}

七、在Activity当中,也就是你要解析的那个类:

实现IView接口

public class MainActivity extends AppCompatActivity implements IView {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        IPresenter mPresenter = new MPresenter(this);
        mPresenter.loginPresenter();
    }


    @Override
    public void loginView(Bean bean) {

    }
}

猜你喜欢

转载自blog.csdn.net/qq_42749901/article/details/81950876