如何检查viewStub是否已经膨胀?

时间:2014-05-21 12:25:29

标签: android view viewstub

我没有找到任何布尔方法执行此任务。我可以通过检查id的{​​{1}}是否更改为指定为viewStub的那个来完成此操作吗?

Javacode:

inflatedid

更新 关于电子输入的评论

根据此代码, protected void plantViewTree() { // TODO Auto-generated method stub ViewStub mViewstub = (ViewStub) findViewById(R.id.viewStub00); if (mViewStub is inflated) { //do somthing }else mViewstub.inflate(); } 始终显示其消息,这意味着自toast分配给mViewStub后,除了底层的findViewById视图外,它永远不会为空alyout不可用。有什么建议。?

viewstub

9 个答案:

答案 0 :(得分:17)

我们可以查看ViewStub源代码,最重要的方法是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");
    }
}

注意这一行parent.removeViewInLayout(this),它在inflate之后在布局中删除了。所以我们可以通过这种方式检查viewStub是否已经膨胀。

if (mViewStub.getParent() != null) {
    mViewStub.inflate();
} else {
    mViewStub.setVisibility(View.VISIBLE);
}

答案 1 :(得分:2)

来自谷歌的说明:

  

当ViewStub可见时,或者调用inflate()时,   布局资源膨胀。

所以你可以检查可见性(甚至检查它是否&#34; null&#34;)。

答案 2 :(得分:0)

致电yourViewStub.visibility(View.VISIBLE)您无需检查是否充气。

答案 3 :(得分:0)

if (mViewStub.getParent() != null) { 
    //have not been inflated
    mViewStub.inflate();
} else { 
    //already inflated
}

答案 4 :(得分:0)

在Kotlin中更简单的方法:

if (viewStub != null) {
   viewStub.isVisible = true
} else {
   // The view has been inflated already.
}

答案 5 :(得分:0)

您可以在Kotlin中使用它。.我知道它使用标签(不酷)但易于使用。 这是kotlin扩展功能:

fun ViewStub.doInflate(init: View.() -> Unit = {}, action: (View) -> Unit = {}) {
val isInflated = (tag as? Boolean) ?: false
if (!isInflated) {
    setOnInflateListener { _, view ->
        this.tag = true
        init.invoke(view)
        action.invoke(view)
    }
    this.inflate()
} else {
    action.invoke(this.rootView)
}

}

,您可以像这样使用它:

 myViewStub.doInflate({
            //code that run with initialisation
        }, {
           //code that run if stub already inflated
        })

答案 6 :(得分:0)

正如文档所建议的那样,我们不应保留对ViewStub的长期引用,并且android还允许为ViewStub分配getInflatedView(): View,一旦它膨胀,它就会存在

所以我们可以有一个像fun getInflatedView(@LayoutRes layoutId: Int): View { val stub: ViewStub? = findViewById(R.id.stubId) stub?.layoutResource = layoutId return stub?.inflate() ?: findViewById(R.id.inflatedId) }

这样的函数签名
{{1}}

答案 7 :(得分:0)

您可以使用此扩展程序:

/**
 * To prevent crash when we try inflate view that was already inflated. Because OS delete ViewStub by inflating.
 */
fun ViewStub?.safeInflate() {
    if (this?.parent != null) inflate()
}

答案 8 :(得分:-5)

我使用flags解决了这个问题。我声明了一个全局布尔变量isInflatedBefore集 最初到false

代码:

//declaration of the variable
private boolean isInflatedBefore = false

...
... 
...

protected void plantViewTree() {
    // TODO Auto-generated method stub

    ViewStub mViewstub = (ViewStub) findViewById(R.id.viewStub00);

    if (! isInflatedBefore) {
        isInflatedBefore = true;
        mViewstub.inflate();
              }else {
                //some code
               }