Android MVP在项目中的用法!!!

网上有很多MVP的理解,很多都是偏理论的,面试官一问,在项目中如何使用的呢?所以今天主要讲的是针对于项目构建的实操!gogogo!

你知道MVP吗?

  • MVP和MVC有个重大区别:在MVP中的View并不直接使用Model,通信通过Presenter(MVC中的Controller)来进行,所有交互在Presenter内部进行,而在MVC中的View会直接从Model中读取数据而不是通过Controller.

你在代码中如何使用MVP吗?

1.打开AS,按照MVP的代码目录结构,先创建package

结构如下,M-model ,P-presenter; contract作用 在后面讲到。
Android MVP在项目中的用法!!!_第1张图片

2.在base目录结构下基础类

如图在base目录下分别创建以下三个文件

  • M-BaseActivity.java
  • V-BaseView.java
  • P-BasePresenter.java
    Android MVP在项目中的用法!!!_第2张图片

BaseView.java

package com.luobo.mvp.base;

import android.content.Context;

public interface BaseView {
    Context getContext();

    /**
     * @param title
     */
    void setTitle(String title);

    /**
     * @param description
     */
    void showDescription(String description);
}

BasePresenter.java

package com.luobo.mvp.base;

public interface BasePresenter<V extends  BaseView> {
    /**
     *
     * @param view
     */
    void attachView(V view);

    /**
     * release resourse
     */
    void deachView();

}

BaseActivity.java

package com.luobo.mvp.base;

import android.app.Activity;

public class BaseActivity<P extends  BasePresenter<? extends BaseView>> extends Activity{
    protected P mPresenter;
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (null != mPresenter){
            mPresenter.deachView();
            this.mPresenter = null;
        }
    }
}

3.在contract目录结构下创建接口MvpContract.java

  • 在谷歌官方的MVP中就有Contract,类似合同类的接口,它把P,V的所有方法写在一起,这样所有的功能你只需要看Contract类就能清楚了解。
    Android MVP在项目中的用法!!!_第3张图片

MvpCotract.java中有两个内部接口View和Presenter,代码如下:

package com.luobo.mvp.contract;

import com.luobo.mvp.base.BasePresenter;
import com.luobo.mvp.base.BaseView;

public interface MvpContract {
    interface View extends BaseView {

        @Override
        void setTitle(String title);

        @Override
        void showDescription(String description);

        void showActiviteStates(boolean isActive);
    }

    interface Presenter extends BasePresenter<MvpContract.View> {
        void saveTask(String title, String description);
    }
}

4.在presenter目录结构下创建MvpPresenter.java

Android MVP在项目中的用法!!!_第4张图片

代码如下:

package com.luobo.mvp.presenter;

import android.content.Context;

import com.luobo.mvp.contract.MvpContract;

public class MvpPresenter implements MvpContract.Presenter {
    private MvpContract.View mBindView;
    private Context mContext;

    @Override
    public void attachView(MvpContract.View view) {
        mBindView = view;
        mContext = view.getContext();
    }

    @Override
    public void deachView() {
        mBindView = null;

    }

    @Override
    public void saveTask(String title, String description) {
        if (null != mBindView) {
            mBindView.showActiviteStates(true);
            mBindView.setTitle(title);
            mBindView.showDescription(description);
        }
    }
}

5.在model目录结构下创建MvpActivity.java

  • MvpActivity 继承BaseActivity 实现MvpContract.View接口,此时已经通过Presenter可以进行View和Model的通信,代码如下:
package com.luobo.mvp.model;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.luobo.mvp.R;
import com.luobo.mvp.base.BaseActivity;
import com.luobo.mvp.contract.MvpContract;
import com.luobo.mvp.presenter.MvpPresenter;

public class MvpActivity extends BaseActivity<MvpPresenter> implements MvpContract.View {
   private MvpContract.Presenter mPresenter;
   private Button saveBtn;
   private EditText titleEdit, descriptionEdit;
   TextView titleContex, descriptionContext;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_mvp);
       initData();
       initView();
   }

   private void initView() {
       saveBtn = findViewById(R.id.save_task);
       titleEdit = findViewById(R.id.title);
       descriptionEdit = findViewById(R.id.description);

       titleContex = findViewById(R.id.title_context);
       descriptionContext = findViewById(R.id.description_context);

       saveBtn.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {
               mPresenter.saveTask(null == titleEdit.getText() ? "无标题" : titleEdit.getText().toString(),
                       null == descriptionEdit.getText() ? "无描述" : descriptionEdit.getText().toString());
           }
       });
   }

   private void initData() {
       mPresenter = new MvpPresenter();
       mPresenter.attachView(this);
   }

   @Override
   public Context getContext() {
       return MvpActivity.this;
   }

   @Override
   public void setTitle(String title) {
       titleContex.setText(title);
   }

   @Override
   public void showDescription(String description) {
       descriptionContext.setText(description);
   }

   @Override
   public void showActiviteStates(boolean isActive) {

   }
}

到这里,你已经学会了MVP在代码中的实现,根据自己的业务逻辑,开始试一试吧

你可能感兴趣的:(Android,安卓,mvp)