How to check that viewStub is already overpriced?

I did not find any logical method for this task. Can I do this by checking if the id on the viewStub has changed to the one listed 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 eOutput

According to this code, toast always displays its message, which means that since mViewStub assigned to findViewById , it is never null, except that viewStub is not available in the alyout subclass. 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(); } 
+9
android view viewstub
source share
8 answers

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

Pay attention to this line parent.removeViewInLayout(this) , it was deleted in the layout after the inflate. So we can check if viewStub is too overpriced this way.

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

note from google:

When the ViewStub becomes visible or when the inflate () function is called, the layout resource is inflated.

so you can check visibility (or even check if it is "null").

+2
source share

Calling yourViewStub.visibility(View.VISIBLE) you do not need to check whether it is inflated or not.

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

A simpler approach in Kotlin:

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

You can use this in Kotlin .. I know that it uses a tag (this is not cool), but it is easy to use. this is the kotlin extension function:

 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) } 

}

and you can use it like this:

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

As follows from the documentation, we should not keep a long-lived link to the ViewStub and Android also allows Assigning an inflatedId to the ViewStub, which comes into existence as soon as it inflates

so that we can have a function signature such as 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) } 
0
source share

I solved this using flags . I declared the global boolean variable isInflatedBefore set initially to false

the code:

 //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 } 
-5
source share

All Articles