MVP簡易版(乞丐版)
阿新 • • 發佈:2018-12-29
Activity
import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.TextView; import zhao.com.modelviewp.R; import zhao.com.modelviewp.bean.Result; import zhao.com.modelviewp.bean.User; import zhao.com.modelviewp.presenter.MyPresenter; import zhao.com.modelviewp.view.IBaseView; public class MainActivity extends AppCompatActivity { TextView textView; private MyPresenter myPresenter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //獲取控制元件 textView = findViewById(R.id.textView); //建立presenter myPresenter = new MyPresenter(); //繫結IBaseView myPresenter.bindBaseView(new LoginCall()); } //button點選事件 public void login(View view){ //呼叫presenter層 myPresenter.login("張三","123"); } //實現view層IBaseView介面接收presenter處理資料 class LoginCall implements IBaseView<User>{ //請求成功 @Override public void success(User data) { textView.setText(data.getName()+" "+data.getAge()+" "+data.getToken()); } //請求失敗 @Override public void fail(Result result) { textView.setText(result.getMsg()); } } @Override protected void onDestroy() { super.onDestroy(); //解除繫結,防止記憶體碎片無法釋放 myPresenter.unbindBaseView(); } }
View層 IBaseView 介面類
import zhao.com.modelviewp.bean.Result;
public interface IBaseView<T> {
void success(T t);
void fail(Result result);
}
Presenter層 MyPresenter
import android.os.Handler; import android.os.Message; import zhao.com.modelviewp.bean.Result; import zhao.com.modelviewp.bean.User; import zhao.com.modelviewp.model.LoginModel; import zhao.com.modelviewp.view.IBaseView; public class MyPresenter { private IBaseView iBaseView; private Handler handler = new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); Result<User> userResult = (Result<User>) msg.obj; if (userResult.getStatus() == 0) { iBaseView.success(userResult.getT()); }else { iBaseView.fail(userResult); } } }; public void login(final String name, final String pwd) { //建立子執行緒請求model層資料 new Thread(new Runnable() { @Override public void run() { //呼叫model層資料 Result<User> userResult = new LoginModel().loginData(name,pwd); //通過Handler傳送出去 Message message = handler.obtainMessage(); message.obj = userResult; handler.sendMessage(message); } }).start(); } //繫結IBaseView public void bindBaseView(IBaseView iBaseView) { this.iBaseView = iBaseView; } //解除繫結 public void unbindBaseView() { iBaseView = null; } }
Model層 LoginModel
import zhao.com.modelviewp.bean.Result; import zhao.com.modelviewp.bean.User; public class LoginModel { public Result<User> loginData(String name, String pwd) { Result<User> userResult = new Result<>(); userResult.setStatus(0); userResult.setMsg("請求成功!"); User user = new User(); user.setId(100); user.setAge(20); user.setName("啊哈"); user.setToken("呃呃呃"); userResult.setT(user); return userResult; } }
封裝類 Result
public class Result<T> {
private int status;
private String msg;
private T t;
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public T getT() {
return t;
}
public void setT(T t) {
this.t = t;
}
}
User
public class User {
private int id;
private String name;
private int age;
private String token;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getToken() {
return token;
}
public void setToken(String token) {
this.token = token;
}
}