Use of the MVP mode for Android development

Source: Internet
Author: User

A few days ago found that in the Android project code there is an activity class number actually has more than 1000 rows, and 600 lines are logical control, real and page control processing related code is not much, although you can use #region <>...# Endregion block package up, but overall, the page and logic processing too tightly, sometimes code reuse is not convenient, so, decided to refactor, find a bit, have MVP(Model-view-presenter,model layer is responsible for data management, The view layer is responsible for the page control data presentation and settings, presenter is responsible for logical processing, control how the view layer display and display data, this level of design, although the code files, but the overall logical division is clear, for the team division and testing is very convenient style architecture is good, Understand, not difficult, get a half-day, although a few more code files and functions, but the original activity class row number reduced to more than 400 lines, the logic looks more refreshing.

Now create a new test project to illustrate how the MVP is used:

1. New test Project:

    

2. Modify the interface file as follows:

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "xmlns:tools=" Http://schemas.android.com/tools "android:id=" @+id/activity_main "Android:layout_width=" mat Ch_parent "android:layout_height=" match_parent "android:paddingleft=" @dimen/activity_horizontal_margin "android:p addingright= "@dimen/activity_horizontal_margin" android:paddingtop= "@dimen/activity_vertical_margin" Android: paddingbottom= "@dimen/activity_vertical_margin" android:orientation= "vertical" tools:context= " Cn.linjk.testmvp.MainActivity "> <textview android:id=" @+id/tv_info "android:layout_gravity=" Center_ Horizontal "android:layout_width=" Match_parent "android:layout_height=" wrap_content "android:text=" He        Llo world! " android:textalignment= "center"/> <button android:id= "@+id/btn_edit_info" android:layout_width= "MATC H_parent "android:layout_height=" WRAp_content "android:layout_margintop=" 10DP "android:text=" Modify Info "/></linearlayout> 

3. Add interface to modify interface:

Package cn.linjk.testmvp.views;/** * Created by LINJK on 24/12/2016. */public interface Imainview {    String gettextviewinfo ();    void Settextviewinfo (String info);}

4. In mainactivity implement this interface to modify the interface Imainview:

package Cn.linjk.testmvp;import android.support.v7.app.AppCompatActivity; Import Android.os.bundle;import Android.widget.button;import Android.widget.textview;import Cn.linjk.testmvp.views.imainview;public class Mainactivity extends appcompatactivity implements imainview{private T    Extview Tvininfo;    Private Button Btnmodifyinfo;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main);    Initviewcomponents ();    } @Override Public String Gettextviewinfo () {return Tvininfo.gettext (). toString ();    } @Override public void Settextviewinfo (String info) {Tvininfo.settext (info);        } private void Initviewcomponents () {tvininfo = (TextView) Findviewbyid (r.id.tv_info);    Btnmodifyinfo = (Button) Findviewbyid (r.id.btn_edit_info); }}

5. Add Mainactivity Controller Interface Imainviewpresenter to control the display and content setting logic of the interface:

Package cn.linjk.testmvp.presenters;/** * Created by LINJK on 24/12/2016. */public interface Imainviewpresenter {    void Modifytextviewinfo (String isssnfo);}

6. Implement the Controller interface:

Package Cn.linjk.testmvp.presenters;import cn.linjk.testmvp.views.imainview;/** * Created by LINJK on 24/12/2016. */public class Mainviewpresenter implements imainviewpresenter{    private Imainview Imainview;    Public Mainviewpresenter (Imainview pimainview) {        this.imainview = Pimainview;    }    @Override public    void Modifytextviewinfo (String info) {        imainview.settextviewinfo (info);}    }

7. Add controller in Mainactivity, add control logic, modify mainactivity after code as follows:

Package Cn.linjk.testmvp;import Android.support.v7.app.appcompatactivity;import Android.os.bundle;import Android.view.view;import Android.widget.button;import Android.widget.textview;import Cn.linjk.testmvp.presenters.mainviewpresenter;import Cn.linjk.testmvp.views.imainview;public class MainActivity    Extends Appcompatactivity implements imainview{private TextView tvininfo;    Private Button Btnmodifyinfo;    Private Mainviewpresenter Mainviewpresenter;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main);        Initviewcomponents ();    Initglobalvars ();    } @Override Public String Gettextviewinfo () {return Tvininfo.gettext (). toString ();    } @Override public void Settextviewinfo (String info) {Tvininfo.settext (info);        } private void Initviewcomponents () {tvininfo = (TextView) Findviewbyid (r.id.tv_info); Btnmodifyinfo = (BUtton) Findviewbyid (r.id.btn_edit_info); Btnmodifyinfo.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View v)            {Mainviewpresenter.modifytextviewinfo ("Android MVP");    }        });    private void Initglobalvars () {mainviewpresenter = new Mainviewpresenter (this); }}

8. Run the code at this time to complete the purpose.

From the above code logic can be seen, in this way to write code, Mainactivity class logic is clear, there is not too much logic control code, logic control is handled in the controller, so in the team division of labor is also easy to allocate, as long as the business interface is written, you can assign the interface to the implementation of personnel, of course, One drawback is that it is easy to reach 65535 of this method limit after the project is complex, so that the other part of the DEX process is OK. This programming model is worth recommending.

Use of the MVP mode for Android development

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.