1.m指model,在项目中主要是指实体类和业务逻辑处理类
2.v指view,因为mvp框架中把model和view进行解耦,所以view功能点仅在于布局交互和生命周期的一些应用。
3.p指presenter,主要以接口的方式在view和model中进行两者间的信息交互。
下面是我写的一个简单的用户登录demo简单来说明一下,
其中个项目中model中的model主要负责bean类和逻辑处理类,还有与presenter交互的接口ILogin。
/**
* Created by Mr.wang
* Date 2016/7/5
* E-mail 1678173987@qq.com
* Describe model类别,对接presenter
*/
public interface ILogin {
public void login(String username, String password, final OnLoginListener listener);
public interface OnLoginListener{
void onLoginSuccess( LoginModel model);
void onFailed();
}
}
model中的bean类 LoginModel
/**
* Created by Mr.wang
* Date 2016/7/5
* E-mail 1678173987@qq.com
* Describe 登陆的model
*/
public class LoginModel {
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;
}
@Override
public String toString() {
return "LoginModel{" +
"username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
model中的逻辑处理类Login
/**
* Created by Mr.wang
* Date 2016/7/5
* E-mail 1678173987@qq.com
* Describe
*/
public class Login implements ILogin{
@Override
public void login(final String username, final String password, final OnLoginListener listener) {
//进行登陆
//进行模拟登录
new Thread(){
@Override
public void run() {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//进行模拟登录
if("123456".equals(username)&&"123".equals(password)){
LoginModel loginModel=new LoginModel();
loginModel.setUsername(username);
loginModel.setPassword(password);
listener.onLoginSuccess(loginModel);
}else{
listener.onFailed();
}
}
}.start();
}
}
view中负责接入的接口LoginView
/**
* Created by Mr.wang
* Date 2016/7/5
* E-mail 1678173987@qq.com
* Describe 登陆接口进行view登陆的接入
*/
public interface ILoginView {
String getUsername();//得到username
String getPassword();//得到password
void clearUsername();//清除username
void clearPassword();//清除password
void showLoading();//显示loading
void hideLoading();//隐藏loading
void toMainActivity(LoginModel model);//跳转到mainActivity
void showFailedError();//显示错误信息
}
view中UI交互的LoginActivity
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 com.peng.mvpdemo.R;
import com.peng.mvpdemo.model.LoginModel;
import com.peng.mvpdemo.presenter.LoginPresenter;
/**
* Created by Mr.wang
* Date 2016/7/5
* E-mail 1678173987@qq.com
* Describe 登陆界面
*/
public class LoginActivity extends AppCompatActivity implements ILoginView{
private EditText etusername,etpassword;
private Button btnlogin,btnclear;
private ProgressBar progressbar;
private LoginPresenter loginpresenter=new LoginPresenter(this);//view与presenter交互的地方
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
initView();
}
private void initView() {
etusername= (EditText) findViewById(R.id.edit_username);
etpassword= (EditText) findViewById(R.id.edit_password);
btnlogin= (Button) findViewById(R.id.login);
btnclear= (Button) findViewById(R.id.clear);
progressbar= (ProgressBar) findViewById(R.id.progressBar);
btnlogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//进行登录
loginpresenter.login();
}
});
btnclear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//进行清除
loginpresenter.clear();
}
});
}
@Override
public String getUsername() {
return etusername.getText().toString().trim();
}
@Override
public String getPassword() {
return etpassword.getText().toString().trim();
}
@Override
public void clearUsername() {
etusername.setText("");
}
@Override
public void clearPassword() {
etpassword.setText("");
}
@Override
public void showLoading() {
progressbar.setVisibility(View.VISIBLE);
}
@Override
public void hideLoading() {
progressbar.setVisibility(View.INVISIBLE);
}
@Override
public void toMainActivity(LoginModel model) {
Toast.makeText(this,"跳转",Toast.LENGTH_SHORT).show();
}
@Override
public void showFailedError() {
Toast.makeText(this,"登录失败",Toast.LENGTH_SHORT).show();
}
}
mvp中负责解耦模块的presenter
import android.os.Handler;
import com.peng.mvpdemo.model.ILogin;
import com.peng.mvpdemo.model.Login;
import com.peng.mvpdemo.model.LoginModel;
import com.peng.mvpdemo.view.ILoginView;
/**
* Created by Mr.wang
* Date 2016/7/5
* E-mail 1678173987@qq.com
* Describe 登陆的presenter
*/
public class LoginPresenter {
private ILoginView loginView;//进行view的控制
private ILogin login;//链接model
private Handler handler=new Handler();
public LoginPresenter(ILoginView iLoginView){
//构造器进行初始化对象的时候进行初始化数据使用的,
this.loginView=iLoginView;
login=new Login();
}
/**
* 登陆
*/
public void login(){
loginView.showLoading();//显示loading
login.login(loginView.getUsername(), loginView.getPassword(), new ILogin.OnLoginListener() {
@Override
public void onLoginSuccess(final LoginModel model) {
//登陆成功进行更新 ui
handler.post(new Runnable() {
@Override
public void run() {
loginView.toMainActivity(model);
loginView.hideLoading();
}
});
}
@Override
public void onFailed() {
//同样进行更新ui
handler.post(new Runnable() {
@Override
public void run() {
loginView.showFailedError();
loginView.hideLoading();
}
});
}
});
}
/**
* 进行clear
*/
public void clear(){
loginView.clearUsername();
loginView.clearPassword();
}
}
附上demo地址
github上的demo地址
有问题加博主创建的Android高级技术交流群 :570650538