Layout optimization using viewstub in Android
In Android development, view is a technology we must contact to display Generally, as the view becomes more and more complex, the performance of the overall layout will decline Here is a view to improve layout performance in some scenarios, which is viewstub
What is viewstub
Note: explanation of stub
A stub is a small program routine that substitutes for a longer program,possibly to be loaded later or that is located remotely
In Java, a stake is a code used to replace associated code or unimplemented code
Viewstub usage scenario
As shown in the figure above,
Therefore, viewstub is useful at this time Use viewstub to delay loading layout resources
How do I use viewstub
Use viewstub labels in layout files
2. Insert the layout in the code
About viewstub
In addition to the inflate method, we can also call the setvisibility () method to load the layout file. Once the layout is loaded, viewstub will delete Android: ID from the current layout level. Specify the viewstub ID, which is used to find the resource ID of the viewstub for delayed loading. Android: layout delayed loading layout. Android: inflated id the ID of the loaded layout, which is the ID of the relativelayout
Insufficient of viewstub
There is such a description in the official document
Note: One drawback of ViewStub is that it doesn't currently support the tag in the layouts to be inflated.
This means that viewstub does not support < merge > tags
To the extent that < merge > tags are not supported, let's make a simple verification
Verification 1: direct label
As follows, we have a layout file named merge_ layout. xml
After replacing the Android: layout attribute value of the corresponding viewstub, the following crash occurs after running (click the button button)
E AndroidRuntime: android. view. InflateException: Binary XML file line #1: <merge /> can be used only with a valid ViewGroup root and attachToRoot=true E AndroidRuntime: at android. view. LayoutInflater. inflate(LayoutInflater.java:551) E AndroidRuntime: at android. view. LayoutInflater. inflate(LayoutInflater.java:429) E AndroidRuntime: at android. view. ViewStub. inflate(ViewStub.java:259) E AndroidRuntime: at com. droidyue. viewstubsample. MainActivity$1. onClick(MainActivity.java:20) E AndroidRuntime: at android. view. View. performClick(View.java:5697) E AndroidRuntime: at android. widget. TextView. performClick(TextView.java:10815) E AndroidRuntime: at android. view. View$PerformClick. run(View.java:22526) E AndroidRuntime: at android. os. Handler. handleCallback(Handler.java:739) E AndroidRuntime: at android. os. Handler. dispatchMessage(Handler.java:95) E AndroidRuntime: at android. os. Looper. loop(Looper.java:158) E AndroidRuntime: at android. app. ActivityThread. main(ActivityThread.java:7237) E AndroidRuntime: at java. lang.reflect. Method. invoke(Native Method) E AndroidRuntime: at com. android. internal. os. ZygoteInit$MethodAndArgsCaller. run(ZygoteInit.java:1230) E AndroidRuntime: at com. android. internal. os. ZygoteInit. main(ZygoteInit.java:1120) E AndroidRuntime: Caused by: android. view. InflateException: <merge /> can be used only with a valid ViewGroup root and attachToRoot=true E AndroidRuntime: at android. view. LayoutInflater. inflate(LayoutInflater.java:491) E AndroidRuntime: ... 13 more
It can be seen that viewstub does not support direct < merge > tags
Validate the second indirect viewstub
The following layout uses the merge tag indirectly The file name is include_ merge. xml
Then modify the Android: layout value of viewstub, run it, and everything is normal
In addition, this example also verifies that viewstub also supports < include > tags well
Code analysis of viewstub
inflate vs setVisibility
What inflate and setvisibility have in common is that they can load layouts
Setvisibility only calls the inflate method when the viewstub delays initialization for the first time and the visibility is non gone
Inflate source code
We will understand it better by reading the inflate method implementation below
This is the research on viewstub. I hope it will be helpful and enlightening for everyone to optimize the view