Android performance optimization 3 layout optimization use of ViewStub labels

Source: Internet
Author: User

Android performance optimization 3 layout optimization use of ViewStub labels


The story of Xiao Hei and Xiao Bai learns the use of ViewStub through the form of a question and answer from the two figures.


Tom: Hi, Tom. What is ViewStub? I heard that it can be used for layout optimization. Small Black: ViewStub is a hidden view object that does not occupy memory space. It can delay loading layout resource files during runtime. (For more details about APIs and other information, see the official document ViewStub.) The computer industry has always been a matter of fact. The following is an example to demonstrate the effect.

Xiao Hei: Let's talk about the concept just for a general understanding, or use an instance to demonstrate it. First, create a layout file used in the Activity. The file name is activity_main.xml.
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "horizontal"> <Button android: id = "@ + id/show_button" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "display"/> <ViewStub android: id = "@ + id/viewstub" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout = "@ layout/sub_layout"/> <Button android: id = "@ + id/hide_button" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "hide"/> </LinearLayout>

White: the "show" and "hide" strings are not put into values/string. xml, and the control IDs are not messy with certain naming rules.
Black :...... "Are you a rescue engineer from a monkey ?", Everything should be simplified. Note the usage of ViewStub above. android: layout = "@ layout/sub_layout" introduces a new layout. The Code of sub_layout.xml is as follows:
<TextView xmlns: android = "http://schemas.android.com/apk/res/android" android: id = "@ + id/textview" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "TextVeiw included in ViewStub"/>

Now there are two layout files: activity_main.xml required by Activity setContentView and sub_layout introduced in Activity setContentView.
Xiao: Below is a MainActivity. java. Add some click events.
package com.example.viewstub;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.ViewStub;import android.view.View.OnClickListener;public class MainActivity extends Activity {     @Override     protected void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          setContentView(R.layout.activity_main);                   final ViewStub viewStub = (ViewStub) findViewById(R.id.viewstub);                   View showButton = findViewById(R.id.show_button);          showButton.setOnClickListener(new OnClickListener() {                             @Override               public void onClick(View v) {                    viewStub.inflate();               }          });                   View hideButton = findViewById(R.id.hide_button);          hideButton.setOnClickListener(new OnClickListener() {                             @Override               public void onClick(View v) {                    viewStub.setVisibility(View.GONE);               }          });     }}


: After the code is run, the "DDMS-> Dump View Hierarchy for UI Automator" tool is used without clicking the Show button by default.


TIPS: I found that the layout file activity_main.xml is "displayed" and "hidden" has one <ViewStub> between the two buttons, it indicates that ViewStub is not added to the view Tree (Android UI Tree) during initialization and loading ). I also use the "Hierarchy View" tool as follows:




Xiao: Yes. This is where ViewStub can optimize its layout. "lazy loading View" is used. during initialization, the system does not initialize the view referenced by ViewStub. Next, let's take a look at the "show" button.



Small Black: ViewStub use flow is 1. Add ViewStub in layout (add in XML, add in code) 2. inflate show 3. setVisibility hide.
TIPS: the layout of viewStub. inflate (); ViewStub reference is displayed in the code. But isn't that about adding a view dynamically? What is the difference with View. setVisibility (View. GONE?
: ViewStub is a lightweight view that has no size and does not nest or render anything in the layout. Therefore, the cost of displaying or hiding a view is very small. The layout resource file is loaded only when ViewStub is visible or the inflate () function is called. 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 inflatedId attribute to define or rename the Id value of the view object to be loaded. View. setVisibility (View. GONE); the method is added to the View Tree (Android UI Tree) during initialization, but ViewStub is not added during initialization.
TIPS: Do you need to pay attention to using ViewStub? Small Black: 1. The <merge/> label is not supported in the layout to be rendered. 2. The ViewStub. infalte method cannot be called twice. Otherwise, the following exception occurs:
java.lang.IllegalStateException: ViewStub must have a non-null ViewGroup viewParent     at android.view.ViewStub.inflate(ViewStub.java:287)     at com.example.viewstub.MainActivity$1.onClick(MainActivity.java:23)     at android.view.View.performClick(View.java:4475)     at android.view.View$PerformClick.run(View.java:18786)     at android.os.Handler.handleCallback(Handler.java:730)     at android.os.Handler.dispatchMessage(Handler.java:92)     at android.os.Looper.loop(Looper.java:176)     at android.app.ActivityThread.main(ActivityThread.java:5419)     at java.lang.reflect.Method.invokeNative(Native Method)     at java.lang.reflect.Method.invoke(Method.java:525)     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)     at dalvik.system.NativeStart.main(Native Method)

Xiao Bai: the materials I read on the Internet say that ViewStub's disadvantage is that the introduced layout cannot be a separate view, but must be a layout. Is that true? Xiao: This is incorrect. In the example of sub_layout.xml, there is only one TextView.
Tom: Why does ViewStub work as a layout label? : You can check the source code of ViewStub. public final class ViewStub extends View is a View subclass.
TIPS: I have several questions: 1. Can views added by ViewStub be dynamically deleted? 2. Why can't I use inflate for the second time? 3. Although you cannot directly inflate the second time, you can directly delete it. Do you want to add a View to the code? 4. Can the inflate method be displayed in other ways? (Locate id and set visible)
Tom: I don't know...

ReferencesViewStub-official documentation Loading Views On Demand-official tutorial Android Layout Tricks #3: Optimize with stubs-official blog Android Study Notes 31: optimize layout dynamic loading performance by using ViewStub of the inertia control

For more Optimization-related articles, see section 3 Performance Optimization in summary of Android basic learning articles.


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.