New skill Get:android "delayed loading" for application of optimization skills

Source: Internet
Author: User

Deteriorating, anymore.

Now the Sihai seems to be peaceful, but it is hidden in the murderous. Big to a plane a bus, small to an app, will make you mad.

More and more complex interface, more and more demand, for the app itself is a bad news, but in human infinite imagination, in the function module relentless accumulation, for the application, either thin, or die!

So how thin, how thin? For the general beauty of programmers and products and users, only simple, on demand is the king.

Narrator: Write articles on the article, too much nonsense!

"This is the point" above a bunch of crap actually there are two useful words, simple and on-demand . Simplicity is the design to think, then on-demand is what our program will do.

"On Demand: Lazy loading" The so-called lazy loading is not in the initialization of the interface to load part of the view, when needed to load, in the layout of multiple controls can effectively improve the interface loading and response speed.

"We are generally using on-demand loading" Some of the more experienced Android developers will understand that some of the modules in the complex interface are written separately in an XML-written layout, and then inflate () into the program when needed to achieve the purpose of lazy loading. Doing so is very convenient when the interface is not deterministic, it needs to instantiate the view multiple times, or the view is generated based on the data traversal, which is completely implemented by Java code. So what if we have a more intuitive approach when the interface element is determined and requires a delay load? Of course: Use Viewstub.

"A Lightweight control: Viewstub" as the meaning of the name, just a view of the stub, not what memory (different from <include>), but can occupy a position, when the program needs to Viewstub point to the view extension to the interface. By convention, the code will sneak up on me when I'm going to give an example--"

1<scrollview xmlns:android= "Http://schemas.android.com/apk/res/android"2Xmlns:tools= "Http://schemas.android.com/tools"3Android:layout_width= "Match_parent"4android:layout_height= "Match_parent"5Tools:context= ". Mainactivity ">6 7<LinearLayout8Android:id= "@+id/main"9Android:layout_width= "Match_parent"Tenandroid:layout_height= "Wrap_content" Oneandroid:orientation= "Vertical" Aandroid:paddingbottom= "@dimen/activity_vertical_margin" -android:paddingleft= "@dimen/activity_horizontal_margin" -android:paddingright= "@dimen/activity_horizontal_margin" theandroid:paddingtop= "@dimen/activity_vertical_margin" > -  -<TextView -Android:layout_width= "Wrap_content" +android:layout_height= "Wrap_content" -Android:text= "The so-called delay loading is not in the initialization of the interface load part of the view, when needed to load, in the layout of multiple controls can effectively improve the interface response speed. "/> +  A<Button atAndroid:id= "@+id/btn_by_inflate" -Android:layout_width= "Match_parent" -android:layout_height= "Wrap_content" -android:text= "Common inflate Way"/> -  -<Button inAndroid:id= "@+id/btn_by_viewstub" -Android:layout_width= "Match_parent" toandroid:layout_height= "Wrap_content" +android:text= "Viewstub Way"/> -  the         <viewstub35 android:id= "@+id/viewstub" android:layout_width= "Match_parent" 37 android:layout_height= "Wrap_content" android:inflatedid= "@+id/inflate_view_id" android:layout= "@layout/delay_by_viewstub"/> +</LinearLayout> A  the</ScrollView>

This is the viewstub defined in our main interface XML, there are three points to note: The ID of id->stub, according to this stub object;inflatedid-> the view ID of the extension (that is, the ID of the point of view can not be defined); layout- > is going to inflate layout. Ok, too intuitive, no more explaining.

With a stub, how do we get it when the program needs it? In a few words, the view is easy to get.

1  PackageCom.change.delayloaddemo;2 3 Importandroid.app.Activity;4 ImportAndroid.graphics.Color;5 ImportAndroid.os.Bundle;6 ImportAndroid.view.Menu;7 ImportAndroid.view.View;8 ImportAndroid.view.View.OnClickListener;9 Importandroid.view.ViewStub;Ten ImportAndroid.widget.Button; One Importandroid.widget.LinearLayout; A ImportAndroid.widget.TextView; -  - /** the * Two lazy loading modes demo Android is recommended viewstub mode.  -  *  -  * @author Change -  *  +  */ -  Public classMainactivityextendsActivityImplementsOnclicklistener { +     PrivateLinearLayout main; A     PrivateTextView normalinflate, delaybyviewstub; at     Privateviewstub mstub; -     PrivateButton byinflate, byviewstub; -  - @Override -     protected voidonCreate (Bundle savedinstancestate) { -         Super. OnCreate (savedinstancestate); in Setcontentview (r.layout.activity_main); - initviews (); to     } +      -     Private voidinitviews () { theMain =(LinearLayout) Findviewbyid (r.id.main); *Byinflate =(Button) Findviewbyid (r.id.btn_by_inflate); $Byviewstub =(Button) Findviewbyid (r.id.btn_by_viewstub);Panax Notoginseng          -Byinflate.setonclicklistener ( This); theByviewstub.setonclicklistener ( This); +     } A  the @Override +      Public BooleanOncreateoptionsmenu (Menu menu) { -         //inflate the menu; This adds items to the action bar if it is present. $ getmenuinflater (). Inflate (R.menu.main, menu); $         return true; -     } -  the @Override -      Public voidOnClick (View arg0) {Wuyi         Switch(Arg0.getid ()) { the          Caser.id.btn_by_inflate: -Normalinflate =(TextView) Getlayoutinflater (). Inflate ( WuR.layout.delay_by_viewstub,NULL); - Main.addview (normalinflate); AboutNormalinflate.settext ("I am the usual inflate method to load the view"); $Normalinflate.setbackgroundcolor (Color.parsecolor ("#00ffff")); -              Break; -          Caser.id.btn_by_viewstub: -            mstub = (viewstub) Findviewbyid (r.id.viewstub); if (null!=mstub)//mstub executes a inflate () method, it is empty, so 62 Delaybyviewstub = (TextView) mstub.inflate ();//Mstub.setvisibility (view.visible);//or it can inflate out View. -              Break; $  the         default: the              Break; the         } the     } -  in}

The red section above is a few lines of code that are loaded using viewstub delay.

"points to note" viewstub will be empty after executing the inflate () method or the Setvisibility (view.visible) method, and the object can no longer be used.

"Full Code"Demo's github address:Https://github.com/ChangeWu/SomePoject/tree/master/DelayLoadDemo

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.