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

移动开发 Android
ViewStub是一种不可见的并且大小为0的试图,它可以延迟到运行时才填充inflate布局资源,当Viewstub设为可见或者是inflate的时候,就会填充布局资源,这个布局和普通的试图就基本上没有任何区别,比如说,加载网络失败,或者是一个比较消耗性能的功能,需要用户去点击才可以加载!从而这样更加的节约了性能。对安卓布局很友好!

源码基于安卓8.0分析结果

ViewStub是一种不可见的并且大小为0的试图,它可以延迟到运行时才填充inflate 布局资源,当Viewstub设为可见或者是inflate的时候,就会填充布局资源,这个布局和普通的试图就基本上没有任何区别,比如说,加载网络失败,或者是一个比较消耗性能的功能,需要用户去点击才可以加载!从而这样更加的节约了性能。对安卓布局很友好!

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" /> 

这篇文章安卓代码、图片、布局、网络和电量优化说如果这个根布局是个View,比如说是个ImagView,那么找出来的id为null,得必须注意这一点 -----2018.6.7修正这个说法,以前我说的是错误的,根本上的原因是ViewStub设置了 inflateid ,这才是更本身的原因

在这里记住一点,如果在 ViewStub标签中设置了android:inflatedId="@+id/view_stub_inflateid",在layout布局中的根布局在设置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设置了 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,具体的骚操作,后续我介绍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函数直接返回ListView对象 
  6.      commLv2 = (View)mViewStub.inflate(); 
  7.    } else { 
  8.       // ViewStub已经加载 
  9.   } 

ViewStub构造方法,注意获取了几个值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 中设置的标签id 如果设置了 这里就一定有值 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.         // 设置不绘制 
  14.         setWillNotDraw(true); 
  15.     } 

在构造方法中:同时注意不可见 setVisibility(GONE); ,设置不绘制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.   } 

关于inflate()方法

  1. public View inflate() { 
  2.       // 1、获取ViewStub的parent view,也是目标布局根元素的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、加载目标布局  牛逼的方法 
  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 必须设置布局的文件 
  21.               throw new IllegalArgumentException("ViewStub must have a valid layoutResource"); 
  22.           } 
  23.       } else { 
  24.           // TODO: 2018/5/23 iewParent instanceof ViewGroup 不属于的话,就好比在一个TextView创建一个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");
  • 第二点,也必须设置layout属性,要不然也会抛出异常throw new IllegalArgumentException("ViewStub must have a valid layoutResource");;

关于方法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一个道理,设置了,ViewStub 引用进来的根布局的id找出来为null  非常有些意思 
  10.       if (mInflatedId != NO_ID) { 
  11.           view.setId(mInflatedId); 
  12.       } 
  13.       return view
  14.   } 
  • 第一点,底层调用的还是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);,具体的原因,这里不做分析,因为有点小复杂,这里就大概明白就行,对于理解这个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、将目标布局的根元素添加到parent中,有参数 
  9.          parent.addView(viewindex, layoutParams); 
  10.      } else { 
  11.         // 5、将目标布局的根元素添加到parent中 
  12.          parent.addView(viewindex); 
  13.      } 
  14.  } 

这里使用到了弱引用,只有弱引用指向的对象的生命周期更短,当垃圾回收器扫描到只有具有弱引用的对象的时候,不论当前空间是否不足,都会对弱引用对象进行回收,当然弱引用也可以和一个队列配合着使用,为了更好地释放内存,安卓代码、图片、布局、网络和电量优化这篇文章有很好的解释,而且这个mInflatedViewRef只在这里初始化,如果说没有调用inflate的方法的话,这个对象一定为null;

  1. //更好的释放内存 
  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进行了重写。可以看出代码的逻辑,只要没有调用过,inflate()方法,setVisibility(VISIBLE )和setVisibility(INVISIBLE)这个两个参数走的方法一样,只不过,一个看不到,实际上的位置已经确定了(INVISIBLE)。但是如果调用多次的话setVisibility()记得也得判断下null!=mViewStub.getParent()

  1. @Override 
  2.     @android.view.RemotableViewMethod(asyncImpl = "setVisibilityAsync"
  3.     public void setVisibility(int visibility) { 
  4.         // TODO: 2018/5/23  弱引用的使用 
  5.         //如果已经加载过则只设置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.             // 如果未加载,这加载目标布局 
  15.             super.setVisibility(visibility); 
  16.             if (visibility == VISIBLE || visibility == INVISIBLE) { 
  17.                 inflate();// 调用inflate来加载目标布局 
  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.     // 只有弱引用指向的对象的生命周期更短,当垃圾回收器扫描到只有具有弱引用的对象的时候, 
  7.     // 不敢当前空间是否不足,都会对弱引用对象进行回收,当然弱引用也可以和一个队列配合着使用 
  8.  
  9.     //更好的释放内存 
  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 中设置的标签id 如果设置了 这里就一定有值 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.         // 设置不绘制 
  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.         //如果已经加载过则只设置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.             // 如果未加载,这加载目标布局 
  184.             super.setVisibility(visibility); 
  185.             if (visibility == VISIBLE || visibility == INVISIBLE) { 
  186.                 inflate();// 调用inflate来加载目标布局 
  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一个道理,设置了,ViewStub 引用进来的根布局的id找出来为null  非常有些意思 
  210.         if (mInflatedId != NO_ID) { 
  211.             view.setId(mInflatedId); 
  212.         } 
  213.         return view
  214.     } 
  215.  
  216.     // TODO: 2018/5/23 关注他 
  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、将目标布局的根元素添加到parent中,有参数 
  225.             parent.addView(viewindex, layoutParams); 
  226.         } else { 
  227.            // 5、将目标布局的根元素添加到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,也是目标布局根元素的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、加载目标布局  牛逼的方法 
  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 必须设置布局的文件 
  259.                 throw new IllegalArgumentException("ViewStub must have a valid layoutResource"); 
  260.             } 
  261.         } else { 
  262.             // TODO: 2018/5/23 iewParent instanceof ViewGroup 不属于的话,就好比在一个TextView创建一个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的原理很简单!好吧,这个有点皮

责任编辑:未丽燕 来源: 安卓巴士Android开发者门户
相关推荐

2010-02-06 13:28:31

Android源码

2015-09-22 10:10:13

AndroidVolleyHTTP

2022-03-18 15:55:15

鸿蒙操作系统架构

2022-01-20 14:33:29

openharmonwayland协议鸿蒙

2010-01-25 10:35:12

Android复选框

2016-08-31 13:48:00

AndroidRetrofit源码解析

2010-03-24 17:03:57

Python源码分析

2021-07-03 08:51:30

源码Netty选择器

2023-03-17 07:53:20

K8sAPIServerKubernetes

2014-04-29 13:16:42

OpenGLAndroid库加载过程

2021-07-09 06:48:30

注册源码解析

2011-03-15 11:33:18

iptables

2014-08-26 11:11:57

AsyncHttpCl源码分析

2015-03-31 18:26:43

陌陌社交

2013-04-03 15:45:51

Android瀑布流android_wat

2017-02-21 12:20:20

Android事件分发机制实例解析

2021-09-09 06:55:43

AndroidViewDragHel原理

2023-10-09 09:02:50

.Net析构函数分配

2022-07-19 20:04:31

NAPI模块鸿蒙

2022-05-20 10:32:49

事件循环器事件队列鸿蒙
点赞
收藏

51CTO技术栈公众号