Analysis of Android Development optimization: An optimization of Interface UI (Iii.) _android

Source: Internet
Author: User

Sometimes, our pages may contain layouts that are hidden by default, and the hidden layout is displayed when a user triggers a certain action. For example, we have an activity to display a list of friends, and when the user clicks "Import" in menu, a layout interface for importing friends is displayed in the current activity. From the requirements point of view, this import function, in general, the user is not used. Most of the time, the layout of the imported friends will not be displayed. This time, you can use the lazy load feature.

Viewstub is a hidden, memory-not-occupied view object that can delay loading a layout resource file at run time. The layout resource file is actually loaded when the viewstub is set to visible, or when the inflate () function is invoked. The viewstub replaces itself in the parent container when the view is loaded. Therefore, viewstub will always be present in the view until the setvisibility (int) or inflate () is called. Viewstub layout parameters are added to the Viewstub parent container along with the number of views that are loaded. Similarly, you can define or rename the ID value of the View object that you want to load by using the inflated id attribute.

Please refer to the code snippet below.

Copy Code code as follows:

<viewstub

Android:id= "@+id/stub_import"

Android:inflatedid= "@+id/panel_import"

android:layout= "@layout/progress_overlay"

Android:layout_width= "Fill_parent"

android:layout_height= "Wrap_content"

android:layout_gravity= "Bottom"/>


A defined Viewstub object can be found by using the id "Stub_import". After loading the layout resource file "Progress_overlay", the Viewstub object is removed from its parent container. You can find a view created by the layout resource "Progress_overlay" through the id "Panel_import".


The recommended way to perform load layout resource files is as follows:

Copy Code code as follows:

((viewstub) Findviewbyid (R.id.stub_import)). Setvisibility (view.visible);

Or

View Importpanel = ((viewstub) Findviewbyid (R.id.stub_import)). Inflate ();


When inflate () is invoked, the viewstub is replaced by the loaded view, and the View object is returned. This allows the application to take no additional findviewbyid () to obtain a reference to the load view.
Copy Code code as follows:

Experience Sharing:

The viewstub can be associated with the layout resource file specified in the XML file, allowing the layout resource file to be loaded again when it needs to be used. When and when to load, not at the start of the time to load. This will speed up the application and save memory resources.


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.