androidviewviewstub

How to check if a viewStub is already inflated?


I did not find any boolean method does this task. Can I do this by checking if the id of the viewStubchanged to the one specified as inflatedid?

Javacode:

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

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

            if (mViewStub is inflated) {
              //do somthing
              }else
               mViewstub.inflate();

}

Update Comments on th eOutput

According to this code, the toast always displays its message, which mean since mViewStub is assigned to findViewById it is never null except the viewstub view in the underlaying alyout is not available. Any suggestions.?

protected void plantViewTree() {
    // TODO Auto-generated method stub
    ViewStub mViewstub = (ViewStub) findViewById(R.id.viewStub00);
    if (mViewstub != null)
        Toast.makeText(getApplicationContext(), "view is inflated", 
Toast.LENGTH_LONG).show();
    else
        mViewstub.inflate();
}

Solution

  • We can view the ViewStub source code, the most important method is 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");
        }
    }
    

    Notice this line parent.removeViewInLayout(this) ,it had removed in layout after inflate.so we can check if a viewStub is already inflated by this way.

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