So far, there's not a single standard for MVP use, so let's take a look at some of the things you've learned about using MVP in Android.
Developed in the traditional way, often makes the activity mixed with UI interaction, business logic and other processes. The MVP model is a clever solution to this problem. Let's go straight to a small example.
/** * Defines an interface that operates on UI components, allowing the activity to implement this interface * @author Quinn * @date 2015-5-9 */public interface LoginView {public void Sho Wprogress (); public void hideprogress (); public void Setusernameerror (); public void Setpassworderror (); public void Navigatetohome ();}
/** * Activity needs to implement the view interface, and holds a presenter object that transfers the logic to presenter * @author Quinn * @date 2015-5-9 */public class Loginactivit Y extends Activity implements Loginview,view.onclicklistener {private ProgressBar progressbar;private EditText username ;p rivate EditText password;private loginpresenter presenter; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_login);p Rogressbar = ( ProgressBar) Findviewbyid (r.id.progress) Username = (EditText) Findviewbyid (r.id.username);p assword = (EditText) Findviewbyid (R.id.password); Findviewbyid (R.id.button) Setonclicklistener (this);p resenter = new Loginpresenterimpl (this);} @Overridepublic void ShowProgress () {progressbar.setvisibility (view.visible);} @Overridepublic void Hideprogress () {progressbar.setvisibility (view.gone);} @Overridepublic void Setusernameerror () {Username.seterror (getString (R.string.username_error));} @Overridepublic void Setpassworderror () {Password.seterror (GetstriNg (R.string.password_error));} @Overridepublic void Navigatetohome () {startactivity (new Intent (this, mainactivity.class); Finish ();} @Overridepublic void OnClick (View v) {presenter.validatecredentials (Username.gettext (). toString (), Password.gettext (). toString ());}}
/** * Presenter interface, declaring the login logic processing interface * @author Quinn * @date 2015-5-9 */public interface Loginpresenter {public void Valida Tecredentials (string Username, string password);}
/** * Presenter specific implementations, holding view and Interator references, handling UI responses, data processing, etc. * where interactor represents the model, you can handle each use case by different interactor @author Quinn * @date 2015-5-9 */public class Loginpresenterimpl implements Loginpresenter,onloginfinishedlistener {private LoginView loginview;private logininteractor logininteractor;public loginpresenterimpl (LoginView LoginView) { This.loginview = Loginview;this.logininteractor = new Logininteractorimpl ();} @Overridepublic void Validatecredentials (string username, string password) {loginview.showprogress (); Logininteractor.login (username, password, this);} @Overridepublic void Onusernameerror () {loginview.setusernameerror (); loginview.hideprogress ();} @Overridepublic void Onpassworderror () {loginview.setpassworderror (); loginview.hideprogress ();} @Overridepublic void onsuccess () {loginview.navigatetohome ();}}
/** * Processing Login interface * @author Quinn * @date 2015-5-9 */public interface Logininteractor {public void login (String username, String password, Onloginfinishedlistener listener);}
/** * login Interactor implementation , holding its reference in presenter * @author Quinn * @date 2015-5-9 */public class Logininteractorimpl implements Logininteractor {@Ov Erride public void Login (final string username, final string password, final Onloginfinishedlistener listener) { Mock Login. I ' m creating a handler to delay the answer a couple of seconds new Handler (). postdelayed (New Runnable () { @Override public void Run () {Boolean error = false; if (Textutils.isempty (username)) {listener.onusernameerror (); Error = TRUE; } if (Textutils.isempty (password)) {listener.onpassworderror (); Error = TRUE; } if (!error) {listener.onsuccess (); }}}, 2000); }}
/** * Login Result Callback Interface * @author Quinn * @date 2015-5-9 */public interface Onloginfinishedlistener {public void Onusernameerr or (); public void Onpassworderror (); public void onsuccess ();}
A little analysis of the above code interface.
View layer: Is a layer, the main is to define a view interface, declaring a variety of methods to process the UI components, and then implemented by the activity, but the last real call in the presenter, Because presenter is defined in activity, the view is passed to presenter as a parameter of the constructor.
Presenter layer: Business layer, Response UI layer, data layer, logic processing, holding interactor reference.
Model/interactor layer: Here The interactor is the model layer, it defines the specific behavior of the interface for presenter calls, you can implement different interactor processing different use case. such as data reading, data storage, network requests and so on operations.
Resources:
Http://antonioleiva.com/mvp-android/
Https://github.com/antoniolg/androidmvp
An analysis of MVP model in Android development