An MVP Model framework for Android

Source: Internet
Author: User

What we are sharing today is a schema that separates view initialization from logic and activity, using the MVP model. Unfortunately, this is only a new idea, and I found in practice that it does not completely separate the UI logic from the activity, so there is no way to think of this design as reasonable in practice. The idea is to think that activity is going to receive intent or to do a lot of other things, and it's hard to think that activity is a view-related class, so we're thinking of separating the logic of the view from the activity. In this way of separation, we are going to use a UI class interface. This idea comes from: Https://github.com/wongcain/MVP-Simple-Demo, I just analyze the author's code and modify it in small parts.

Viewuiimp.java

 PackageFrame.kale.com.frame;ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;/** * @authorJack Tony * @brief * @date 2015/4/4*/ Public InterfaceViewuiimp {/**     *     * @paramInflater *@paramContainer*/     Public voidinitviews (layoutinflater inflater, ViewGroup container);  PublicView Getrootview ();}

There are only two methods in this interface, one is the method of initializing views, and the other is the method of getting root view. We want the activity to interact with this interface only, without having to control the view logic. The logical judgment of view is done in the class that implements the interface. In order for the activity to be able to reuse the code better, here we need to create a base class for activity.

Basepresenteractivity.java

 PackageFrame.kale.com.frame;Importandroid.app.Activity;ImportAndroid.os.Bundle;/** * @authorJack Tony * @brief the base class of the activity-related performance layer * @date 2015/4/4*/ Public  Abstract classBasepresenteractivity <vextendsViewuiimp>extendsActivity {protectedV Uiimp;//objects of the Viewuiimp@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Try {            //produces an object and calls the Initviews method to initialize the viewsUiimp =Getviewuiclass (). newinstance (); Uiimp.initviews (Getlayoutinflater (),NULL); //set the root view to the activitySetcontentview (Uiimp.getrootview ()); //Binding viewsOnbindviewui (); } Catch(instantiationexception e) {e.printstacktrace (); } Catch(illegalaccessexception e) {e.printstacktrace (); }    }            /*** When activity exits, destroy views by Ondestroyviewui*/@Overrideprotected Final voidOnDestroy () {Ondestroyviewui (); Uiimp=NULL; Super. OnDestroy (); }    protected AbstractClass<v>Getviewuiclass (); /*** method to trigger when binding views*/    protected voidOnbindviewui () {}/*** method to trigger when moving out of views*/    protected voidOndestroyviewui () {}}

You can see that we have initialized a Viewuiimp object through Newinstance (), and we have the root view through Getrootview () and then set the root view into activity. What we see in these steps is the interface object, which has no entities and is highly flexible. In addition, when activity is initialized or destroyed, it triggers a callback when the view is bound or destroyed, so that we can manipulate it in the activity's sub-class.

Now that we've built the framework, try writing an implementation class to see how it works.

 PackageFrame.kale.com.frame;ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;ImportAndroid.widget.EditText;ImportAndroid.widget.TextView;/** * @authorJack Tony * @brief * @date 2015/4/4*/ Public classHelloworldviewuiImplementsviewuiimp{PrivateView Rootview;  PublicTextView Helloworldtv;  PublicEditText Msget; @Override Public voidinitviews (layoutinflater inflater, ViewGroup container) {Rootview= Inflater.inflate (R.layout.activity_main, container,false); HELLOWORLDTV=(TextView) Rootview.findviewbyid (R.id.textview); Msget=(EditText) Rootview.findviewbyid (R.id.edittext); } @Override PublicView Getrootview () {returnRootview; }}

This class is simple enough to initialize views and return to root view. Now it seems that the Findviewbyid method and the logic of view can be done here. So what do we do in the activity's implementation class? What we do is also very simple, just inherit the base class we just wrote, and inject a Helloworldviewui object on it.

 Public classMainactivityextendsbasepresenteractivity<Helloworldviewui>{@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); UiImp.helloWorldTv.setText ("12345"); } @OverrideprotectedClassGetviewuiclass () {return Helloworldviewui.class; }}

We can use this Uiimp object to manipulate the various view definitions defined in the layout, which is really simple and convenient. But here comes the problem again! What we see here is that the UI and activity are actually extracted, but the activity still does the UI logic, and the initialization and definition of the menu is still in the activity, so the view and activity are not fully independent at the moment. For example, the following code is difficult to put into the Uiimp interface.

@Override Public BooleanOncreateoptionsmenu (Menu menu) {//inflate the menu; This adds items to the action bar if it is present.getmenuinflater (). Inflate (R.menu.menu_main, menu); return true; } @Override Public Booleanonoptionsitemselected (MenuItem item) {//Handle Action Bar item clicks here. The Action Bar would//automatically handle clicks on the Home/up button, so long//As you specify a the parent activity in Androidmanifest.xml.        intID =Item.getitemid (); //noinspection simplifiableifstatement        if(id = =r.id.action_settings) {            return true; }        return Super. onoptionsitemselected (item); }

Summing up is this idea is very good, but in the actual use still have certain problems, and later also have to think more research ah.

SOURCE Download:http://download.csdn.net/detail/shark0017/8565091

Reference from:

Https://github.com/wongcain/MVP-Simple-Demo

https://github.com/bboyfeiyu/android-tech-frontier/tree/master/androidweekly/%E4%B8%80%E7%A7%8D%E5%9C% A8android%e4%b8%ad%e5%ae%9e%e7%8e%b0mvp%e6%a8%a1%e5%bc%8f%e7%9a%84%e6%96%b0%e6%80%9d%e8%b7%af

An MVP Model framework for Android

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.