[Turn] ViewStub lazy loading for android-performance optimization

This article is excerpted from the "Android Development Authoritative Guide"

      and introduced an <include> tag in section 4.5.6, which can refer to another layout file in the layout file, and can cover all layout-related elements of the root node of the referenced layout file. Attributes, that is, attributes starting with android:layout. Through the <include> tag, a very large layout file can be decomposed into several smaller layout files, and these small layout files can also be referenced multiple times, so as to achieve a purpose of reuse.

     The <include> tag is very useful, but there is a problem that all the controls in the layout file are not necessarily used when the program starts, and some controls are only used in specific situations. For example, a software for reading books only needs to display a progress bar when downloading e-books . When reading books at ordinary times , they are loaded with local e-books and do not need to use a progress bar. Therefore, the progress bar can not be loaded at all when the program starts. However, when using the <include> tag to refer to the layout file containing the progress bar, all the controls are loaded into the memory regardless of the situation. Some readers may say that a progress bar does not take up much system resources, and it does not matter if it is loaded. These readers may be right, but if you load not a progress bar, but a lot of ImageView controls (showing large images), and not in one place, then I'm afraid it will consume the poor phone resources. . Therefore, we desperately need a mechanism to change this behavior of the <include> tag to only load controls when needed. This mechanism is the ViewStub control introduced in this section.

ViewStub is an invisible control, and its function is basically the same as the <include> tag. Use the <ViewStub> tag in the layout file to refer to other layout files. But the only difference from <include> is that ViewStub does not immediately load the referenced layout file. ViewStub will load the referenced controls only after the ViewStub.inflate or ViewStub.setVisibility(View.VISIBLE) methods are called. Let's look at the two layout files first.

main.xml

<?xml version="1.0"encoding="utf-8"?>

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"

   android:orientation="vertical"android:layout_width="fill_parent"

   android:layout_height="fill_parent">

   <Button android:layout_width="fill_parent"

       android:layout_height="wrap_content" android:text="My button"

       android:onClick="onClick_Button" />

    <includelayout="@layout/custom" />

</LinearLayout>

custom.xml

<?xml version="1.0"encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

          android:orientation="vertical"android:layout_width="fill_parent"

          android:layout_height="fill_parent">

              <Buttonandroid:layout_width="fill_parent"

                      android:layout_height="wrap_content" an  droid:text="按钮1" />

              <Buttonandroid:layout_width="fill_parent"

                      android:layout_height="wrap_content" android:text="按钮2" />

</LinearLayout>

       The <include> tag is used in the main.xml file to reference custom.xml, in this case the three buttons shown in Figure 5.56 are immediately displayed on the screen. If you replace the <include> tag with the following code, when the program starts, only the definition button in the main.xml file will be displayed, as shown in Figure 5.57.

<ViewStub android:id="@+id/viewstub"android:inflatedId="@+id/button_layout"

   android:layout="@layout/custom"android:layout_width="fill_parent"

   android:layout_height="wrap_content"  />
\"><br> Figure 5.56 Loading controls using <include> tag <br><br><img style=

    }

}

After clicking "My Button", the two buttons defined in the custom.xml file will be displayed, and the effect is exactly the same as Figure 5.56.

   Note: Like the <include> tag, <ViewStub> can also set all layout-related properties of the root node in the referenced layout file. The difference is that the android:id attribute of the <include> tag directly overrides the android:id attribute value of the root node in the referenced layout file, while the android:id attribute of the <ViewStub> tag is the same as the android:id attribute of the ordinary control tag. Used to reference controls in code. In the <ViewStub> tag, you need to use the android:inflatedId attribute to override the android:id attribute value of the root node in the referenced layout file. Although <ViewStub> can completely replace <include>, the only drawback is that <ViewStub> cannot replace <merge> yet.

 

 

Reprinted from: http://www.2cto.com/kf/201110/108620.html

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326684839&siteId=291194637