刚开始对mvp的架构模式很模糊,而且也有很多文章对mvp模式的介绍,说实话,看了那么多知道的都是一些文字上的东西。真的需要手动实际去操作,才能真正领悟mvp的极度舒适的感觉。
话不多说,首先我们做一个登录的简单例子。
首先是布局:
布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_marginTop="20dp"
android:layout_marginRight="20dp"
android:layout_marginLeft="20dp"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:textSize="20sp"
android:textColor="#000000"
android:text="用户名:"
android:layout_width="100dp"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/et_username"
android:hint="请输入用户名"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_marginTop="20dp"
android:layout_marginRight="20dp"
android:layout_marginLeft="20dp"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:textSize="20sp"
android:textColor="#000000"
android:text="密码:"
android:layout_width="100dp"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/et_passworld"
android:hint="请输入用户名"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:gravity="center"
android:layout_gravity="center"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/btn_sure"
android:gravity="center"
android:layout_gravity="center"
android:text="login"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn_cancle"
android:gravity="center"
android:layout_gravity="center"
android:text="clear"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content" />
</LinearLayout>
<ProgressBar
android:visibility="gone"
android:id="@+id/pb_loading"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
然后我们从布局中可以看到,其实主要就是俩个功能,1 登录 2 清空。
然后首先我们先写bean类:
public class User {
private String userName;
private String 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;
}
}
然后presenter和moudle之间也是通过接口进行交互的。
所以写一个接口,用于之后的操作:
public interface IUser {
public void login(String userNmae,String passWorld,LogInListener logInListener);
}
然后这里需要我们一个监听的回调:
public interface LogInListener {
void loginSucceed(User user);
void loginFaile();
}
然后是我们的presenter:
public class UserPresenter implements IUser {
@Override
public void login(final String userNmae, final String passWorld, final LogInListener logInListener) {
new Thread(new Runnable() {
@Override
public void run() {
if (TextUtils.equals(userNmae,"pw") && TextUtils.equals(passWorld,"123")){
User user = new User();
user.setUserName(userNmae);
user.setPassWord(passWorld);
logInListener.loginSucceed(user);
}else {
logInListener.loginFaile();
}
}
}).start();
}
}
然后是我们的presenter和view之间的接口:
public interface IloginView {
String getUserName();
String getPassWorld();
void clearUserName();
void clearPassWorld();
void toMainActivity(User user);
void showFaileToast();
void showLoading();
void hideLoading();
}
然后就是我们的逻辑操作了:
public class UserPresenterManager {
private final IloginView loginView;
private final UserPresenter userPresenter;
private Handler mHandler = new Handler();
public UserPresenterManager(IloginView iloginView) {
this.loginView = iloginView;
this.userPresenter = new UserPresenter();
}
public void login(){
loginView.showLoading();
userPresenter.login(loginView.getUserName(), loginView.getPassWorld(), new LogInListener() {
@Override
public void loginSucceed(final User user) {
mHandler.post(new Runnable() {
@Override
public void run() {
loginView.toMainActivity(user);
loginView.hideLoading();
}
});
}
@Override
public void loginFaile() {
mHandler.post(new Runnable() {
@Override
public void run() {
loginView.showFaileToast();
loginView.hideLoading();
}
});
}
});
}
public void clear() {
loginView.clearUserName();
loginView.clearPassWorld();
}
}
然后是我们的view:
public class MainActivity extends AppCompatActivity implements IloginView{
private EditText et_userName;
private EditText et_userPassWorld;
private Button btn_sure;
private Button btn_cancle;
private ProgressBar pb_loading;
private UserPresenterManager loginPresenter = new UserPresenterManager(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
}
private void initViews() {
et_userName = findViewById(R.id.et_username);
et_userPassWorld = findViewById(R.id.et_passworld);
pb_loading = findViewById(R.id.pb_loading);
btn_sure = findViewById(R.id.btn_sure);
btn_cancle = findViewById(R.id.btn_cancle);
btn_sure.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
loginPresenter.login();
}
});
btn_cancle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
loginPresenter.clear();
}
});
}
@Override
public String getUserName() {
return et_userName.getText().toString() == null ? "" : et_userName.getText().toString();
}
@Override
public String getPassWorld() {
return et_userPassWorld.getText().toString() == null ? "" : et_userPassWorld.getText().toString();
}
@Override
public void clearUserName() {
et_userName.setText("");
}
@Override
public void clearPassWorld() {
et_userPassWorld.setText("");
}
@Override
public void toMainActivity(User user) {
Toast.makeText(this, "succeed:"+"userName="+user.getUserName(), Toast.LENGTH_SHORT).show();
}
@Override
public void showFaileToast() {
Toast.makeText(this, "faile", Toast.LENGTH_SHORT).show();
}
@Override
public void showLoading() {
pb_loading.setVisibility(View.VISIBLE);
}
@Override
public void hideLoading() {
pb_loading.setVisibility(View.GONE);
}
}
ok,完美了。