本篇文章只聊MVP结构实现,需要概念等高(bai)级(fu)用(mei)法请踏上最下方传送阵
基本结构的构建
1 首先是分model
根据Android MVP 详解(下)提供的分模块原则,把项目分成如下model
data:负责数据获取,比如网络,本地存储,数据库
damain:负责业务逻辑(平时平常的项目感觉用不上)
library:负责管理第三方库,以及二次封装的库
presentation:这个就是我们平时的app(model),负责展示图形界面,并填充数据,包扩view和Presenter
2 确定要用哪些第三方库
用gradle导包写入library(model)下
3 针对获取的数据类型,在data(model)下建立基本结构
4 同理domain模块
此处demo没有app的业务逻辑
暂不写
5 presentation模块的gradle中引入data domain library模块
data模块也需要引入library(要用到库)
--
6 presentation模块结构
建议不要按类的特性分包,按照功能分包,如图中的function_a文件夹是app的一个功能(比如首页,比如登录),图中loader文件夹先略过(这个放到下面讲)
base:存放除presenter外所有基类
mvp:存放presenter相关
util:所有的工具类
widget:所有的组件view等
常量类C的:模仿R包,存放所有的静态常量
基类设计
Activity的基类设计
BaseActivity
public abstract class BaseActivity extends RxAppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getContentView());
ButterKnife.bind(this);
initView(savedInstanceState);
setListeners();
bind();
}
@Override
public void setContentView(@LayoutRes int layoutResID) {
super.setContentView(layoutResID);
}
@Override
protected void onDestroy() {
super.onDestroy();
unBind();
}
public abstract void unBind();
/**
* --次序3--
* 绑定
*/
public abstract void bind();
/**
* 设置监听器,在initView()之后
* --次序2--
*/
public abstract void setListeners();
/**
* --次序1--
* 初始化view
*/
public abstract void initView(Bundle savedInstanceState);
/**
* --次序0--
* 返回layout的id
*/
public abstract int getContentView();
}
Presenter的基类设计
BasePresenter
public class BasePresenter<T extends MvpView> implements IPresenter<T> {
private T mvpView;
// public CompositeDisposable compositeDisposable;//用于绑定或取消绑定Rxjava的
/**
* @param view 子view,比如,子presenter是控制当前的Activity的presenter,那么子view就是该Activity
* 绑定view
*/
@Override
public void attachView(T view) {
this.mvpView = view;
// this.compositeDisposable = new CompositeDisposable();
}
@Override
public void onDestroyed() {
}
/**
* 取消view绑定
*/
@Override
public void detachView(T view) {
mvpView = null;
// compositeDisposable.isDisposed();
// compositeDisposable = null;
}
/**
* view是否连接
*/
@Override
public boolean isAttachView(T view) {
return mvpView != null;
}
/**
* 得到当前的绑定的view对象
*/
@Override
public T getMvpView() {
return mvpView;
}
}
其中抽取的presenter接口如下
public interface IPresenter<T> {
void attachView(T view);
void detachView(T view);
void onDestroyed();
boolean isAttachView(T view);
//当传入Activity的context时,在presenter中用此方法获取到Activity的上下文,以控制Activity中的方法
T getMvpView();
}
MvpView接口如下
public interface MvpView {
void onFailed(Throwable e);
}
使用MVP模式
GankActivity中
public class GankActivity extends BaseActivity implements MvpView{
private GankPresenter presenter = new GankPresenter();
@Override
public void unBind() {
presenter.detachView(this);
}
@Override
public void bind() {
presenter.attachView(this);
presenter.loadData();
}
@Override
public void setListeners() {
}
@Override
public void initView(Bundle savedInstanceState) {
}
@Override
public int getContentView() {
return R.layout.activity_gank;
}
@Override
public void onFailed(Throwable e) {
}
}
GankPresenter
public class GankPresenter extends BasePresenter<GankActivity> {
Observer<GankModel> observer = new Observer<GankModel>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(GankModel value) {
Log.e("gankUrl:", value.getResults().get(1).getUrl());
}
@Override
public void onError(Throwable e) {
Log.e("gank", "错误");
}
@Override
public void onComplete() {
}
};
public void loadData() {
Network.getGankApi()
.getBeauties(10, 1)
.compose(getMvpView().<GankModel>bindToLifecycle())
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(observer);
}
}
数据获取的相关代码
在presentation模块里给网络权限
运行程序
关键点
MVP模式简单的说就是把控制逻辑交给presenter来控制
BasePresenter的设计是关键
在IPresenter接口里面的getMvpView()方法保证presenter可以获取到Activity中的上下文进而控制Activity中的逻辑
利用Loader延长presenter的生命周期
GankPresenterLoader
public class GankPresenterLoader<T extends BasePresenter> extends Loader<T>{
private GankPresenterFactory factory;
private T presenter;
public GankPresenterLoader(Context context, GankPresenterFactory factory) {
super(context);
this.factory = factory;
}
@Override
protected void onStartLoading() {
super.onStartLoading();
if(presenter != null){
deliverResult((T) presenter);
return;
}
forceLoad();
}
@Override
protected void onForceLoad() {
super.onForceLoad();
presenter = (T) factory.create();
deliverResult(presenter);
}
@Override
protected void onReset() {
super.onReset();
presenter.onDestroyed();
presenter = null;
}
}
然后只需要在GankActivity中调用
getLoaderManager().initLoader(LOADER_ID, null, this);
实现LoaderManager.LoaderCallbacks<GankPresenter>的方法
public class GankActivity extends BaseActivity implements MvpView, LoaderManager.LoaderCallbacks<GankPresenter> {
private static final int LOADER_ID = 1110;
private GankPresenter presenter = new GankPresenter();
@Override
public void unBind() {
presenter.detachView(this);
}
@Override
public void bind() {
presenter.attachView(this);
presenter.loadData();
}
@Override
public void setListeners() {
}
@Override
public void initView(Bundle savedInstanceState) {
getLoaderManager().initLoader(LOADER_ID, null, this);
Log.e("presenterId",presenter.getClass().toString());
}
@Override
public int getContentView() {
return R.layout.activity_gank;
}
@Override
public void onFailed(Throwable e) {
}
@Override
public Loader<GankPresenter> onCreateLoader(int id, Bundle args) {
Log.e("GankActivity", "onCreateLoader");
return new GankPresenterLoader<>(this, new GankPresenterFactory());
}
@Override
public void onLoadFinished(Loader<GankPresenter> loader, GankPresenter presenter) {
Log.e("GankActivity", "onLoadFinished");
this.presenter = presenter;
}
@Override
public void onLoaderReset(Loader<GankPresenter> loader) {
Log.e("GankActivity", "onLoaderReset");
presenter = null;
}
}
这样就可以了
Github源代码Mvp-Test
Android MVP 详解(上)
Google官方加载器
Android:聊聊 MVP 中 Presenter 的生命周期
通过Loader延长Presenter生命周期
Android:聊聊我所理解的MVP
end