1page.title=Loading Views On Demand 2parent.title=Improving Layout Performance 3parent.link=index.html 4 5trainingnavtop=true 6previous.title=Re-using Layouts with <include/> 7previous.link=reusing-layouts.html 8next.title=Making ListView Scrolling Smooth 9next.link=smooth-scrolling.html 10 11@jd:body 12 13 14<div id="tb-wrapper"> 15<div id="tb"> 16 17<!-- table of contents --> 18<h2>This lesson teaches you to</h2> 19<ol> 20 <li><a href="#ViewStub">Define a ViewStub</a></li> 21 <li><a href="#Load">Load the ViewStub Layout</a></li> 22</ol> 23 24<!-- other docs (NOT javadocs) --> 25<h2>You should also read</h2> 26<ul> 27 <li><a href="http://android-developers.blogspot.com/2009/03/android-layout-tricks-3-optimize-with.html" 28 >Optimize with stubs (blog post)</a></li> 29</ul> 30 31</div> 32</div> 33 34 35<p>Sometimes your layout might require complex views that are rarely used. Whether 36they are item details, progress indicators, or undo messages, you can reduce memory usage and speed 37up rendering by loading the views only when they are needed.</p> 38 39 40<h2 id="ViewStub">Define a ViewStub</h2> 41 42<p>{@link android.view.ViewStub} is a lightweight view with no dimension and doesn’t draw anything 43or participate in the layout. As such, it's cheap to inflate and cheap to leave in a view hierarchy. 44Each {@link android.view.ViewStub} simply needs to include the {@code android:layout} attribute to 45specify the layout to inflate.</p> 46 47<p>The following {@link android.view.ViewStub} is for a translucent progress bar overlay. It should 48be visible only when new items are being imported into the application.</p> 49 50<pre> 51<ViewStub 52 android:id="@+id/stub_import" 53 android:inflatedId="@+id/panel_import" 54 android:layout="@layout/progress_overlay" 55 android:layout_width="fill_parent" 56 android:layout_height="wrap_content" 57 android:layout_gravity="bottom" /> 58</pre> 59 60 61<h2 id="Load">Load the ViewStub Layout</h2> 62 63<p>When you want to load the layout specified by the {@link android.view.ViewStub}, either set it 64visible by calling {@link android.view.View#setVisibility setVisibility(View.VISIBLE)} or call 65{@link android.view.ViewStub#inflate()}.</p> 66 67<pre> 68((ViewStub) findViewById(R.id.stub_import)).setVisibility(View.VISIBLE); 69// or 70View importPanel = ((ViewStub) findViewById(R.id.stub_import)).inflate(); 71</pre> 72 73<p class="note"><strong>Note:</strong> The {@link android.view.ViewStub#inflate()} method returns 74the inflated {@link android.view.View} once complete. so you don't need to call {@link 75android.app.Activity#findViewById findViewById()} if you need to interact with the layout.</p> 76 77<p>Once visible/inflated, the {@link android.view.ViewStub} element is no longer part of the view 78hierarchy. It is replaced by the inflated layout and the ID for the root view of that layout is 79the one specified by the {@code android:inflatedId} attribute of the ViewStub. (The ID {@code 80android:id} specified for the {@link android.view.ViewStub} is valid only until the {@link 81android.view.ViewStub} layout is visible/inflated.)</p> 82 83<p class="note"><strong>Note:</strong> One drawback of {@link android.view.ViewStub} is that it 84doesn’t currently support the {@code <merge>} tag in the layouts to be inflated.</p> 85 86 87 88