首先看下结构:Model、View(接口,Activity继承)、Presenter(包含Module、View)
- 数据逻辑相当于M
- Activity(负责View的绘制以及与用户交互)相当于V
- View与Model间的交互则为P
Mvp代码实现
代码很简单就用登录的简单逻辑
登录页面.png 代码.png
从M开始:一个Model(UserModel.Class),包含数据,以及对数据的一些处理,如判空、拼接等等;
接下来是V:一个View接口(ILoginView.Class),一个实体类Activity(LoginActivity继承接口ILoginView),所以Activity(负责View的绘制以及与用户交互)相当于V;
最后就是P了:一个Presenter接口(ILoginPresenter.Class),一个实体类(LoginPersenter继承接口ILoginPresenter,其实个人觉得可以不用写Presenter接口,直接写一个LoginPersenter实体类就行了),
Persenter其实是3个组成:1、一个View接口,2、一个Model实体,3、业务逻辑(只针对业务逻辑,如网络请求,不做数据处理,如判空、拼接等。
从M开始
UserModel.Class
public class UserModel {
private String username;
private String password;
public UserModel(String username, String password) {
this.username = username;
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int checkUserValidity(String username, String password) {
if (username == null || password == null ||
username.isEmpty() ||
password.isEmpty()) {
return -1;
}
return 0;
}
}
接下来是V
ILoginView.Class
public interface ILoginView {
void showProgress();
void hideProgress();
void setPasswordError();
String getUsername();
String getPassword();
void loginSuccess();
}
LoginActivity.Class
public class LoginActivity extends AppCompatActivity implements ILoginView,View.OnClickListener{
private EditText usernameEdit,passwrodEdit;
private Button loginButton;
ProgressDialog pd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mvplogin);
pd = new ProgressDialog(this);
usernameEdit = (EditText) findViewById(R.id.et_username);
passwrodEdit = (EditText) findViewById(R.id.et_username);
loginButton = (Button) findViewById(R.id.bt_login);
loginButton.setOnClickListener(this);
}
@Override
public void showProgress() {
pd.show();
}
@Override
public void hideProgress() {
pd.cancel();
}
@Override
public void setPasswordError() {
passwrodEdit.setError("passwrod error");
}
@Override
public String getUsername() {
return usernameEdit.getText().toString();
}
@Override
public String getPassword() {
return passwrodEdit.getText().toString();
}
@Override
public void loginSuccess() {
Toast.makeText(this, "login success", Toast.LENGTH_SHORT).show();
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.bt_login:
break;
}
}
最后就是P了
ILoginPresenter.Class
public interface ILoginPresenter {
void Login(String username, String password);
}
LoginPresenter.Class
public class LoginPersenter implements ILoginPresenter{
private ILoginView loginView;
private UserModel mUser;
public LoginPersenter(ILoginView loginView) {
this.loginView = loginView;
initUser();
}
private void initUser(){
mUser = new UserModel(loginView.getUsername(),loginView.getPassword());
}
@Override
public void Login(String username, String password) {
loginView.showProgress();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
loginView.hideProgress();
int code = mUser.checkUserValidity(loginView.getUsername(), loginView.getPassword());
if (code == -1) {
loginView.setPasswordError();
} else if (code == 0) {
loginView.loginSuccess();
}
}
},2000);
}
}
最后在LoginActivity中补上P的调用
//初始化
loginPresenter = new LoginPersenter(this);
//Click方法中的调用
loginPresenter.Login(usernameEdit.getText().toString(),passwrodEdit.getText().toString());
到这里MVP模式的代码就已经实现了。
数据流向:view->presenter->module->presenter->view
感谢:
Android开发架构选择MVP or MVVM
网友评论