In Android development, view is the technology that we have to touch to show. Typically, as the view becomes more complex, the performance of the overall layout decreases. Here's a view that promotes layout performance in some scenarios, which is viewstub.
What's viewstub?
- Viewstub is a subclass of view
- It is not visible, the size is 0
- Used to defer loading of layout resources
Note, the explanation of the stub
A stub is a small program routine which substitutes for a longer program, possibly to was loaded later or is located re motely
In Java, a pile is code that replaces the associated code or the code that is not implemented.
Viewstub Use scenes
As shown in the figure above,
- A ListView contains items such as news, business, technology, etc.
- Each item contains its own corresponding sub topic,
- But the view (blue area) of the sub topic only really needs to be loaded if you click the expand button.
- If you load the view of the sub topic by default, it can cause memory usage and CPU consumption.
So it's viewstub to be useful. Use Viewstub to defer loading of layout resources.
Viewstub How to use
Using the viewstub tag in a layout file
<?xml version= "1.0" encoding= "Utf-8"?> <relativelayout 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:paddingleft= "@dimen/activity_horizontal_margin" android:paddingright= "@dimen/activity_ Horizontal_margin "android:paddingtop=" @dimen/activity_vertical_margin "android:paddingbottom=" @dimen/activity_ Vertical_margin "tools:context=" com.droidyue.viewstubsample.MainActivity "> <button android:id=" @+id/clickme
"Android:text=" Hello world! " Android:layout_width= "Wrap_content" android:layout_height= "wrap_content"/> <viewstub android:id= "@+id/myVie" Wstub "android:inflatedid=" @+id/myinflatedviewid "android:layout=" @layout/include_merge "android:layout_width=" WR
Ap_content "android:layout_height=" wrap_content "android:layout_below=" @id/clickme "/> </RelativeLayout>
2. Inflate layout in code
Viewstub myviewstub = (viewstub) Findviewbyid (r.id.myviewstub);
if (myviewstub!= null) {
myviewstub.inflate ();
Or in the following form load
//myviewstub.setvisibility (view.visible);
}
It's 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 is removed from the current layout level
ANDROID:ID Specifies the Viewstub ID, which is used to find viewstub for deferred loading
Android:layout resource ID for deferred load layout
Android:inflatedid loaded layout is overridden by ID, here for relativelayout ID
The shortage of viewstub
There is a description in the official document
Note:one drawback of the viewstub is this it doesn ' t currently support the tag in the layouts to be inflated.
The viewstub does not support <merge> tags.
About the extent of not supporting <merge> labels, we do a simple validation
Verification One: Direct label
As follows, we have the layout file named Merge_layout.xml
<merge xmlns:android= "Http://schemas.android.com/apk/res/android" >
<button
android:layout_ Width= "Fill_parent"
android:layout_height= "wrap_content" android:text=
"Yes"/>
<button
Android:layout_width= "Fill_parent"
android:layout_height= "wrap_content"
android:text= "No"/>
</merge>
After replacing the corresponding viewstub's Android:layout attribute value, the run (Click the button button) gets the following crash
E AndroidRuntime:android.view.InflateException:Binary XML file line #1: <merge/> Can is used only with a valid Vi Ewgroup 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 a nd attachtoroot=true
E androidruntime:at android.view.LayoutInflater.inflate (layoutinflater.java:491)
E androidruntime: ... More
Visible, direct <merge> tag, viewstub is not supported.
Verifying the viewstub of two indirect
The following layout uses the merge label indirectly. File name is Include_merge.xml
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
android:orientation=" vertical "
android:layout_width=" match_parent "
android:layout_" height= "Match_parent" >
<include layout= "@layout/merge_layout"/>
</LinearLayout>
Then modify the value of the Viewstub android:layout, run, everything normal.
In addition, this example also verifies that Viewstub is also good for <include> label support.
A bit of code analysis about Viewstub
Inflate vs setvisibility
What inflate and setvisibility have in common is that they can load the layout
/**
* When visibility are set to {@link #VISIBLE} or {@link #INVISIBLE},
* {@link #inflate ()} are invoked and this S Tubbedview is replaced into its parent
* by the inflated layout resource.
*
* @param visibility one of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
*
* @see #inflate ()
/
@Override public
void setvisibility (int visibility) {
if ( Minflatedviewref!= null) {
View view = Minflatedviewref.get ();
if (view!= null) {
view.setvisibility (visibility);
} else {
throw new IllegalStateException (" Setvisibility called on Un-referenced View ");
}
else {
super.setvisibility (visibility);
if (visibility = = VISIBLE | | visibility = = invisible) {
inflate ();
}}
}
Setvisibility The Inflate method is invoked only when Viewstub is first delayed initialization and visibility is GONE.
Inflate source
By reading the inflate method below, we will be more understanding
- The purpose of the Android:inflatedid
- viewstub removes the
- viewstub layoutparameters application
- MINFLA after initialization from the view level Tedviewref establishes a connection between viewstub and loaded view by means of a weakly referenced form.
/** * Inflates the layout resource identified by {@link #getLayoutResource ()} * and replaces-Stubbedview in its
Parent by the inflated layout resource.
* * @return the inflated layout resource.
* */Public View inflate () {final viewparent viewparent = GetParent (); if (viewparent!= null && viewparent instanceof ViewGroup) {if (mlayoutresource!= 0) {Final ViewGroup pa
Rent = (viewgroup) viewparent;
Final Layoutinflater factory = Layoutinflater.from (Mcontext);
Final View view = Factory.inflate (Mlayoutresource, parent, false);
if (Minflatedid!= no_id) {View.setid (minflatedid);
Final int index = Parent.indexofchild (this);
Parent.removeviewinlayout (this);
Final Viewgroup.layoutparams layoutparams = Getlayoutparams ();
if (layoutparams!= null) {Parent.addview (view, index, layoutparams);
else {Parent.addview (view, index); } minflatedviewref = new weakreference<view> (vieW);
if (Minflatelistener!= null) {minflatelistener.oninflate (this, view);
} return view;
else {throw new IllegalArgumentException ("Viewstub must have a valid Layoutresource");
} else {throw new IllegalStateException ("Viewstub must have a non-null viewgroup");
}
}
The
Research on Viewstub is one of those things that I hope will help and inspire you about the optimization view.