androidlayoutviewviewstub

Removing or Adding views vs ViewStub


What is the difference between .remove() .addView() and using ViewStub? How ViewStub increasing layout render efficiency?


Solution

  • As you can see in this article, when you use ViewStub, the layout that you <include /> inside the stub won't be inflated unless required (setting the visibility of the stub to VISIBLE or calling show()). The advantage is, none of the included view will be part of the view hierarchy, so your view is lighter.

    See this example: ViewStub not inflated: enter image description here

    After inflating: enter image description here

    Of course you have a tradeoff here, when you set VISIBLE (or show()) a reinflate (only on the ViewStub layout) happens.