andrid中mvp简单实例demo登录和注册
首先简单介绍一下MVP的构成及优缺点:
MVP的全称为Model-View-Presenter,
Model提供数据(网络请求、数据存储等);
View负责页面显示;
Presenter负责逻辑的处理;
优点:主要就是将Model层和View层完全解耦,使得两者任一方的修改都不会对另一方产生影响,而是将逻辑处理放在了Presenter层;
缺点:显而易见,随着项目的增大,Presenter层也会变得臃肿,所以根据不同项目逻辑灵活的去使用Presenter也很重要;
之前有做过基于MVP的Android项目,根据别人写好的结构,无非就是填充代码,对于MVP整体的理解不够到位,所以该如何从头去理解一个MVP项目,对今后避免一些逻辑错误也是至关重要的。
下面我会以简单的登录模块为例,结合具体代码将整个MVP分步来构建,以便更好的去理解MVP,有需要的可以将完整demo去文末clone下来。
1.View–因为是负责页面的显示所以我们要写一个ILoginView的接口内里面写上比如显示进度 应酬进度 登录成功 登录失败用来提示客户
public interface ILoginView {
void loginSuccess(UserBean userBean);
void loginFailed(int status, String errorMsg);
}
2.Model–因为model主要是处理数据问题,所以我们要写一个ILoginModel的接口类,写一个登录的抽象方法
public interface ILoginModel {
//登录
void toLogin(String username, String password,ResultCallback callback);
}
3.接着我们要写一个Model的实现类,重写Model里面的抽象方法(实现登录的请求)
public class LoginModelImpl implements ILoginModel{
@Override
public void toLogin(String username, String password,ResultCallback callback) {
UserManager.getUserManager().toLogin(username,password,callback);
}
}
4.Presenter–作为负责处理逻辑,当view和Model写完后我们就应该写Presenter来将view和Model联系起来
首先我们要写一个presenter的接口类,写一个登录的抽象方法实现
public interface ILoginPresenter extends BasePresenter{
void toLogin(String username, String password);
}
接着我们要写一个presenter的实现类,重写登录方法
public class LoginPresenterImpl extends BasePresenterImpl implements ILoginPresenter {
private ILoginModel loginModel;
public LoginPresenterImpl(ILoginView view) {
super(view);
mView = view;
loginModel = new LoginModelImpl();//这里要注意
}
@Override
public void toLogin(String username, String password) {
loginModel.toLogin(username, password, new ResultCallback<ResultBean<UBean>>() {
@Override
public void onError(int status, String errorMsg) {
mView.loginFailed( status, errorMsg);
}
@Override
public void onResponse(ResultBean<UBean> response) {
UBean uBean=response.getData();
UserBean userBean=uBean.getUser();
mView.loginSuccess(userBean);
}
});
}}
5.接下来就开始写activity
/**
* 登录界面
* Created by wangbin on 16/12/22.
*/
public class LoginAct extends BaseAct<ILoginPresenter> implements ILoginView {
@BindView(R.id.login_phone)
EditText login_phone;
@BindView(R.id.login_pwd)
EditText login_pwd;
private String username, pwd;
@Override
public int getLayout() {
return R.layout.act_login;
}
@Override
public void init(Bundle savedInstanceState) {
mPresenter = new LoginPresenterImpl(this);
}
@OnClick({R.id.tv_toRegister, R.id.tv_toForget, R.id.btn_login})
public void OnClick(View v) {
switch (v.getId()) {
case R.id.tv_toRegister://注册
AppManager.getAppManager().addActivity(LoginAct.this);
openActivity(RegisterAct.class);
break;
case R.id.tv_toForget://忘记密码
openActivity(ForgetPwdAct.class);
break;
case R.id.btn_login://登录
if (isValidate()) {
showProgress("正在登录中...");
mPresenter.toLogin(username, pwd);
}
break;
}
}
@Override
public void loginSuccess(UserBean userBean) {
hideProgress();
toast("登录成功");
openActivity(MainAct.class);
// sendBroadcast(new Intent(HomeFrg.UPDATA));
finish();
}
/**
* 失败
*
* @param status
* @param errorMsg
*/
@Override
public void loginFailed(int status, String errorMsg) {
hideProgress();
toast(errorMsg);
}
private boolean isValidate() {
username = login_phone.getText().toString().trim();
pwd = login_pwd.getText().toString().toString();
if (TextUtils.isEmpty(username)) {
toast("请输入手机号码!");
login_phone.requestFocus();
return false;
}
if (TextUtils.isEmpty(pwd)) {
toast("请输入密码!");
login_pwd.requestFocus();
return false;
}
return true;
}
}
Base基类
**
-
基础类
-
Created by wangbin on 16/12/22.
*/public abstract class BaseAct extends FragmentActivity implements BaseView {
private TextView tv_title;
private TextView tvRight;
private ImageView ivRight;
private RelativeLayout rl_right;
private SafeDialog dialog;
/**
* 将代理类通用行为抽出来
*/
protected T mPresenter;@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 手机窗口设置无标题 requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(View.inflate(this, getLayout(), null)); //注入控件 ButterKnife.bind(this); init(savedInstanceState); } /** * 设置布局 */ public abstract int getLayout(); /** * 填充数据 */ public abstract void init(Bundle savedInstanceState); @Override protected void onResume() { super.onResume(); if (mPresenter != null) { mPresenter.onResume(); } } @Override protected void onDestroy() { super.onDestroy(); if (mPresenter != null) { mPresenter.onDestroy(); } } @Override public void toast(String msg) { MyToastUtils.showShortToast(getApplicationContext(), msg); } @Override public void showProgress(String message) { dialog=new SafeDialog(this); dialog.setMessage(message); dialog.setCancelable(false); dialog.show(); } @Override public void hideProgress() { if(dialog!=null){ dialog.dismiss(); } } @Override public void openActivity(Class<?> pClass) { openActivity(pClass, null); } @Override public void openActivity(Class<?> pClass, Bundle pBundle) { Intent intent = new Intent(this, pClass); if (pBundle != null) { intent.putExtras(pBundle); } startActivity(intent); } /** * 设置顶部标题 * * @param title */ public void setTopTitle(String title) { tv_title = (TextView) findViewById(R.id.tv_title); if (title != null) { tv_title.setText(title); } } /** * 顶部右边按键 * * @param rightText */ public void setRightBtn(String rightText, View.OnClickListener listener) { tvRight = (TextView) findViewById(R.id.tvRight); rl_right = (RelativeLayout) findViewById(R.id.rl_right); if (!TextUtils.isEmpty(rightText)) { tvRight.setText(rightText); rl_right.setVisibility(View.VISIBLE); tvRight.setVisibility(View.VISIBLE); } if (listener != null) { rl_right.setOnClickListener(listener); } } /** * 顶部右边按键(带图片) * * @param res */ public void setRightBtn(int res, View.OnClickListener listener) { ivRight = (ImageView) findViewById(R.id.ivRight); rl_right = (RelativeLayout) findViewById(R.id.rl_right); rl_right.setVisibility(View.VISIBLE); ivRight.setVisibility(View.VISIBLE); ivRight.setImageResource(res); if (listener != null) { rl_right.setOnClickListener(listener); } } public void goBack(View view) { finish(); }
}