自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

基于Android 8.0分析源碼的ViewStub源碼解析

移動開發(fā) Android
ViewStub是一種不可見的并且大小為0的試圖,它可以延遲到運行時才填充inflate布局資源,當(dāng)Viewstub設(shè)為可見或者是inflate的時候,就會填充布局資源,這個布局和普通的試圖就基本上沒有任何區(qū)別,比如說,加載網(wǎng)絡(luò)失敗,或者是一個比較消耗性能的功能,需要用戶去點擊才可以加載!從而這樣更加的節(jié)約了性能。對安卓布局很友好!

源碼基于安卓8.0分析結(jié)果

ViewStub是一種不可見的并且大小為0的試圖,它可以延遲到運行時才填充inflate 布局資源,當(dāng)Viewstub設(shè)為可見或者是inflate的時候,就會填充布局資源,這個布局和普通的試圖就基本上沒有任何區(qū)別,比如說,加載網(wǎng)絡(luò)失敗,或者是一個比較消耗性能的功能,需要用戶去點擊才可以加載!從而這樣更加的節(jié)約了性能。對安卓布局很友好!

ViewStub用法

  1. <ViewStub 
  2.     android:padding="10dp" 
  3.     android:background="@color/colorPrimary" 
  4.     android:layout_gravity="center" 
  5.     android:inflatedId="@+id/view_stub_inflateid" 
  6.     android:id="@+id/view_stub" 
  7.     android:layout="@layout/view_stub_imageview" 
  8.     android:layout_width="wrap_content" 
  9.     android:layout_height="wrap_content" /> 

這篇文章安卓代碼、圖片、布局、網(wǎng)絡(luò)和電量優(yōu)化說如果這個根布局是個View,比如說是個ImagView,那么找出來的id為null,得必須注意這一點 -----2018.6.7修正這個說法,以前我說的是錯誤的,根本上的原因是ViewStub設(shè)置了 inflateid ,這才是更本身的原因

在這里記住一點,如果在 ViewStub標(biāo)簽中設(shè)置了android:inflatedId="@+id/view_stub_inflateid",在layout布局中的根布局在設(shè)置android:id="@+id/view_stub_layout",這個id永遠找出來都是為null的,原因會在下面說明

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:padding="10dp" 
  4.     android:id="@+id/view_stub_layout" 
  5.     android:src="@drawable/ic_launcher_background" 
  6.     android:layout_width="match_parent" 
  7.     android:layout_height="match_parent"
  8.     <TextView 
  9.         android:text="如果這個根布局是個View,比如說是個ImagView,那么找出來的id為null,得必須注意這一點  -----2018.6.7修正這個說法,以前我說的是錯誤的,根本上的原因是ViewStub設(shè)置了 inflateid ,這才是更本身的原因" 
  10.         android:layout_width="wrap_content" 
  11.         android:layout_height="wrap_content" /> 
  12.     <ImageView 
  13.         android:layout_marginTop="20dp" 
  14.         android:id="@+id/imageview" 
  15.         android:padding="10dp" 
  16.         android:src="@drawable/ic_launcher_background" 
  17.         android:layout_width="match_parent" 
  18.         android:layout_height="match_parent"/> 
  19. </FrameLayout> 

在activity或者是fragment中的使用,mViewStub.getParent()==null就是說明沒有被填充,需要填充,如果填充了,那么它的parent不會為null,具體的騷操作,后續(xù)我介紹View的繪制流程的時候在詳細說明。

第一種使用的方法

  1.  mViewStub = findViewById(R.id.view_stub); 
  2.  if (null!=mViewStub.getParent()){ 
  3.  View inflate = mViewStub.inflate(); 
  4.  .... 

第二種方式:mViewStub.setVisibility(View.VISIBLE);和inflate()方法一樣。

  1.  mViewStub = findViewById(R.id.view_stub); 
  2.  if (null!=mViewStub.getParent()){ 
  3.    mViewStub.setVisibility(View.VISIBLE); 
  4.  .... 

第三種方式,my_title_parent_id是layout的根布局的id

  1. mViewStub = findViewById(R.id.view_stub); 
  2.  // 成員變量commLv2為空則代表未加載 commLv2 的id為ViewStub中的根布局的id 
  3.  View commLv2=findViewById(R.id.my_title_parent_id); 
  4. if ( commLv2 == null ) { 
  5.    // 加載評論列表布局, 并且獲取評論ListView,inflate函數(shù)直接返回ListView對象 
  6.      commLv2 = (View)mViewStub.inflate(); 
  7.    } else { 
  8.       // ViewStub已經(jīng)加載 
  9.   } 

ViewStub構(gòu)造方法,注意獲取了幾個值mInflatedId就是android:inflatedId="@+id/find_view_stub"這個值, mLayoutResource就是layout的resId,ViewStub的 mIDid。可以看出ViewStub是View的子類.

  1. public final class ViewStub extends View { 
  2.  public ViewStub(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 
  3.         super(context); 
  4.         final TypedArray a = context.obtainStyledAttributes(attrs, 
  5.                 R.styleable.ViewStub, defStyleAttr, defStyleRes); 
  6.         // TODO: 2018/5/23  ViewStub 中設(shè)置的標(biāo)簽id 如果設(shè)置了 這里就一定有值 mInflatedId!=NO_Id 
  7.         mInflatedId = a.getResourceId(R.styleable.ViewStub_inflatedId, NO_ID); 
  8.         mLayoutResource = a.getResourceId(R.styleable.ViewStub_layout, 0); 
  9.         mID = a.getResourceId(R.styleable.ViewStub_id, NO_ID); 
  10.         a.recycle(); 
  11.         //不可見 
  12.         setVisibility(GONE); 
  13.         // 設(shè)置不繪制 
  14.         setWillNotDraw(true); 
  15.     } 

在構(gòu)造方法中:同時注意不可見 setVisibility(GONE); ,設(shè)置不繪制setWillNotDraw(true);,同時通過下面的方法看出,ViewStub 是一個大小為0的視圖。

  1. @Override 
  2.   protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
  3.       // 寬高都為0   onMeasure的時候 寬高都為0 
  4.       setMeasuredDimension(0, 0); 
  5.   } 
  6.   //todo 為啥這個控件 是個大小為0的控件 ,那是因為他媽的這里更不就沒有畫 
  7.   @Override 
  8.   public void draw(Canvas canvas) { 
  9.   } 
  10.  
  11.   @Override 
  12.   protected void dispatchDraw(Canvas canvas) { 
  13.   } 

關(guān)于inflate()方法

  1. public View inflate() { 
  2.       // 1、獲取ViewStub的parent view,也是目標(biāo)布局根元素的parent view 
  3.       final ViewParent viewParent = getParent(); 
  4.  
  5.       if (viewParent != null &amp;&amp; viewParent instanceof ViewGroup) { 
  6.           if (mLayoutResource != 0) { 
  7.               final ViewGroup parent = (ViewGroup) viewParent; 
  8.               /// 2、加載目標(biāo)布局  牛逼的方法 
  9.               final View view = inflateViewNoAdd(parent); 
  10.               // 3、將ViewStub自身從parent中移除 
  11.               replaceSelfWithView(view, parent); 
  12.  
  13.               mInflatedViewRef = new WeakReference<>(view); 
  14.               if (mInflateListener != null) { 
  15.                   mInflateListener.onInflate(this, view); 
  16.               } 
  17.  
  18.               return view
  19.           } else { 
  20.               // TODO: 2018/5/23 必須設(shè)置布局的文件 
  21.               throw new IllegalArgumentException("ViewStub must have a valid layoutResource"); 
  22.           } 
  23.       } else { 
  24.           // TODO: 2018/5/23 iewParent instanceof ViewGroup 不屬于的話,就好比在一個TextView創(chuàng)建一個ViewStub直接爆炸 
  25.           throw new IllegalStateException("ViewStub must have a non-null ViewGroup viewParent"); 
  26.       } 
  27.   } 
  • 第一點,ViewStup也只能在ViewGroup中使用,不能在View中去使用,要不然會拋出異常IllegalStateException("ViewStub must have a non-null ViewGroup viewParent");
  • 第二點,也必須設(shè)置layout屬性,要不然也會拋出異常throw new IllegalArgumentException("ViewStub must have a valid layoutResource");;

關(guān)于方法inflateViewNoAdd(parent);

  1. private View inflateViewNoAdd(ViewGroup parent) { 
  2.       final LayoutInflater factory; 
  3.       if (mInflater != null) { 
  4.           factory = mInflater; 
  5.       } else { 
  6.           factory = LayoutInflater.from(mContext); 
  7.       } 
  8.       final View view = factory.inflate(mLayoutResource, parent, false); 
  9.       //和 LayoutInflater一個道理,設(shè)置了,ViewStub 引用進來的根布局的id找出來為null  非常有些意思 
  10.       if (mInflatedId != NO_ID) { 
  11.           view.setId(mInflatedId); 
  12.       } 
  13.       return view
  14.   } 
  • 第一點,底層調(diào)用的還是LayoutInflater.from(mContext).inflate(mLayoutResource, parent, false);
  • 第二點,又看到這個方法,似曾相識,對,這也是為什么ViewStub找不到根布局id的原因,因為mInflatedId != NO_ID,就會view.setId(mInflatedId);
  1.  if (mInflatedId != NO_ID) { 
  2.         view.setId(mInflatedId); 

將ViewStub自身從parent中移除replaceSelfWithView(view, parent);,具體的原因,這里不做分析,因為有點小復(fù)雜,這里就大概明白就行,對于理解這個ViewStub不困難,哈哈

  1. private void replaceSelfWithView(View view, ViewGroup parent) { 
  2.      final int index = parent.indexOfChild(this); 
  3.      // 3、將ViewStub自身從parent中移除 
  4.      parent.removeViewInLayout(this); 
  5.  
  6.      final ViewGroup.LayoutParams layoutParams = getLayoutParams(); 
  7.      if (layoutParams != null) { 
  8.          // 4、將目標(biāo)布局的根元素添加到parent中,有參數(shù) 
  9.          parent.addView(viewindex, layoutParams); 
  10.      } else { 
  11.         // 5、將目標(biāo)布局的根元素添加到parent中 
  12.          parent.addView(viewindex); 
  13.      } 
  14.  } 

這里使用到了弱引用,只有弱引用指向的對象的生命周期更短,當(dāng)垃圾回收器掃描到只有具有弱引用的對象的時候,不論當(dāng)前空間是否不足,都會對弱引用對象進行回收,當(dāng)然弱引用也可以和一個隊列配合著使用,為了更好地釋放內(nèi)存,安卓代碼、圖片、布局、網(wǎng)絡(luò)和電量優(yōu)化這篇文章有很好的解釋,而且這個mInflatedViewRef只在這里初始化,如果說沒有調(diào)用inflate的方法的話,這個對象一定為null;

  1. //更好的釋放內(nèi)存 
  2.  private WeakReference<View> mInflatedViewRef; 
  3.  mInflatedViewRef = new WeakReference<>(view); 
  4.                if (mInflateListener != null) { 
  5.               mInflateListener.onInflate(this, view
  6.  } 

為啥setVisibility(View.VISIBLE)等同于inflate,原因是ViewStub進行了重寫??梢钥闯龃a的邏輯,只要沒有調(diào)用過,inflate()方法,setVisibility(VISIBLE )和setVisibility(INVISIBLE)這個兩個參數(shù)走的方法一樣,只不過,一個看不到,實際上的位置已經(jīng)確定了(INVISIBLE)。但是如果調(diào)用多次的話setVisibility()記得也得判斷下null!=mViewStub.getParent()

  1. @Override 
  2.     @android.view.RemotableViewMethod(asyncImpl = "setVisibilityAsync"
  3.     public void setVisibility(int visibility) { 
  4.         // TODO: 2018/5/23  弱引用的使用 
  5.         //如果已經(jīng)加載過則只設(shè)置Visibility屬性 
  6.         if (mInflatedViewRef != null) { 
  7.             View view = mInflatedViewRef.get(); 
  8.             if (view != null) { 
  9.                 view.setVisibility(visibility); 
  10.             } else { 
  11.                 throw new IllegalStateException("setVisibility called on un-referenced view"); 
  12.             } 
  13.         } else { 
  14.             // 如果未加載,這加載目標(biāo)布局 
  15.             super.setVisibility(visibility); 
  16.             if (visibility == VISIBLE || visibility == INVISIBLE) { 
  17.                 inflate();// 調(diào)用inflate來加載目標(biāo)布局 
  18.             } 
  19.         } 
  20.     } 

貼出全部的代碼,有空的話,可以研究下。

  1. @RemoteView 
  2. public final class ViewStub extends View { 
  3.     private int mInflatedId; 
  4.     private int mLayoutResource; 
  5.     // TODO: 2018/5/23 弱引用:弱引用是比軟引用更弱的一種的引用的類型, 
  6.     // 只有弱引用指向的對象的生命周期更短,當(dāng)垃圾回收器掃描到只有具有弱引用的對象的時候, 
  7.     // 不敢當(dāng)前空間是否不足,都會對弱引用對象進行回收,當(dāng)然弱引用也可以和一個隊列配合著使用 
  8.  
  9.     //更好的釋放內(nèi)存 
  10.     private WeakReference<View> mInflatedViewRef; 
  11.  
  12.     private LayoutInflater mInflater; 
  13.     private OnInflateListener mInflateListener; 
  14.  
  15.     public ViewStub(Context context) { 
  16.         this(context, 0); 
  17.     } 
  18.  
  19.     /** 
  20.      * Creates a new ViewStub with the specified layout resource. 
  21.      * 
  22.      * @param context The application's environment. 
  23.      * @param layoutResource The reference to a layout resource that will be inflated. 
  24.      */ 
  25.     public ViewStub(Context context, @LayoutRes int layoutResource) { 
  26.         this(context, null); 
  27.  
  28.         mLayoutResource = layoutResource; 
  29.     } 
  30.  
  31.     public ViewStub(Context context, AttributeSet attrs) { 
  32.         this(context, attrs, 0); 
  33.     } 
  34.  
  35.     public ViewStub(Context context, AttributeSet attrs, int defStyleAttr) { 
  36.         this(context, attrs, defStyleAttr, 0); 
  37.     } 
  38.  
  39.     public ViewStub(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 
  40.         super(context); 
  41.  
  42.         final TypedArray a = context.obtainStyledAttributes(attrs, 
  43.                 R.styleable.ViewStub, defStyleAttr, defStyleRes); 
  44.         // TODO: 2018/5/23  ViewStub 中設(shè)置的標(biāo)簽id 如果設(shè)置了 這里就一定有值 mInflatedId!=NO_Id 
  45.         mInflatedId = a.getResourceId(R.styleable.ViewStub_inflatedId, NO_ID); 
  46.         mLayoutResource = a.getResourceId(R.styleable.ViewStub_layout, 0); 
  47.         mID = a.getResourceId(R.styleable.ViewStub_id, NO_ID); 
  48.         a.recycle(); 
  49.         //不可見 
  50.         setVisibility(GONE); 
  51.         // 設(shè)置不繪制 
  52.         setWillNotDraw(true); 
  53.     } 
  54.  
  55.     /** 
  56.      * Returns the id taken by the inflated view. If the inflated id is 
  57.      * {@link View#NO_ID}, the inflated view keeps its original id. 
  58.      * 
  59.      * @return A positive integer used to identify the inflated view or 
  60.      *         {@link #NO_ID} if the inflated view should keep its id. 
  61.      * 
  62.      * @see #setInflatedId(int
  63.      * @attr ref android.R.styleable#ViewStub_inflatedId 
  64.      */ 
  65.     @IdRes 
  66.     public int getInflatedId() { 
  67.         return mInflatedId; 
  68.     } 
  69.  
  70.     /** 
  71.      * Defines the id taken by the inflated view. If the inflated id is 
  72.      * {@link View#NO_ID}, the inflated view keeps its original id. 
  73.      * 
  74.      * @param inflatedId A positive integer used to identify the inflated view or 
  75.      *                   {@link #NO_ID} if the inflated view should keep its id. 
  76.      * 
  77.      * @see #getInflatedId() 
  78.      * @attr ref android.R.styleable#ViewStub_inflatedId 
  79.      */ 
  80.     @android.view.RemotableViewMethod(asyncImpl = "setInflatedIdAsync"
  81.     public void setInflatedId(@IdRes int inflatedId) { 
  82.         mInflatedId = inflatedId; 
  83.     } 
  84.  
  85.     /** @hide **/ 
  86.     public Runnable setInflatedIdAsync(@IdRes int inflatedId) { 
  87.         mInflatedId = inflatedId; 
  88.         return null
  89.     } 
  90.  
  91.     /** 
  92.      * Returns the layout resource that will be used by {@link #setVisibility(int)} or 
  93.      * {@link #inflate()} to replace this StubbedView 
  94.      * in its parent by another view
  95.      * 
  96.      * @return The layout resource identifier used to inflate the new View
  97.      * 
  98.      * @see #setLayoutResource(int
  99.      * @see #setVisibility(int
  100.      * @see #inflate() 
  101.      * @attr ref android.R.styleable#ViewStub_layout 
  102.      */ 
  103.     @LayoutRes 
  104.     public int getLayoutResource() { 
  105.         return mLayoutResource; 
  106.     } 
  107.  
  108.     /** 
  109.      * Specifies the layout resource to inflate when this StubbedView becomes visible or invisible 
  110.      * or when {@link #inflate()} is invoked. The View created by inflating the layout resource is 
  111.      * used to replace this StubbedView in its parent. 
  112.      * 
  113.      * @param layoutResource A valid layout resource identifier (different from 0.) 
  114.      * 
  115.      * @see #getLayoutResource() 
  116.      * @see #setVisibility(int
  117.      * @see #inflate() 
  118.      * @attr ref android.R.styleable#ViewStub_layout 
  119.      */ 
  120.     @android.view.RemotableViewMethod(asyncImpl = "setLayoutResourceAsync"
  121.     public void setLayoutResource(@LayoutRes int layoutResource) { 
  122.         mLayoutResource = layoutResource; 
  123.     } 
  124.  
  125.     /** @hide **/ 
  126.     public Runnable setLayoutResourceAsync(@LayoutRes int layoutResource) { 
  127.         mLayoutResource = layoutResource; 
  128.         return null
  129.     } 
  130.  
  131.     /** 
  132.      * Set {@link LayoutInflater} to use in {@link #inflate()}, or {@code null
  133.      * to use the default
  134.      */ 
  135.     public void setLayoutInflater(LayoutInflater inflater) { 
  136.         mInflater = inflater; 
  137.     } 
  138.  
  139.     /** 
  140.      * Get current {@link LayoutInflater} used in {@link #inflate()}. 
  141.      */ 
  142.     public LayoutInflater getLayoutInflater() { 
  143.         return mInflater; 
  144.     } 
  145.  
  146.     @Override 
  147.     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
  148.         // 寬高都為0   onMeasure的時候 寬高都為0 
  149.         setMeasuredDimension(0, 0); 
  150.     } 
  151.     //todo 為啥這個控件 是個大小為0的控件 ,那是因為他媽的這里更不就沒有畫 
  152.     @Override 
  153.     public void draw(Canvas canvas) { 
  154.     } 
  155.  
  156.     @Override 
  157.     protected void dispatchDraw(Canvas canvas) { 
  158.     } 
  159.  
  160.     /** 
  161.      * When visibility is set to {@link #VISIBLE} or {@link #INVISIBLE}, 
  162.      * {@link #inflate()} is invoked and this StubbedView is replaced in its parent 
  163.      * by the inflated layout resource. After that calls to this function are passed 
  164.      * through to the inflated view
  165.      * 
  166.      * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}. 
  167.      * 
  168.      * @see #inflate() 
  169.      */ 
  170.     @Override 
  171.     @android.view.RemotableViewMethod(asyncImpl = "setVisibilityAsync"
  172.     public void setVisibility(int visibility) { 
  173.         // TODO: 2018/5/23  弱引用的使用 
  174.         //如果已經(jīng)加載過則只設(shè)置Visibility屬性 
  175.         if (mInflatedViewRef != null) { 
  176.             View view = mInflatedViewRef.get(); 
  177.             if (view != null) { 
  178.                 view.setVisibility(visibility); 
  179.             } else { 
  180.                 throw new IllegalStateException("setVisibility called on un-referenced view"); 
  181.             } 
  182.         } else { 
  183.             // 如果未加載,這加載目標(biāo)布局 
  184.             super.setVisibility(visibility); 
  185.             if (visibility == VISIBLE || visibility == INVISIBLE) { 
  186.                 inflate();// 調(diào)用inflate來加載目標(biāo)布局 
  187.             } 
  188.         } 
  189.     } 
  190.  
  191.     /** @hide **/ 
  192.     public Runnable setVisibilityAsync(int visibility) { 
  193.         if (visibility == VISIBLE || visibility == INVISIBLE) { 
  194.             ViewGroup parent = (ViewGroup) getParent(); 
  195.             return new ViewReplaceRunnable(inflateViewNoAdd(parent)); 
  196.         } else { 
  197.             return null
  198.         } 
  199.     } 
  200.  
  201.     private View inflateViewNoAdd(ViewGroup parent) { 
  202.         final LayoutInflater factory; 
  203.         if (mInflater != null) { 
  204.             factory = mInflater; 
  205.         } else { 
  206.             factory = LayoutInflater.from(mContext); 
  207.         } 
  208.         final View view = factory.inflate(mLayoutResource, parent, false); 
  209.         //和 LayoutInflater一個道理,設(shè)置了,ViewStub 引用進來的根布局的id找出來為null  非常有些意思 
  210.         if (mInflatedId != NO_ID) { 
  211.             view.setId(mInflatedId); 
  212.         } 
  213.         return view
  214.     } 
  215.  
  216.     // TODO: 2018/5/23 關(guān)注他 
  217.     private void replaceSelfWithView(View view, ViewGroup parent) { 
  218.         final int index = parent.indexOfChild(this); 
  219.         // 3、將ViewStub自身從parent中移除 
  220.         parent.removeViewInLayout(this); 
  221.  
  222.         final ViewGroup.LayoutParams layoutParams = getLayoutParams(); 
  223.         if (layoutParams != null) { 
  224.             // 4、將目標(biāo)布局的根元素添加到parent中,有參數(shù) 
  225.             parent.addView(viewindex, layoutParams); 
  226.         } else { 
  227.            // 5、將目標(biāo)布局的根元素添加到parent中 
  228.             parent.addView(viewindex); 
  229.         } 
  230.     } 
  231.  
  232.     /** 
  233.      * Inflates the layout resource identified by {@link #getLayoutResource()} 
  234.      * and replaces this StubbedView in its parent by the inflated layout resource. 
  235.      * 
  236.      * @return The inflated layout resource. 
  237.      * 
  238.      */ 
  239.     public View inflate() { 
  240.         // 1、獲取ViewStub的parent view,也是目標(biāo)布局根元素的parent view 
  241.         final ViewParent viewParent = getParent(); 
  242.  
  243.         if (viewParent != null &amp;&amp; viewParent instanceof ViewGroup) { 
  244.             if (mLayoutResource != 0) { 
  245.                 final ViewGroup parent = (ViewGroup) viewParent; 
  246.                 /// 2、加載目標(biāo)布局  牛逼的方法 
  247.                 final View view = inflateViewNoAdd(parent); 
  248.                 // 3、將ViewStub自身從parent中移除 
  249.                 replaceSelfWithView(view, parent); 
  250.  
  251.                 mInflatedViewRef = new WeakReference<>(view); 
  252.                 if (mInflateListener != null) { 
  253.                     mInflateListener.onInflate(this, view); 
  254.                 } 
  255.  
  256.                 return view
  257.             } else { 
  258.                 // TODO: 2018/5/23 必須設(shè)置布局的文件 
  259.                 throw new IllegalArgumentException("ViewStub must have a valid layoutResource"); 
  260.             } 
  261.         } else { 
  262.             // TODO: 2018/5/23 iewParent instanceof ViewGroup 不屬于的話,就好比在一個TextView創(chuàng)建一個ViewStub直接爆炸 
  263.             throw new IllegalStateException("ViewStub must have a non-null ViewGroup viewParent"); 
  264.         } 
  265.     } 
  266.  
  267.     /** 
  268.      * Specifies the inflate listener to be notified after this ViewStub successfully 
  269.      * inflated its layout resource. 
  270.      * 
  271.      * @param inflateListener The OnInflateListener to notify of successful inflation. 
  272.      * 
  273.      * @see ViewStub.OnInflateListener 
  274.      */ 
  275.     public void setOnInflateListener(OnInflateListener inflateListener) { 
  276.         mInflateListener = inflateListener; 
  277.     } 
  278.  
  279.     /** 
  280.      * Listener used to receive a notification after a ViewStub has successfully 
  281.      * inflated its layout resource. 
  282.      * 
  283.      * @see ViewStub#setOnInflateListener(ViewStub.OnInflateListener) 
  284.      */ 
  285.     public static interface OnInflateListener { 
  286.         /** 
  287.          * Invoked after a ViewStub successfully inflated its layout resource. 
  288.          * This method is invoked after the inflated view was added to the 
  289.          * hierarchy but before the layout pass. 
  290.          * 
  291.          * @param stub The ViewStub that initiated the inflation. 
  292.          * @param inflated The inflated View
  293.          */ 
  294.         void onInflate(ViewStub stub, View inflated); 
  295.     } 
  296.  
  297.     /** @hide **/ 
  298.     public class ViewReplaceRunnable implements Runnable { 
  299.         public final View view
  300.  
  301.         ViewReplaceRunnable(View view) { 
  302.             this.view = view
  303.         } 
  304.  
  305.         @Override 
  306.         public void run() { 
  307.             replaceSelfWithView(view, (ViewGroup) getParent()); 
  308.         } 
  309.     } 

最后做了一張圖

基于Android 8.0分析源碼ViewStub源碼解析

說明一下ViewStub的原理很簡單!好吧,這個有點皮

責(zé)任編輯:未麗燕 來源: 安卓巴士Android開發(fā)者門戶
相關(guān)推薦

2022-03-18 15:55:15

鴻蒙操作系統(tǒng)架構(gòu)

2015-09-22 10:10:13

AndroidVolleyHTTP

2010-02-06 13:28:31

Android源碼

2010-01-25 10:35:12

Android復(fù)選框

2022-01-20 14:33:29

openharmonwayland協(xié)議鴻蒙

2010-03-24 17:03:57

Python源碼分析

2016-08-31 13:48:00

AndroidRetrofit源碼解析

2021-07-03 08:51:30

源碼Netty選擇器

2023-03-17 07:53:20

K8sAPIServerKubernetes

2024-09-11 09:25:03

Tomcat組件PREP

2021-07-09 06:48:30

注冊源碼解析

2014-08-26 11:11:57

AsyncHttpCl源碼分析

2011-03-15 11:33:18

iptables

2022-07-19 20:04:31

NAPI模塊鴻蒙

2017-02-21 12:20:20

Android事件分發(fā)機制實例解析

2021-09-09 06:55:43

AndroidViewDragHel原理

2015-09-16 09:10:27

Java源碼解析

2023-10-09 09:02:50

.Net析構(gòu)函數(shù)分配

2014-04-29 13:16:42

OpenGLAndroid庫加載過程

2013-04-03 15:45:51

Android瀑布流android_wat
點贊
收藏

51CTO技術(shù)棧公眾號