Android笔记-自用MVP框架


资料来源如下

  • 网络

编程环境

  • Android Studio 2.2.3

导语

  • 转眼刚用上手的MVP又不符合需求了,还是总结一下,继续MVVM吧。

  • 主要内容是转载,搭建小工程应该足够了。
    Android mvp 架构的自述
    如何更高效的使用MVP以及官方MVP架构解析

  • 框架开源在GitHub 地址点我直达

  • 好,我们开始吧!


MVC MVP MVVM

  • 省略200行,详情看Android App的设计架构:MVC,MVP,MVVM与架构经验谈
  • 再累赘属于掉书袋了,作者写的是很用心,恩,MVVM也有。

框架详解

  • 整个框架截图
    ScreenShot_20170509215208.png

  • 整体层次比较明确了,BaseClass 里存放基类,Model层存放数据存储有关类、Presenter层存放逻辑代码、View层存放Activity、frament等。

  • 这个框架是由MVP在Android中应用存在的问题而搭建的,基类中代码也由此而来。

问题

  • presenter一直持有Activity对象导致的内存泄漏问题

    • 使用mvp的时候,presenter会持有view,如果presenter有后台异步的长时间的动作,比如网络请求,这时如果返回退出了Activity,后台异步的动作不会立即停止,这里就会有内存泄漏的隐患。
    • 需要一个销毁view的方法,这里是在基类中完成的。
  • presenter绑定和解绑繁琐

    • 一般一个presenter对应一个Activity,一般应用内存在多个Actiivity,绑定与解绑相当繁琐。
    • 统一在 Basepresenter 与 BaseActivity中进行,同时解决内存泄漏问题,还有其他常用内容一并添加。
  • presenter 与 Model 的通信问题。

    • presenter已经与View层 强耦合了,框架中需要解耦presenter 与 Model ,使用异步通信。Handle等都太复杂了。。。
    • 开始没有什么好的解决办法,直到遇到了EventBus,当然EventBus也不是万能解决方案,跨进程,还是要另辟蹊径,不过EventBus足够自己写着完了。还有Rxjava,恩,还在玩着,没搞太懂。

代码

  • BasePresenter

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
     public abstract class BasePresenter<Viewinterface> {
    //传入泛型
    public Viewinterface mView;
    //绑定
    public void attach(Viewinterface mView) {
    this.mView = mView;
    }
    //解绑,防止view为空是内存泄漏
    public void dettach() {
    mView = null;
    }
    }
  • BaseActivity

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
     public abstract class BaseActivity <Viewinterface,mPresenter extends BasePresenter<Viewinterface>>extends AppCompatActivity {
    //获取Presenter对象
    public mPresenter mpresenter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //Presenter实例化
    mpresenter = initPresenter();
    //打印当前activity
    LogUtil.d("Activity", getClass().getSimpleName()+"onCreate");
    }

    @Override
    protected void onResume() {
    //重新刷新时重新绑定view
    mpresenter.attach((Viewinterface) this);
    super.onResume();
    LogUtil.d("Activity", getClass().getSimpleName()+"onResume");
    }

    @Override
    protected void onDestroy() {
    //解绑presenter持有的view
    mpresenter.dettach();
    super.onDestroy();
    LogUtil.d("Activity", getClass().getSimpleName()+"onDestroy");
    }


    public abstract mPresenter initPresenter();
    }
  • Presenter

    1
    2
     public interface Presenter {
    }
  • mPresenter

    1
    2
    3
    4
    5
    6
    7
    8
     public class mPresenter extends BasePresenter<Viewif> implements Presenter {
    private Model mModel;

    public mPresenter(Viewif view) {
    this.attach(view);
    this.mModel = new mModel();
    }
    }
  • BaseView

    1
    2
    3
     public interface BaseView<T>  {

    }
  • Viewif

    1
    2
    3
     public interface Viewif extends BaseView {

    }
  • MainActivity

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
     public class MainActivity extends BaseActivity<Viewif, mPresenter> implements Viewif{

    Presenter presenter;
    //Presenter初始化
    public mPresenter initPresenter() {
    return new mPresenter(this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    //presenter初始化
    presenter = mpresenter;
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    }
    }
  • LogUtil
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
     public class LogUtil {
    public static final int VERBOSE = 1;//啰嗦,等级最低的
    public static final int DEBUG = 2;//调试
    public static final int INFO = 3;//信息
    public static final int WARN = 4;//警告
    public static final int ERROR = 5;//错误
    public static final int NOTHING = 6;//什么也不打印出来
    public static final int level = VERBOSE;//LEVEL:标准

    public static void v(String tag, String msg) {
    if (level <= VERBOSE) {//如果大于或者等于定义的标准就打印出来
    Log.v(tag, msg);
    }
    }

    public static void d(String tag, String msg) {
    if (level <= DEBUG) {
    Log.d(tag, msg);
    }
    }

    public static void i(String tag, String msg) {
    if (level <= INFO) {
    Log.i(tag, msg);
    }
    }

    public static void w(String tag, String msg) {
    if (level <= WARN) {
    Log.w(tag, msg);
    }
    }

    public static void e(String tag, String msg) {
    if (level <= ERROR) {
    Log.e(tag, msg);
    }
    }
    }