inflate()
的时候,布局就会被加载(替换 ViewStub)。所以,ViewStub 一直存在于视图层次结构中直到调用了 setVisibility(int)
或 inflate()
。public ViewStub(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context);
final TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.ViewStub, defStyleAttr, defStyleRes);
// 要被加载的布局 Id
mInflatedId = a.getResourceId(R.styleable.ViewStub_inflatedId, NO_ID);
// 要被加载的布局
mLayoutResource = a.getResourceId(R.styleable.ViewStub_layout, 0);
// ViewStub 的 Id
mID = a.getResourceId(R.styleable.ViewStub_id, NO_ID);
a.recycle();
// 初始状态为 GONE
setVisibility(GONE);
// 设置为不会绘制
setWillNotDraw(true);
}
复制代码
// 复写了 setVisibility(int) 方法
@Override
@android.view.RemotableViewMethod public void setVisibility(int visibility) {
// private WeakReference<View> mInflatedViewRef;
// mInflatedViewRef 是对布局的弱引用
if (mInflatedViewRef != null) {
// 若是不为 null,就拿到懒加载的 View
View view = mInflatedViewRef.get();
if (view != null) {
// 而后就直接对 View 进行 setVisibility 操做
view.setVisibility(visibility);
} else {
// 若是为 null,就抛出异常
throw new IllegalStateException("setVisibility called on un-referenced view");
}
} else {
super.setVisibility(visibility);
// 以前说过,setVisibility(int) 也能够进行加载布局
if (visibility == VISIBLE || visibility == INVISIBLE) {
// 由于在这里调用了 inflate()
inflate();
}
}
}
复制代码
public View inflate() {
// 获取父视图
final ViewParent viewParent = getParent();
if (viewParent != null && viewParent instanceof ViewGroup) {
// 若是没有指定布局,就会抛出异常
if (mLayoutResource != 0) {
// viewParent 需为 ViewGroup
final ViewGroup parent = (ViewGroup) viewParent;
final LayoutInflater factory;
if (mInflater != null) {
factory = mInflater;
} else {
// 若是没有指定 LayoutInflater
factory = LayoutInflater.from(mContext);
}
// 获取布局
final View view = factory.inflate(mLayoutResource, parent,
false);
// 为 view 设置 Id
if (mInflatedId != NO_ID) {
view.setId(mInflatedId);
}
// 计算出 ViewStub 在 parent 中的位置
final int index = parent.indexOfChild(this);
// 把 ViewStub 从 parent 中移除
parent.removeViewInLayout(this);
// 接下来就是把 view 加到 parent 的 index 位置中
final ViewGroup.LayoutParams layoutParams = getLayoutParams();
if (layoutParams != null) {
// 若是 ViewStub 的 layoutParams 不为空
// 就设置给 view
parent.addView(view, index, layoutParams);
} else {
parent.addView(view, index);
}
// mInflatedViewRef 就是在这里对 view 进行了弱引用
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");
}
}
复制代码
@Override
@android.view.RemotableViewMethod(asyncImpl = "setVisibilityAsync")
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 {
}
}
复制代码
public View inflate() {
final ViewParent viewParent = getParent();
if (viewParent != null && viewParent instanceof ViewGroup) {
if (mLayoutResource != 0) {
mInflatedViewRef = new WeakReference<>(view);
return view;
} else {
throw new IllegalArgumentException("ViewStub must have a valid layoutResource");
}
}
}
复制代码
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(0, 0);
}
@Override
public void draw(Canvas canvas) {
}
@Override
protected void dispatchDraw(Canvas canvas) {
}
复制代码
public void setWillNotDraw(boolean willNotDraw) {
setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
}
复制代码
/** * This view won't draw. {@link #onDraw(android.graphics.Canvas)} won't be * called and further optimizations will be performed. It is okay to have * this flag set and a background. Use with DRAW_MASK when calling setFlags. * {@hide} */
static final int WILL_NOT_DRAW = 0x00000080;
复制代码
public ViewGroup(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
initViewGroup();
initFromAttributes(context, attrs, defStyleAttr, defStyleRes);
}
private void initViewGroup() {
// ViewGroup doesn't draw by default
if (!debugDraw()) {
setFlags(WILL_NOT_DRAW, DRAW_MASK);
}
mGroupFlags |= FLAG_CLIP_CHILDREN;
mGroupFlags |= FLAG_CLIP_TO_PADDING;
mGroupFlags |= FLAG_ANIMATION_DONE;
mGroupFlags |= FLAG_ANIMATION_CACHE;
mGroupFlags |= FLAG_ALWAYS_DRAWN_WITH_CACHE;
if (mContext.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.HONEYCOMB) {
mGroupFlags |= FLAG_SPLIT_MOTION_EVENTS;
}
setDescendantFocusability(FOCUS_BEFORE_DESCENDANTS);
mChildren = new View[ARRAY_INITIAL_CAPACITY];
mChildrenCount = 0;
mPersistentDrawingCache = PERSISTENT_SCROLLING_CACHE;
}
复制代码
public View inflate() {
//获取viewStub的父容器对象
final ViewParent viewParent = getParent();
if (viewParent != null && viewParent instanceof ViewGroup) {
if (mLayoutResource != 0) {
final ViewGroup parent = (ViewGroup) viewParent;
//这里是加载布局,而且给它设置id
//布局的加载是经过LayoutInflater解析出来的
final View view = inflateViewNoAdd(parent);
//这行代码很重要,下面会将到
replaceSelfWithView(view, parent);
//使用弱引用
mInflatedViewRef = new WeakReference<>(view);
if (mInflateListener != null) {
mInflateListener.onInflate(this, view);
}
return view;
} else {
//若是已经加载出来,再次inflate就会抛出异常呢
throw new IllegalArgumentException("ViewStub must have a valid layoutResource");
}
} else {
throw new IllegalStateException("ViewStub must have a non-null ViewGroup viewParent");
}
}
复制代码