ViewStub - Optimized Layout ViewTreeObserver

ViewStub - Optimized Layout ViewTreeObserver

作用:
用于优化布局,运行时才会加载布局, 宽高为0的View。
使用场景:通常隐藏,特殊情况才会显示的布局。例如:一个ListView,数据为空时,显示一个布局告诉用户

在xml中是使用:

ViewStub android:id="@+id/stub" android:inflatedId="@+id/subTree" android:layout="@layout/mySubTree" /

以前一直都不知道inflatedId有什么用,怎么用,决定看看源码:

ViewStub的构造器很简单:

1
2
3
4
5
6
7
8
9
10
super(context);
final TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.ViewStub, defStyleAttr, defStyleRes);
mInflatedId = a.getResourceId(R.styleable.ViewStub_inflatedId, NO_ID);
mLayoutResource = a.getResourceId(R.styleable.ViewStub_layout, 0);
mID = a.getResourceId(R.styleable.ViewStub_id, NO_ID);
a.recycle();

setVisibility(GONE);
setWillNotDraw(true);

构造很简单,读取布局inflatedId, layout, id的属性,
使用时发现在xml布局把ViewStub设置android:visible=”VISIBLE”并没什么卵用。
原因是:ViewStub继承View, 构造器只是调用super(context),也就是说ViewStub除了
有inflatedId, layout, id这三个属性,其他View的属性都没有。而且调用了setVisibility(GONE)。
所以ViewStub默认是隐藏的。

ViewStub的优化原理简单粗暴, 测量时设置宽高为0,不绘制任何东西,
当调用inflate方法, ViewStub会被@layout/mySubTree的布局替换,并返回mySubTree里布局的View.

1
2
3
4
5
6
7
8
9
10
11
12
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(0, 0);
}

@Override
public void draw(Canvas canvas) {
}

@Override
protected void dispatchDraw(Canvas canvas) {
}

看看inflate方法源码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
public View inflate() {
final ViewParent viewParent = getParent();
if (viewParent != null && viewParent instanceof ViewGroup) {
if (mLayoutResource != 0) {
final ViewGroup parent = (ViewGroup) viewParent;
final LayoutInflater factory;
if (mInflater != null) {
factory = mInflater;
} else {
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");
}
}

分析源码得知:
首先调用getParent()获取父布局parent,利用LayoutInflater把@layout/mySubTree这个layout加载,(为加载的layout起名为inflatedView),
把mInflatedId设置给inflatedView, int index = parent.indexOfChild(this)这一句获取ViewStub在父布局的位置,
使用parent.removeViewInLayout(this)把ViewStub从parent中移除,最后将inflatedView添加到相应的位置并替换ViewStub.

使用ViewStub通常可以不用直接调用inflate(), 要显示的时候直接可以调用setVisibility就可以了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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();
}
}
}

mInflatedViewRef是一个View的弱引用,调用inflate()后才不为null,
第一次调用setVisibility(VISIBLE||INVISIBLE)会调用inflate(),之后会从mInflatedViewRef中
获取inflactedView的弱引用,直接setVisibility(visibility).

还可以设置OnInflateListener这个接口进行一些inflatedView的初始化工作,
这个接口只会被调用一次。
public static interface OnInflateListener {
void onInflate(ViewStub stub, View inflated);
}

注意事项:

  1. inflate()方法只能调用一次,由于第一次ViewStub已经从parent中移除,parent第二次调用会为null,
    接着就会throw new IllegalStateException(“ViewStub must have a non-null ViewGroup viewParent”)
  2. ViewStub在xml中id为stub,第一次使用findViewById()可以找到ViewStub,第二次之后就找不到了。
  3. 当设置了android:inflatedId=”@+id/subTree”,
    首次可以使用inflacte方法获取inflactedView, View inflactedView = viewStub.inflacte(),
    接着可以findViewById(R.id.subTree)获取inflactedView.
Loading comments box needs to over the wall