刚开始接触android的时候就开始用mvc结构,但是在以后的项目开发中发现功能越来越多,activity的代码量剧增,甚至达到了几千行,后续修改逻辑时很是查找方法时很是头疼。针对这个问题网上有好多mvp的格式,把activity的业务逻辑分到presenter上,v只是负责刷新ui,结构清晰简单,也方便后期维护。现在也开始尝试着咋项目中引入mvp结构。
按照GitHub上给的sample范例,自己写了一套,有什么不足的地方希望留言指出,一下是参考的github地址:
https://github.com/googlesamples/android-architecture
1.建一个协议基类basecontract
public interface BaseContract { interface BaseView<P extends BasePresenter> { void setPresenter(P presenter); } interface BasePresenter<BaseView> { void attachMVPView(BaseView view); void detachMVPView(); boolean isAttachMVPView(); } }
包含两个内部接口,baseview和basePresenter,方法很简单,baseview包含一个绑定presenter的方法,basepresenter包含了绑定解绑和判断view是否销毁的方法。github的例子没有判断和销毁view的方法,认为有点不太合理,哈哈!!!
2.建一个basepresenter实现类,统一处理view的绑定和解绑
public class IBasePresent<P extends BaseContract.BaseView> implements BaseContract.BasePresenter<BaseContract.BaseView> { public P mvpView; @Override public void attachMVPView(BaseContract.BaseView view) { this.mvpView = (P) view; mvpView.setPresenter(this);//给P的子类赋值 } @Override public void detachMVPView() { if (mvpView != null) mvpView = null; } @Override public boolean isAttachMVPView() { return mvpView != null; }
最近在学习泛型的引用,所以在例子中小试了一把,效果不错。
3.定义一个fragment基类
public class MVPFragment<P extends IBasePresent> extends Fragment { public P mIBasePresenter; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override public void onDestroy() { super.onDestroy(); if (mIBasePresenter != null) { mIBasePresenter.detachMVPView(); } } }
当view销毁的时候,presenter也要释放view,防止内存泄漏
4.开始测试,新建一个view的实现类,presenter的实现类,和一个fragment
public interface RightViewImpl extends BaseContract.BaseView { void showData(String data); }
public class RightPresenterImpl extends IBasePresent<RightViewImpl> { public void getData() { new Handler().postDelayed(new Runnable() { @Override public void run() { if(isAttachMVPView()){ mvpView.showData("获取数据成功"); } } }, 3000); } }
public class RightFragment extends MVPFragment<RightPresenterImpl> implements RightViewImpl { private ProgressDialog mProgressDialog; private TextView m_right_tv; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.frag_right, null); m_right_tv = v.findViewById(R.id.tv_right); mProgressDialog = new ProgressDialog(getActivity()); mProgressDialog.setTitle("提示"); mProgressDialog.setMessage("正在加载中..."); mProgressDialog.show(); mIBasePresenter.getData(); return v; } @Override public void setPresenter(BaseContract.BasePresenter presenter) { this.mIBasePresenter = (RightPresenterImpl) presenter; } @Override public void showData(String data) { mProgressDialog.dismiss(); Toast.makeText(getActivity(), data, Toast.LENGTH_SHORT).show(); m_right_tv.setText(data); } }
三秒延迟,模拟网络请求,在presenter处理网络请求相关逻辑,fragment只有ui的刷新逻辑,结构清晰,耦合度低。
小结:
1》因为不同的模块处理的逻辑不同,需要不同的view和presenter
2》fragment基类里的presenter需要先在activity层实例然后绑定,顺序不要烦了
3》presenter的子类都会包含有对应的view,所以在presenter基类里加入泛型,presenter子类里也会指定对应的view子类,就像本例中的
RightPresenterImpl
和
RightViewImpl赠人玫瑰手有余香!!!