<ViewStub
android:id="@+id/view_stub"
android:layout_margin="10dp"
android:inflatedId="@+id/tv_title"
android:layout="@layout/tv_title_layout"
android:layout_width="220dp"
android:layout_height="50dp" />
复制代码
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tv_title"
android:text="I'm inflate view."
android:textColor="#000"
android:textSize="15sp"
android:layout_width="match_parent"
android:layout_height="match_parent">
</TextView>
复制代码
虽然,layout里面布局写的 width 和 height 为 match_parent,可是不生效,只会继承于 viewstub 指定的大小。android
咱们在代码里面就能够这样写:canvas
ViewStub viewStub = (ViewStub) findViewById(R.id.view_stub);
if (viewStub!=null) {
viewStub.inflate();
}
TextView tvTitle = (TextView) findViewById(R.id.tv_title);
tvTitle.setText("I'm inflate: " + System.currentTimeMillis());
复制代码
private int mInflatedId;//viewstub传递给布局的id
private int mLayoutResource;//viewstub的布局
private WeakReference<View> mInflatedViewRef;
复制代码
这里有一个弱引用,是用来干吗的呢?咱们先无论,往下看。bash
public ViewStub(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
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);
}
复制代码
很普通,直接从xml布局中读取相应的属性,而且在构造函数中就设置为 GONE,接着:setWillNotDraw(true),设置一个标记,声明这个View不作 onDraw 绘制。ide
/**
* If this view doesn't do any drawing on its own, set this flag to * allow further optimizations. By default, this flag is not set on * View, but could be set on some View subclasses such as ViewGroup. * * Typically, if you override {@link #onDraw(android.graphics.Canvas)} * you should clear this flag. * * @param willNotDraw whether or not this View draw on its own */ public void setWillNotDraw(boolean willNotDraw) { setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK); } 复制代码
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(0, 0);
}
@Override
public void draw(Canvas canvas) {
}
复制代码
measure直接传入0,说明ViewStub初始化就是一个0大小的View。函数
从上面知道,inflate 或者 setVisibility 均可以加载布局,咱们先看: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();
}
}
}
复制代码
先判断弱引用里面是否有View,有的话就直接设置为可见,为空,就执行 inflate():布局
public View inflate() {
final ViewParent viewParent = getParent();
if (viewParent != null && viewParent instanceof ViewGroup) {
if (mLayoutResource != 0) {
final ViewGroup parent = (ViewGroup) viewParent;
//mInflater 能够从外部传进来
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);
}
//获取当前ViewStub在ViewGroup中的index
final int index = parent.indexOfChild(this);
//ViewStub替换成inflate后的View
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);
//inflate回调
if (mInflateListener != null) {
mInflateListener.onInflate(this, view);
}
return view;
}
//省略异常
}
//省略异常
}
复制代码
public static interface OnInflateListener {
void onInflate(ViewStub stub, View inflated);
}
复制代码
在 inflate 的时候会进行回调ui