概述
现在比较热门的架构有MVP与MVVM,两者比较类似。
- 作为MVP衍生版的MVVM增加了响应式编程,它的缺点很明显。
- 依赖更多复杂的框架,增加了不确定性
- 数据绑定使得 Bug 很难被调试
- 对于过大的项目,数据绑定需要花费更多的内存
- XML页面增加了过多的业务逻辑
这几点都是我比较不喜欢的地方,所以我选择了MVP作为项目的架构。
切入正题:我们直接看代码吧:
项目结构
使用第三方框架
- Retrofit
- RxJava
- Okhttp3
View基类
用于在Presenter中执行View层中的基本方法
public interface IBaseView {
// 只做局部变量使用
Context getActivity();
Context getContext();
void showProgressDialog(String msg);
void showProgressDialog(String msg, boolean cancelable);
void showProgressDialog();
void dismissProgressDialog();
void showToast(String msg);
void showToast(int id);
void onCompleted();
void onError();
}
Presenter基类
绑定视图提供一些基础方法给View调用,也可以看成视图层通知Presenter我加载好了、我销毁了的意思。管理Retrofit的请求并简化RxJava写法。
public abstract class BasePresenter <V extends IBaseView> {
protected V mView;
private CompositeSubscription mCompositeSubscription;
// 绑定视图
public BasePresenter(V v) {
mView = v;
}
public void onResume() {}
public void onStop() {}
private void addSubscription(Subscription s) {
if (mCompositeSubscription == null) {
mCompositeSubscription = new CompositeSubscription();
}
mCompositeSubscription.add(s);
}
protected <D>D addMainSubscription(Observable <D>observable, Subscriber<D> subscriber) {
addSubscription(observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(subscriber));
return null;
}
protected<D> D addThreadSubscription(Observable<D> observable, Subscriber<D> subscriber) {
addSubscription(observable.subscribeOn(Schedulers.io()).observeOn(Schedulers.io()).subscribe(subscriber));
return null;
}
public void onDestroy() {
if (mCompositeSubscription != null && mCompositeSubscription.hasSubscriptions()) {
mCompositeSubscription.unsubscribe();
}
}
public void unAttachView() {
if (mView != null) {
mView = null;
}
}
}
Activity基类
提供一些视图的方法并与Presenter绑定关联它们的生命周期,Activity销毁后Presenter不再执行任何逻辑。并实现一些View的基本方法供Presenter使用。
public abstract class BasicActivity <P extends BasePresenter> extends AppCompatActivity implements IBaseView,
View.OnClickListener {
...省略
protected P mPresenter;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPresenter = attachView();
initView();
getWindow().getDecorView().post(new Runnable() {@Override public void run() {
onCreateView();
}
});
}
private void initView() {
...
}
/**
* 绑定Presenter
*/
protected abstract P attachView();
@Override protected void onResume() {
super.onResume();
if (mPresenter != null) mPresenter.onResume();
}
@Override protected void onStop() {
super.onStop();
if (mPresenter != null) mPresenter.onStop();
}
@Override protected void onDestroy() {
super.onDestroy();
if (mPresenter != null) {
mPresenter.onDestroy();
}
if (mPresenter != null) {
mPresenter.unAttachView();
mPresenter = null;
}
}
...省略
/**
* 尽量不去使用,只做预留功能
*/
@Override public Context getActivity() {
return this;
}
@Override public Context getContext() {
return BaseApplication.getContext();
}
}
Contract
负责约定view层和presenter层的接口,view和presenter实现相应接口,最终达到解耦的目的。
public interface HomeContract {
interface View extends IBaseView {
void onUserLoadCompleted(PhoneLocalBean bean);
void onUserLoadError();
}
abstract class Presenter extends BasePresenter < View > {
Presenter(View view) {
super(view);
}
public abstract void queryWeather(String phone);
}
}
Presenter
负责从model层获取数据、整理数据、行为处理等。处理后调用view显示数据。
public class HomePresenter extends HomeContract.Presenter {
public HomePresenter(HomeContract.View view) {
super(view);
}
@Override public void queryWeather(String phone) {
Map <String, String> options = new HashMap<>();
options.put("phone", phone);
addMainSubscription(HttpManager.getInstance().sendRequest().queryWeather(options), new HttpResultCallBack<PhoneLocalBean> () {
@Override public void onResponse(PhoneLocalBean bean, int status) {
if (bean != null) {
mView.onUserLoadCompleted(bean);
} else {
mView.onUserLoadError();
}
}
@Override public void onErr(String err, int status) {
mView.showToast(err);
mView.onUserLoadError();
}
});
}
}
Model
Model中又分为Net获取数据和DB存取数据,我这里没有使用到数据库操作推荐大家使用GreenDAO。
public interface HttpService {
@GET(GlobalVar.NetPorts.WEATHER)
Observable<HttpResult<PhoneLocalBean>> queryWeather(@QueryMap Map<String, String> options);
}
视图
public class HomeActivity extends BasicActivity<HomePresenter> implements HomeContract.View {
private EditText mPhoneEt;
private TextView mAddressTv;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTitleCenter("首页");
mPhoneEt = (EditText) findViewById(R.id.main_phones_et);
mAddressTv = (TextView) findViewById(R.id.main_address_tv);
Button queryBt = (Button) findViewById(R.id.main_query_bt);
queryBt.setOnClickListener(this);
}
@Override protected HomePresenter attachView() {
return new HomePresenter(this);
}
@Override public void onClick(View v) {
super.onClick(v);
switch (v.getId()) {
case R.id.main_query_bt:
String phone = mPhoneEt.getText().toString().trim();
if (TextUtils.isEmpty(phone)) return;
showProgressDialog();
mPresenter.queryWeather(phone);
break;
}
}
@Override public void onUserLoadCompleted(PhoneLocalBean bean) {
dismissProgressDialog();
mAddressTv.setText("归属地:" + bean.getCity());
}
@Override public void onUserLoadError() {
dismissProgressDialog();
mAddressTv.setText("获取失败");
}
}
最后推荐一张图片,MVP之间的交互关系很好的体现出来