Android Memory Optimizer 6-Interface UI optimization (3)

Source: Internet
Author: User

This blog post mainly discusses one of the techniques commonly used in complex interfaces--interface delay loading.

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

Viewstub is a hidden, non-memory-intensive view object that can delay loading a layout resource file at run time. The layout resource file is actually loaded when viewstub is set to 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 you want to load by using the inflated ID property.

Please refer to the code snippet below.

<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"/>

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

The recommended way to load a layout resource file is as follows:

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

Or

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

When inflate () is called, the viewstub is replaced by the loaded view, and the View object is returned. This allows the application to get a reference to the load view without additional execution of Findviewbyid ().

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 is needed. When and when to load, without having to load at the start of the boot. Doing so can speed up application startup and save memory resources.

Optimization Series related blog posts:

Android Development Optimization--memory optimization for bitmap

Android Development optimization-using soft references and weak references

Optimized for Android development – from a code perspective

Android Development Optimization-UI optimization (1)

Android Development Optimization-UI optimization (2)

---------------------------------------------------------------------------

http://blog.csdn.net/arui319

The "Android Application development Solution" has been published, this article is part of the first draft. Welcome to buy Reading.

This article can be reproduced, but please keep the above author information.

Thank you.

---------------------------------------------------------------------------



From for notes (Wiz)

Android Memory Optimizer 6-Interface UI optimization (3)

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.