本文主要是結合本身對MVP的理解搭建了符合自身業務場景的MVP框架。html
先放一個Demo地址,文章末尾也有android
Activity
Fragment
的形式出現。1.對View進行封裝 通常狀況下,作數據請求都有顯示加載框、請求成功、請求失敗等操做,咱們把這些共有的功能封裝到BaseView中。git
public interface IBaseView {
/**
* 顯示加載框
*/
void showLoading();
/**
* 隱藏加載框
*/
void dismissLoading();
/**
* 空數據
*
* @param tag TAG
*/
void onEmpty(Object tag);
/**
* 錯誤數據
*
* @param tag TAG
* @param errorMsg 錯誤信息
*/
void onError(Object tag, String errorMsg);
/**
* 上下文
*
* @return context
*/
Context getContext();
}
複製代碼
2.對Presenter封裝 爲了不持有View的Presenter作耗時操做而引發的內存泄漏,咱們的Presenter應該和宿主Activity/Fragment
同建立、同銷燬。github
public abstract class BasePresenter{
...
/**
* 綁定View
*/
public void attachView(View view) {
this.view=view;
}
/**
* 解綁View
*/
public void detachView() {
this.view=null;
}
...
}
public abstract class MvpActivity<P extends BasePresenter> extends BaseActivity implements IBaseView{
...
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//建立present
presenter = createPresenter();
if (presenter != null) {
presenter.attachView(this);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (presenter != null) {
presenter.detachView();
presenter = null;
}
}
...
}
複製代碼
如上操做當然能夠解決內存泄漏問題,但又會引起行的問題:bash
場景:用戶打開商品列表頁,網絡很差獲取數據比較慢,用戶離開該頁面,繼續瀏覽其餘頁面,忽然應用崩潰了。網絡
分析問題: 在用戶打開頁面的時候綁定P和V,離開頁面的時候解綁P和V,當耗時操做完成調用V更新界面,此時因爲P和V已經解綁V處於null,調用V的更新頁面方法就會引發空指針異常。架構
解決問題: 使用動態代理對View作弱引用,完整的BasePresenter以下:框架
public abstract class BasePresenter<M extends IBaseModel, V extends IBaseView> {
private V mProxyView;
private M module;
private WeakReference<V> weakReference;
/**
* 綁定View
*/
@SuppressWarnings("unchecked")
public void attachView(V view) {
weakReference = new WeakReference<>(view);
mProxyView = (V) Proxy.newProxyInstance(
view.getClass().getClassLoader(),
view.getClass().getInterfaces(),
new MvpViewHandler(weakReference.get()));
if (this.module == null) {
this.module = createModule();
}
}
/**
* 解綁View
*/
public void detachView() {
this.module = null;
if (isViewAttached()) {
weakReference.clear();
weakReference = null;
}
}
/**
* 是否與View創建鏈接
*/
protected boolean isViewAttached() {
return weakReference != null && weakReference.get() != null;
}
protected V getView() {
return mProxyView;
}
protected M getModule() {
return module;
}
protected Context getContext() {
return getView().getContext();
}
protected void showLoading() {
getView().showLoading();
}
protected void dismissLoading() {
getView().dismissLoading();
}
/**
* 經過該方法建立Module
*/
protected abstract M createModule();
/**
* 初始化方法
*/
public abstract void start();
/**
* View代理類 防止 頁面關閉P異步操做調用V 方法 空指針問題
*/
private class MvpViewHandler implements InvocationHandler {
private IBaseView mvpView;
MvpViewHandler(IBaseView mvpView) {
this.mvpView = mvpView;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//若是V層沒被銷燬, 執行V層的方法.
if (isViewAttached()) {
return method.invoke(mvpView, args);
} //P層不須要關注V層的返回值
return null;
}
}
}
複製代碼
3.契約類Contract的出現 經過契約類來管理Model、View、Presenter的全部接口,這樣使得Presenter和View有哪些功能一目瞭然,維護起來也方便,同時使得View與Presenter一一對應,並有效地減小類的數目。異步
public interface Contract {
interface Model extends IBaseModel {
void login(User user, ResponseCallback callback);
}
interface View extends IBaseView {
User getUserInfo();
void loginSuccess(User user);
}
interface Presenter {
void login();
}
}
複製代碼
4.對Activity
的封裝,Fragment
封裝同理ide
public abstract class BaseMvpActivity<P extends BasePresenter> extends Activity implements IBaseView {
protected P presenter;
@SuppressWarnings("unchecked")
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//建立present
presenter = createPresenter();
if (presenter != null) {
presenter.attachView(this);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (presenter != null) {
presenter.detachView();
presenter = null;
}
}
@Override
public void showLoading() {
if (loadingDialog != null && !loadingDialog.isShowing()) {
loadingDialog.show();
}
}
@Override
public void dismissLoading() {
if (loadingDialog != null && loadingDialog.isShowing()) {
loadingDialog.dismiss();
}
}
@Override
public void onEmpty(Object tag) {
}
@Override
public void onError(Object tag, String errorMsg) {
}
@Override
public Context getContext() {
return mContext;
}
/**
* 建立Presenter
*/
protected abstract P createPresenter();
}
複製代碼
經過泛型規定Presenter,而且暴露抽象方法createPresenter()給子類來建立Presenter,基類實現BaseView中的公共方法,減小子類代碼的冗餘。 5.登陸案例
契約類public interface LoginContract {
interface Model extends IBaseModel {
/**
* 登陸
*
* @param user 用戶信息
* @param callback 回調
*/
void login(User user, ResponseCallback callback);
}
interface View extends IBaseView {
/**
* 返回用戶信息
*/
User getUserInfo();
/**
* 登陸成功
*/
void loginSuccess(User user);
}
interface Presenter {
/**
* 登陸
*/
void login();
}
}
複製代碼
Model
public class LoginModel implements LoginContract.Model {
@Override
public void login(User user, ResponseCallback callback) {
if (user == null) {
callback.onError("", (Throwable) new Exception("用戶信息爲空"));
}
RequestParam param = new RequestParam();
param.addParameter("username", user.getUsername());
param.addParameter("password", user.getPassword());
HttpUtils.getInstance()
.postRequest(Api.LOGIN, param, callback);
}
}
複製代碼
Presenter
public class LoginPresenter extends BasePresenter<LoginContract.Model, LoginContract.View>
implements LoginContract.Presenter {
@Override
public void login() {
if (isViewAttached()) {
getView().showLoading();
getModule().login(getView().getUserInfo(), new OnResultObjectCallBack<User>() {
@Override
public void onSuccess(boolean success, int code, String msg, Object tag, User response) {
if (code == 0 && response != null) {
getView().loginSuccess(response);
} else {
getView().onError(tag, msg);
}
}
@Override
public void onFailure(Object tag, Exception e) {
getView().onError(tag, msg);
}
@Override
public void onCompleted() {
getView().dismissLoading();
}
});
}
}
@Override
protected LoginModel createModule() {
return new LoginModel();
}
@Override
public void start() { }
}
複製代碼
登陸Activity
public class LoginActivity extends ActionBarActivity<LoginPresenter> implements LoginContract.View {
@BindView(R2.id.edt_name)
EditText edtName;
@BindView(R2.id.edt_pwd)
EditText edtPwd;
@BindView(R2.id.ob_login)
ObserverButton obLogin;
@BindView(R2.id.ob_register)
TextView obRegister;
@Override
protected int getLayoutId() {
return R.layout.user_activity_login;
}
@Override
protected void initView() {
setTitleText("登陸");
obLogin.observer(edtName, edtPwd);
}
@OnClick({R2.id.ob_login, R2.id.ob_register})
public void onViewClicked(View view) {
int i = view.getId();
if (i == R.id.ob_login) {
presenter.login();
} else if (i == R.id.ob_register) {
ActivityToActivity.toActivity(mContext, RegisterActivity.class);
}
}
@Override
public void loginSuccess(User user) {
UserInfoUtils.saveUser(user);
EventBusUtils.sendEvent(new Event(EventAction.EVENT_LOGIN_SUCCESS));
finish();
}
@Override
public void onError(Object tag, String errorMsg) {
super.onError(tag, errorMsg);
ToastUtils.showToast(mContext, errorMsg);
}
@Override
protected LoginPresenter createPresenter() {
return new LoginPresenter();
}
@Override
public void onEventBus(Event event) {
super.onEventBus(event);
if (TextUtils.equals(event.getAction(), EventAction.EVENT_REGISTER_SUCCESS)) {
finish();
}
}
@Override
protected boolean regEvent() {
return true;
}
@Override
public User getUserInfo() {
return new User(edtName.getText().toString().trim(), edtPwd.getText().toString().trim());
}
}
複製代碼
不管是MVP仍是MCV或者MVVM,都是爲把業務與UI分離,避免在一個Activity裏把全部的操做都塞進來,各自在各自的領域工做。每一個人對於層級結構都有不一樣的理解和見解,封裝一個適合本身、適合當下業務場景的框架纔是最重要的。
最後放上Demo地址,共同窗習,有什麼很差的地方,歡迎你們指出!
參考文獻 Google爸爸的案例 JesseBraveMan的 Android MVP架構搭建 淺談Android中的MVP架構 深刻講解Android MVP框架