
一、理解MVP
效果图:
项目结构:
实现
一、Model层
首先定义一个实体类User
package app.qcu.pmit.cn.mvpdemo.model; /** * Created by Administrator on 2018/5/24 0024. */ 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; } }
其次就是业务类,抽取了一个接口 IUserBiz,登录界面连接服务器有一个登录方法 Login() ,耗时操作需要开辟子线程,
package app.qcu.pmit.cn.mvpdemo.model; /** * Created by Administrator on 2018/5/24 0024. */ public class UserBiz implements IUserBiz { @Override public void login(final String name, final String pwd, final OnLoginListener listener) { new Thread() { @Override public void run() { try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } if ("yl".equals(name) && "123456".equals(pwd)) { User user = new User(); user.setUsername(name); user.setPassword(pwd); listener.loginSuccess(user); } else { listener.loginFaild(); } } }.start(); } }
package app.qcu.pmit.cn.mvpdemo.model; /** * Created by Administrator on 2018/5/24 0024. */ public interface IUserBiz { void login(String name, String pwd, OnLoginListener listener); }
再用一个回调接口来通知登录的状态,成功或者失败
package app.qcu.pmit.cn.mvpdemo.model; /** * Created by Administrator on 2018/5/24 0024. */ public interface OnLoginListener { void loginSuccess(User user); void loginFaild(); }
二、View层
UI的逻辑,抽取一个接口由Activity实现
package demo.example.com.mvp3.view; /** * Created by Administrator on 2018/5/9 0009. */ public interface IUserLoginView { String getName(); String getPwd(); void showLoading(); void hideLoading(); void toMainActivity(); void showFaildError(); void clearName(); void clearPwd(); }
package demo.example.com.mvp3.view; import android.content.Intent; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.ProgressBar; import android.widget.Toast; import demo.example.com.mvp3.MainActivity; import demo.example.com.mvp3.R; import demo.example.com.mvp3.presenter.UserLoginPresenter; /** * Created by Administrator on 2018/5/9 0009. */ public class UserLoginActivity extends AppCompatActivity implements IUserLoginView { private EditText etName, etPwd; private Button btnLogin, btnClear; private ProgressBar pbLogin; private UserLoginPresenter userLoginPresenter = new UserLoginPresenter(this); @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); initView(); } private void initView() { etName = findViewById(R.id.user_name); etPwd = findViewById(R.id.user_pwd); btnLogin = findViewById(R.id.btn_login); btnClear = findViewById(R.id.btn_clear); pbLogin = findViewById(R.id.pb_login); btnLogin.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { userLoginPresenter.Login(); } }); btnClear.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { userLoginPresenter.clear(); } }); } @Override public String getName() { return etName.getText().toString(); } @Override public String getPwd() { return etPwd.getText().toString(); } @Override public void showLoading() { pbLogin.setVisibility(View.VISIBLE); } @Override public void hideLoading() { pbLogin.setVisibility(View.GONE); } @Override public void toMainActivity() { startActivity(new Intent(this, MainActivity.class)); } @Override public void showFaildError() { Toast.makeText(this, "名字还是密码写错啦!", Toast.LENGTH_SHORT).show(); } @Override public void clearName() { etName.setText(""); } @Override public void clearPwd() { etPwd.setText(""); } }
三、Presenter层
界面整体的用户交互的功能有两个,“登录”和“清除”,所以Presenter中有两个方法,即 login() 和 clear(),使用view和model中的方法实现
package app.qcu.pmit.cn.mvpdemo.presenter; import android.os.Handler; import app.qcu.pmit.cn.mvpdemo.model.IUserBiz; import app.qcu.pmit.cn.mvpdemo.model.OnLoginListener; import app.qcu.pmit.cn.mvpdemo.model.User; import app.qcu.pmit.cn.mvpdemo.model.UserBiz; import app.qcu.pmit.cn.mvpdemo.view.IUserLoginView; /** * Created by Administrator on 2018/5/24 0024. */ public class UserLoginPresenter { private IUserBiz userBiz; private IUserLoginView userLoginView; private Handler mHandler = new Handler(); public UserLoginPresenter(IUserLoginView userLoginView) { this.userLoginView = userLoginView; this.userBiz = new UserBiz(); } public void Login() { userLoginView.showLoading(); userBiz.login(userLoginView.getName(), userLoginView.getPwd(), new OnLoginListener() { @Override public void loginSuccess(User user) { //需要在UI线程执行 mHandler.post(new Runnable() { @Override public void run() { userLoginView.toMainActivity(); userLoginView.hideLoading(); } }); } @Override public void loginFaild() { //需要在UI线程执行 mHandler.post(new Runnable() { @Override public void run() { userLoginView.showFaildError(); userLoginView.hideLoading(); } }); } }); } public void clear() { userLoginView.clearName(); userLoginView.clearPwd(); } }