View the usage of ViewStub-view the use of ViewStub from the source code, and view the usage of viewstub
ViewStub is a lightweight View, which exists as a <ViewStub> label in the layout file. This View is not instantiated when acitivity loads the layout, the View is instantiated only when the inflate () method of ViewStub is called in the code. Defining a ViewStubViewStub is a lightweight View with no size or any painting. Therefore, it is very cost-effective to put inflate in the view tree. Every ViewStub needs to define the android: layout attribute to define the layout file for inflate. For example:
<ViewStub
android:id="@+id/stub_import"
android:inflatedId="@+id/panel_import"
android:layout="@layout/progress_overlay"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom" />
Load the layout of ViewStub: When you want to load the layout of ViewStub, you can call setVisibility (View. VISIBLE) or inflate () method. The Code is as follows:
((ViewStub) findViewById(R.id.stub_import)).setVisibility(View.VISIBLE);
// or
View importPanel = ((ViewStub) findViewById(R.id.stub_import)).inflate();
Note: The inflate () method returns the inflated view. So you no longer need to use the findViewById () method. After visibility or inflated is set, the ViewStub element no longer exists in the view tree. It is replaced by the inflated layout, and the id attribute of the inflated root layout is the android: inflatedId attribute value specified by ViewStub. It is worth noting that when the setVisibility () method of ViewStub is called to change its visibility, the layout file specified by ViewStub is also inflated. However, if you call this method once, it will not be inflate. Let's take a look at the source code of the setVisibility () method in ViewStub: @ 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 () ;}} where mInflatedViewRef is a reference, it is a reference of the inflated view. The Code logic above shows that when we call the setVisibility () method for the first time, it first determines whether the reference of the view to be inflate is empty. If it is not empty, it obtains the view and sets its visibility. If w is null, The inflate () method is executed no matter whether the setVisibility () method is visible or invisible.
Let's take a look at the source code of the inflate () method: public View inflate () {final ViewParent viewParent = getParent ();
If (viewParent! = Null & viewParent instanceof ViewGroup) {if (mLayoutResource! = 0) {final ViewGroup parent = (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 viewParent") ;}} here we can see that this method has done the following: 1. obtain the parent control of the current viewStub, which is generally a ViewGroup object. If viewStub is not in a ViewGroup, an exception is reported. 2. Use the LayoutInflater's inflate method to load the layout file and obtain the view corresponding to the layout specified by viewStub. 3. parent. removeViewInLayout (this); Delete the current viewStub4. obtain the layout parameters of the current viewStub and add the inflate view to the parent. 5. Save the inflate view with reference.