MVP模型一般要创建三个文件夹:View、Interactor(Model)、Presenter
每个部分都有其接口和实现类,就是为了方便回调
这里做一个登陆界面为例子:
接口:
InteractorInterface:
public interface LoginInteractor {
void login(String userName,String password,OnLoginFinishedListener listener);
}
PresenterInterface
public interface LoginPresenter {
void validateCredentials(String userName,String password);
void onDestroy();
}
public interface OnLoginFinishedListener {
void onUserNameError();
void onPasswordError();
void onSuccess();
}
ViewInterface
public interface LoginView {
void setUserNameError();
void setPasswordError();
void navigateToHome();
}
实现类:
InteractorImpl
public class LoginInteractorImpl implements LoginInteractor {
@Override
public void login(final String userName, final String password, final OnLoginFinishedListener listener) {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Log.i("ZhiXueHui", "LoginInteractorImpl-login");
boolean error = false;
if(TextUtils.isEmpty(userName)){
listener.onUserNameError();
error = true;
}
if(TextUtils.isEmpty(password)){
listener.onPasswordError();
error = true;
}
if(!error){
listener.onSuccess();
}
}
},2000);
}
}
PresenterImpl
public 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) {
Log.i("ZhiXueHui", "LoginPresenterImpl-validateCredentials");
loginInteractor.login(userName,password,this);
}
@Override
public void onDestroy() {
Log.i("ZhiXueHui", "LoginPresenterImpl-onDestroy");
loginView = null;
}
@Override
public void onUserNameError() {
Log.i("ZhiXueHui", "LoginPresenterImpl-onUserNameError");
if(loginView!=null){
loginView.setUserNameError();
}
}
@Override
public void onPasswordError() {
Log.i("ZhiXueHui", "LoginPresenterImpl-onPasswordError");
if(loginView!=null){
loginView.setPasswordError();
}
}
@Override
public void onSuccess() {
Log.i("ZhiXueHui", "LoginPresenterImpl-onSuccess");
if(loginView!=null){
loginView.navigateToHome();
}
}
}
ViewActivity
public class LoginActivity extends Activity implements LoginView, View.OnClickListener {
private EditText userName;
private EditText password;
private Button button;
private LoginPresenter presenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
Log.i("ZhiXueHui", "LoginActivity-onCreate");
userName = (EditText) findViewById(R.id.userName);
password = (EditText) findViewById(R.id.password);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(this);
presenter = new LoginPresenterImpl(this);
}
@Override
public void setUserNameError() {
Log.i("ZhiXueHui", "LoginActivity-setUserNameError");
userName.setError(getString(R.string.username_error));
}
@Override
public void setPasswordError() {
Log.i("ZhiXueHui", "LoginActivity-setPasswordError");
password.setError(getString(R.string.password_error));
}
@Override
public void navigateToHome() {
Log.i("ZhiXueHui", "LoginActivity-navigateToHome");
startActivity(new Intent(this, MainActivity.class));
finish();
}
@Override
public void onClick(View v) {
Log.i("ZhiXueHui", "LoginActivity-onClick");
presenter.validateCredentials(userName.getText().toString(),password.getText().toString());
}
}
流程图:
流程解释:
第一步:View的实现类调用Presenter的接口
第二步:Presenter的实现类调用Model的接口
第三步:Model的实现类回调Presenter的接口
第四步:Presenter的实现类回调View的接口
一个循环下来,正好是View->Presenter->Model->Presenter->View。
其中要注意的是,Presenter作为中转,分别对接View的实现类和Model的实现类,所以必须要建立两个接口