前言
什么是MVP,为什么要用MVP?
网上介绍MVP的很多,百度一下你就知道,至于为什么要用MVP,当然是由于它的优势了:
代码易读
虽然这种模式增加了代码量,但是代码的可读性大大提升。
降低耦合,方便维护
MVP的使用,使Activity中的网络请求剥离出来 成为model、presenter。
MVP顾名思义 Model、View、Presenter
- Model:负责网络的请求
- View:进行界面的展示
- Presenter:负责处理请求网络后的数据处理:加载中 成功 or 失败 取消加载
那Contract是什么用呢?
这就涉及到MVP的缺点了,MVP在实现代码易读的同时,额外增加了大量的接口、类,不方便进行管理,于是Contract就登场了。
Contract 如其名,是一个契约,将Model、View、Presenter 进行约束管理,方便后期类的查找、维护。
具体代码
依赖
1 | //butterknife |
基类
BaseActivity
如果使用Kotlin及其插件可以不用butterknife
1 | import android.arch.lifecycle.Lifecycle; |
BaseFragment
1 | import android.app.Fragment; |
BaseView
1 | import com.uber.autodispose.AutoDisposeConverter; |
BasePresenter
1 | public class BasePresenter<V extends BaseView> { |
BaseMvpActivity
1 | import android.arch.lifecycle.Lifecycle; |
BaseMvpFragment
1 | import android.arch.lifecycle.Lifecycle; |
线程调度
1 | import org.reactivestreams.Publisher; |
登录模块
LoginContract
首先,创建一个登陆的Contract:
1 | public interface LoginContract { |
其次创建Presenter、Model、View 对应Contract中的接口;
1 | public class LoginPresenter implements LoginContract.Presenter{} |
具体代码
1 | public interface LoginContract { |
在LoginContract 中
- Model接口 创建对应的联网请求的方法,将Presenter提交的字段放到联网请求中,发送给服务器
- View 接口 创建在界面上显示加载中、取消加载以及登陆成功、失败的方法
- Presenter 接口 创建 登陆的方法,以及需要提交的字段 (username、password)
LoginModel
1 | import io.reactivex.Flowable; |
LoginPresenter
1 | public class LoginPresenter extends BasePresenter<LoginContract.View> implements LoginContract.Presenter { |
LoginPresenter 实现LoginContract.Presenter 接口中的 login(String username, String password) 方法
实例化Model,在LoginPresenter login(String username, String password)方法中,调用model的网络请求,将username、password放在model的login()方法中,进行请求服务器。
请求服务器前 使用LoginContract.View中的 mView.showLoading()方法,进行显示加载中;在成功失败的回调中,使用对应的方法,以及取消加载。
其中BasePresenter、BaseView 是对Presenter以及View进行的封装
LoginActicity
1 | public class LoginActivity extends BaseMvpActivity<LoginPresenter> implements LoginContract.View { |
LoginActivity 中实现 LoginContract.View中的方法 ,在实现的方法中,进行进度条加载、和登陆成功or失败的UI的展示:
1 |
|
MVPHelper
MVP快速生成类的插件:https://github.com/githubwing/MVPHelper
该插件主要是为了生成Contract
、 Model
和Presenter
使用快捷键 Alt + Insert
, 点击MvpHelper
,自动生成文件
注意
只支持Java,不支持Kotlin
可以对后缀为
Contract
或Presenter
的Java源文件中进行生成,
Contract
所处的位置中必须有至少一个包以contract
结尾,
Presenter
所处的位置中必须有至少一个包以presenter
结尾,否则生成报错重复生成将删除旧文件并重新生成,所以不要在已写代码后重新生成,会导致代码丢失
强制
Contract
为interface
Contract
中的三个接口不包含多余的修饰符
作用就是
在contract内创建如下接口
1 | public interface GoodsInfoContract { |
该文件会自动生成如下格式
1 | public interface GoodsInfoContract { |
会自动创建如下文件
model下
1 | public class GoodsInfoModel implements GoodsInfoContract.Model { |
presenter下
1 | public class GoodsInfoPresenter implements GoodsInfoContract.Presenter { |
或者
使用Presenter
生成
1 | public class GoodsInfoPresenter{ |
该文件会自动生成如下格式
1 | public class GoodsInfoPresenter implements GoodsInfoContract.Presenter { |
同时生成其它两个文件
相关
- 示例源码:https://github.com/RookieExaminer/MvpDemo
- MVP快速生成类的插件:https://github.com/githubwing/MVPHelper
- Android MVP架构搭建:http://www.jcodecraeer.com/a/anzhuokaifa/2017/1020/8625.html?1508484926
- Android架构中添加AutoDispose解决RxJava内存泄漏:https://www.jianshu.com/p/8490d9383ba5
- Android MVP 架构:https://www.jianshu.com/p/ae0b21d3238a