Analysis of Android Development Optimization: detailed explanation of interface UI optimization (3)

Sometimes, our page may contain some layouts, which are hidden by default. The hidden layouts will not be displayed until the user triggers a certain operation. For example, we have an activity to display the list of friends. When the user clicks "import" in the menu, a layout interface for importing friends will be displayed in the current activity. From the perspective of requirements, this import function is generally not used by users. That is, most of the time, the layout of imported friends will not be displayed. At this time, you can use the function of delayed loading.

Viewstub is a hidden view object that does not occupy memory space. It can delay the loading of layout resource files at run time. Only when viewstub is set to visible or the inflate () function is called will the layout resource file be loaded. The viewstub replaces itself in the parent container when the view is loaded. Therefore, the viewstub will remain 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 define or rename the ID value of the view object to be loaded by using the inflated ID attribute.

Please refer to the following code snippet.

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

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>