Android MVP 学习

MVP -- Model View Presenter

MartinFowler 的 这篇文章 将MVP分为 SupervisingPresenter 和 PassiveView


Android 中MVP 例子: https://github.com/antoniolg/androidmvp

先看一下项目的结构

                    


可以看出每个功能模块包含 XXActivity,XXView (Interface),XXPresenter(interface),XXPresenterImp


LoginView接口,负责逻辑处理

public interface LoginView {
    public void showProgress();

    public void hideProgress();

    public void setUsernameError();

    public void setPasswordError();

    public void navigateToHome();
}


LoginPresenter接口

public interface LoginPresenter {
    public void validateCredentials(String username, String password);
}

LoginActivity,实现了LoginView接口,实现了对应的方法,内部包含一个Presenter作为成员变量,presenterImp调用响应的方法进行UI更新与同步

public class LoginActivity extends Activity implements LoginView, View.OnClickListener {

    private ProgressBar progressBar;
    private EditText username;
    private EditText password;
    private LoginPresenter presenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        progressBar = (ProgressBar) findViewById(R.id.progress);
        username = (EditText) findViewById(R.id.username);
        password = (EditText) findViewById(R.id.password);
        findViewById(R.id.button).setOnClickListener(this);

        presenter = new LoginPresenterImpl(this);
    }

    @Override public void showProgress() {
        progressBar.setVisibility(View.VISIBLE);
    }

    @Override public void hideProgress() {
        progressBar.setVisibility(View.GONE);
    }

    @Override public void setUsernameError() {
        username.setError(getString(R.string.username_error));
    }

    @Override public void setPasswordError() {
        password.setError(getString(R.string.password_error));
    }

    @Override public void navigateToHome() {
        startActivity(new Intent(this, MainActivity.class));
        finish();
    }

    @Override public void onClick(View v) {
        presenter.validateCredentials(username.getText().toString(), password.getText().toString());
    }
}


LoginPresenterImp,实现了LoginPresenter
ublic class LoginPresenterImpl implements LoginPresenter, OnLoginFinishedListener {

    private LoginView loginView;
    private LoginInteractor loginInteractor;

    public LoginPresenterImpl(LoginView loginView) {
        this.loginView = loginView;
        this.loginInteractor = new LoginInteractorImpl();
    }

    @Override public void validateCredentials(String username, String password) {
        loginView.showProgress();
        loginInteractor.login(username, password, this);
    }

    @Override public void onUsernameError() {
        loginView.setUsernameError();
        loginView.hideProgress();
    }

    @Override public void onPasswordError() {
        loginView.setPasswordError();
        loginView.hideProgress();
    }

    @Override public void onSuccess() {
        loginView.navigateToHome();
    }
}

整个项目架构中,将逻辑处理和UI同步更新从Activity中分离出来,代码更加简洁

对于MVP还不是太理解,还需要再多看例子啊


关于MVP的改进  :博客