1.m指model谈秫,在項(xiàng)目中主要是指實(shí)體類和業(yè)務(wù)邏輯處理類
2.v指view,因?yàn)閙vp框架中把model和view進(jìn)行解耦,所以view功能點(diǎn)僅在于布局交互和生命周期的一些應(yīng)用。
3.p指presenter息尺,主要以接口的方式在view和model中進(jìn)行兩者間的信息交互。
下面是我寫的一個簡單的用戶登錄demo簡單來說明一下疾掰,
其中個項(xiàng)目中model中的model主要負(fù)責(zé)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) {
//進(jìn)行登陸
//進(jìn)行模擬登錄
new Thread(){
@Override
public void run() {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//進(jìn)行模擬登錄
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中負(fù)責(zé)接入的接口LoginView
/**
* Created by Mr.wang
* Date 2016/7/5
* E-mail 1678173987@qq.com
* Describe 登陸接口進(jìn)行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);//跳轉(zhuǎn)到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) {
//進(jìn)行登錄
loginpresenter.login();
}
});
btnclear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//進(jìn)行清除
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,"跳轉(zhuǎn)",Toast.LENGTH_SHORT).show();
}
@Override
public void showFailedError() {
Toast.makeText(this,"登錄失敗",Toast.LENGTH_SHORT).show();
}
}
mvp中負(fù)責(zé)解耦模塊的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;//進(jìn)行view的控制
private ILogin login;//鏈接model
private Handler handler=new Handler();
public LoginPresenter(ILoginView iLoginView){
//構(gòu)造器進(jìn)行初始化對象的時候進(jìn)行初始化數(shù)據(jù)使用的勒葱,
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) {
//登陸成功進(jìn)行更新 ui
handler.post(new Runnable() {
@Override
public void run() {
loginView.toMainActivity(model);
loginView.hideLoading();
}
});
}
@Override
public void onFailed() {
//同樣進(jìn)行更新ui
handler.post(new Runnable() {
@Override
public void run() {
loginView.showFailedError();
loginView.hideLoading();
}
});
}
});
}
/**
* 進(jìn)行clear
*/
public void clear(){
loginView.clearUsername();
loginView.clearPassword();
}
}
附上demo地址
github上的demo地址
有問題加博主創(chuàng)建的Android高級技術(shù)交流群 :570650538