Android Performance Optimization three-layout optimization viewstub label usage

Source: Internet
Author: User


Small black and small white story, through the virtual two characters to carry out a question and answer form to jointly learn the use of viewstub


Little white: Hi, Little black, what is viewstub? I hear it can be used for layout optimization. Black: Viewstub is a hidden, memory-not-occupied view object that can delay loading layout resource files at run time. (More detailed API and other information can view the official document Viewstub), the computer industry has always been the practice of knowledge, the following example shows the effect.

Little black: Say the concept is only for general understanding, or use an example to demonstrate under. To create a layout file used in the activity, the file name is: Activity_main.xml
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:layout_width=" match_parent "    android:layout_height=" Match_parent "    android:orientation= "Horizontal" >    <button        android:id= "@+id/show_button"        android:layout_ Width= "Wrap_content"        android:layout_height= "wrap_content"        android:text= "show"/>       <viewstub        android:id= "@+id/viewstub"        android:layout_width= "wrap_content"        android:layout_height= "Wrap_ Content "        android:layout=" @layout/sub_layout "        />       <button        android:id=" @+id/hide_button "        android:layout_width=" wrap_content "        android:layout_height=" wrap_content "        android:text=" hidden "/ >   </LinearLayout>

Small white: The "show", "hidden" string is not put into the values/string.xml, the control's ID is not a certain naming rules of the chaos.
Little black: ... "Are you the monkey's reinforcements?" he asked. "Well, basics. Notice the usage of the above viewstub, where android:layout= "@layout/sub_layout" introduces a new layout, the Sub_layout.xml code is as follows:
<textview  xmlns:android= "http://schemas.android.com/apk/res/android"    android:id= "@+id/textview"     android:layout_width= "wrap_content"     android:layout_height= "wrap_content"     android:text= " Textveiw "/> Included in the Viewstub

Small white: There are now two layout files, one is the activity Setcontentview need activity_main.xml, one is the introduction of Sub_layout
Little black: Here is a Mainactivity.java, add some click events
Package Com.example.viewstub;import Android.app.activity;import Android.os.bundle;import android.view.View;import      Android.view.viewstub;import Android.view.view.onclicklistener;public class Mainactivity extends Activity {@Override          protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);                   Setcontentview (R.layout.activity_main);                   Final Viewstub viewstub = (viewstub) Findviewbyid (r.id.viewstub);          View Showbutton = Findviewbyid (R.id.show_button); Showbutton.setonclicklistener (New Onclicklistener () {@Override public void Oncl               Ick (View v) {viewstub.inflate ();                   }          });          View Hidebutton = Findviewbyid (R.id.hide_button); Hidebutton.setonclicklistener (New Onclicklistener () {@Override public void Oncl Ick (View v) {Viewstub.setvisiBility (View.gone);     }          }); }}


Black: The following is the code after the run, the default does not click the Display button, using the "DDMS-and-Dump View Hierarchy for UI Automator" tool


Small white: I found that the Activity_main.xml layout file has a <ViewStub> between "show", "Hide" two buttons and not in the above, stating that Viewstub is not added to the view tree when initializing the load (Android UI Tree). I also use the "Hierarchy View" tool as follows:




Black: Yes, this is where the viewstub can be optimized for layout "lazy load View", when initialized the system does not initialize the viewstub referenced view. Look again and then click on the "Show" button.



Small black: The viewstub use process is 1. Add Viewstub (XML Add, add in code) 2 in the layout. Inflate display 3. Setvisibility hidden.
Small white: by Viewstub.inflate () in the Code; The layout of the Viewstub reference is displayed. But isn't that a dynamic addition to the view? and view.setvisibility (View.gone); What's the difference?
Black: Viewstub is a lightweight view with no size and no nesting or rendering of anything in the layout. So the cost of showing or hiding it at the view level is very small. This layout resource file is loaded when viewstub is visible, or when the inflate () function is called. The viewstub replaces itself in the parent container when the view is loaded. Therefore, viewstub is always present in the view until setvisibility (int) or inflate () is called. The layout parameters of the viewstub are added to the Viewstub parent container along with the number of views loaded.    Similarly, you can also define or rename the ID value of the View object to be loaded by using the Inflatedid property. View.setvisibility (View.gone); The method is added to the view tree when it is initialized (Android UI tree), and is not added when initialized with Viewstub.
Little white: Do you need to pay attention to using viewstub? Little black: 1. <merge/> tags are not supported in the layout you want to render. 2. The Viewstub.infalte method cannot be called two times and the following exception occurs:
java.lang.IllegalState     Exception:viewstub must has a non-null viewgroup viewparent at Android.view.ViewStub.inflate (viewstub.java:287) At Com.example.viewstub.mainactivity$1.onclick (mainactivity.java:23) at Android.view.View.performClick (View.java : 4475) at Android.view.view$performclick.run (view.java:18786) at Android.os.Handler.handleCallback (handler.java:73 0) at Android.os.Handler.dispatchMessage (handler.java:92) at Android.os.Looper.loop (looper.java:176) at Androi D.app.activitythread.main (activitythread.java:5419) at java.lang.reflect.Method.invokeNative (Native Method) at JAV A.lang.reflect.method.invoke (method.java:525) at Com.android.internal.os.zygoteinit$methodandargscaller.run ( zygoteinit.java:1209) at Com.android.internal.os.ZygoteInit.main (zygoteinit.java:1025) at Dalvik.system.NativeStar T.main (Native Method) 

Small white: On the Internet to read the information that is viewstub a disadvantage is that the introduction of the layout can not make a separate view, and must be a layout before the line, is it? Black: This view is wrong and there is only one textview view in the example Sub_layout.xml.
Small white: Why Viewstub can be used as a layout label? Little black: You can check the source code of Viewstub, public final class Viewstub extends view, viewstub itself is a view sub-class.
Little white: I have a few questions: 1. Can the views added viewstub be deleted dynamically? 2. Can't inflate the second time why? 3. Although the second time cannot be directly inflate, can be deleted directly, add view in code? 4. Can it be displayed in other ways besides calling the Inflate method? (Find ID settings visible)
Little black: I don't know ...

ReferencesViewstub-Official document loading views on Demand-Official tutorial Android Layout Tricks #3: Optimize with stubs-Official blog Android Learning Note 31: Using Lazy controls views Tub layout optimization for dynamic load performance optimization

For more optimization related articles see: "Android Basic Learning Article Summary" Part III performance optimization


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.