View the usage of ViewStub-view the use of ViewStub from the source code, and view the usage of viewstub

Source: Internet
Author: User

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.

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.