文章目录
1.MVC,MVP,MVPVM(一)实践之路
2.MVC,MVP,MVPVM(二)提升效率之Templates
遗留问题
《MVC,MVP,MVPVM(一)实践之路》中讲到如何从MVC到MVPVM的转变,将各个模块分离,职责划清。不过有个缺点,就是类爆炸,为什么代码写着写着就MVC了,就是因为一个Activity搞定,写着爽。但是,如果要实现解耦,就一定意味着会有很多不同的职能类。如果采用mvp,或mvpvm的实现方式,每次在新建一个页面就需要差不多10+个文件,虽然逻辑简单,不过全都手动创建的话,是不是觉得还是MVC好。
解决方案
套用定义好的代码Templates,自动生成各个职能类。
模版地址:
https://github.com/listen2code/Test_MVPVM/tree/master/doc/MvpvmComponent
使用介绍
1.将写好的templates拷贝到本地
Android Studio.app/Contents/plugins/android/lib/templates/activities/
2.重启AndroidStudio,右键项目->New->Activity->MvpvmComponent
3.在打开的编辑页面输入ActivityName和layoutName,包名为”com.listen.test_mvpvm”
根据Templates自动生成的类
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable name="data" type="com.listen.test_mvpvm.model.viewmodel.ITestMvpvmViewModel"/>
<variable name="presenter" type="com.listen.test_mvpvm.presenter.ITestMvpvmPresenter"/>
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/tv_test"
android:layout_width="wrap_content"