Sometimes, our pages may contain some la S. These la s are hidden by default. After a user triggers some operations, the hidden la s are displayed. For example, an activity is used to display the list of friends. After you click "import" in the menu, a layout interface for importing friends is displayed in the current activity. From the perspective of requirements, this import function is generally not used by users. In most cases, the layout of imported friends is not displayed. In this case, you can use the delayed loading function.
Viewstub is a hidden view object that does not occupy memory space. It can load layout resource files at runtime delay. When viewstub is set to visible or the inflate () function is called, the layout resource file is loaded. This viewstub replaces itself in the parent container when loading the view. Therefore, viewstub will remain in the view until setvisibility (INT) or inflate () is called. The layout parameters of viewstub are added to the parent container of viewstub along with the number of loaded views. Similarly, you can use the inflated ID attribute to define or rename the id value of the view object to be loaded.
See the followingCodeFragment.
Copy code The Code is 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"/>
The ID "stub_import" can be used to find the defined viewstub object. After the layout resource file "progress_overlay" is loaded, the viewstub object is removed from its parent container. You can use the "panel_import" ID to find the view created by the layout resource "progress_overlay.
The recommended method for loading layout resource files is as follows:
Copy code The Code 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, this viewstub is replaced by the loaded view, and this view object is returned. This makes the applicationProgramYou do not need to execute findviewbyid () to obtain reference for loading a view.Copy codeThe Code is as follows:Experience Sharing:
Viewstub can be used to associate with the specified layout resource file in the XML file, so that the layout resource file can be loaded as needed. When to load data, instead of loading data at startup. This can speed up application startup and save memory resources.