ViewStub - Optimized Layout
- 作用:用于优化布局,懒加载,运行时才会加载布局。
- 使用场景:通常用于有些隐藏的或者特殊情况才会显示的布局。
例如:一个ListView,数据为空时,显示一个布局告诉用户
在xml中是使用:
<ViewStub
android:id="@+id/stub"
android:inflatedId="@+id/subTree"
android:layout="@layout/mySubTree"
/>
之前一直都不知道inflatedId有什么用,怎么用,决定看看源码:
ViewStub的构造器
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的属性;调用setVisibility(GONE),所以ViewStub默认是隐藏的。
还有在实际使用中,会发现ViewStub除了
有inflatedId, layout, id这三个属性,其他View的属性都没有效果。
原因是:ViewStub继承View, 构造器只是调用super(context),而在View中只有Content一个参数
的构造器不会读取其他属性.
ViewStub的优化原理简单粗暴, 重写onMeasure(), 测量时设置宽高为0,重写draw()不绘制任何东西.
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(0, 0);
}
@Override
public void draw(Canvas canvas) {
}
@Override
protected void dispatchDraw(Canvas canvas) {
}
看看inflate方法源码:
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就可以了
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);
}
注意事项:
- inflate()方法只能调用一次,由于第一次ViewStub已经从parent中移除,parent第二次调用会为null,
接着就会throw new IllegalStateException("ViewStub must have a non-null ViewGroup viewParent") - ViewStub在xml中id为stub,第一次使用findViewById()可以找到ViewStub,第二次之后就找不到了。
- 当设置了android:inflatedId="@+id/subTree",
首次可以使用inflacte方法获取inflactedView, View inflactedView = viewStub.inflacte(),
接着可以findViewById(R.id.subTree)获取inflactedView.