MVP-Model View Presenter
Martinfowler's article divides the MVP into Supervisingpresenter and Passiveview
Examples of MVP in Android: HTTPS://GITHUB.COM/ANTONIOLG/ANDROIDMVP
First look at the structure of the project
It can be seen that each function module contains Xxactivity,xxview (Interface), Xxpresenter (Interface), Xxpresenterimp
LoginView interface, responsible for logical processing
Public interface LoginView {public void showprogress (); public void hideprogress (); public void Setusernameerror (); public void Setpassworderror (); public void Navigatetohome ();}
Loginpresenter interface
Public interface Loginpresenter {public void Validatecredentials (string username, string password);}
Loginactivity, implements the LoginView interface, implements the corresponding method, contains a presenter as a member variable, PRESENTERIMP call the response method for UI update and synchronization
public class Loginactivity extends Activity implements LoginView, View.onclicklistener {private ProgressBar PROGRESSBA R Private EditText username; private EditText password; Private Loginpresenter presenter; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_login); ProgressBar = (ProgressBar) Findviewbyid (r.id.progress); Username = (EditText) Findviewbyid (r.id.username); Password = (EditText) Findviewbyid (R.id.password); Findviewbyid (R.id.button). Setonclicklistener (this); Presenter = new Loginpresenterimpl (this); } @Override public void ShowProgress () {progressbar.setvisibility (view.visible); } @Override public void Hideprogress () {progressbar.setvisibility (view.gone); } @Override public void Setusernameerror () {Username.seterror (getString (r.string.username_error)); } @Override public voidSetpassworderror () {Password.seterror (getString (r.string.password_error)); } @Override public void Navigatetohome () {startactivity (new Intent (this, mainactivity.class)); Finish (); } @Override public void OnClick (View v) {presenter.validatecredentials (Username.gettext (). toString (), password. GetText (). toString ()); }}
Loginpresenterimp, it realizes the Loginpresenter
Ublic class Loginpresenterimpl implements Loginpresenter, Onloginfinishedlistener { private LoginView LoginView; Private Logininteractor Logininteractor; Public Loginpresenterimpl (LoginView LoginView) { this.loginview = LoginView; This.logininteractor = new Logininteractorimpl (); } @Override public void Validatecredentials (string username, string password) { loginview.showprogress (); Logininteractor.login (username, password, this); } @Override public void Onusernameerror () { loginview.setusernameerror (); Loginview.hideprogress (); } @Override public void Onpassworderror () { loginview.setpassworderror (); Loginview.hideprogress (); } @Override public void onsuccess () { loginview.navigatetohome (); }}
The entire project architecture separates logical processing and UI synchronization updates from activity, and the code is more concise
For the MVP is not too understanding, but also need to see more examples Ah
About the MVP improvements: Blogs
Android MVP Learning