Android MVP Learning

Source: Internet
Author: User
Tags gettext

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.