blob: 0fa2855e4a1c676a975879b97f52e92ac43a355e [file] [log] [blame]
Scott Main569ed222011-12-02 13:49:44 -08001page.title=Loading Views On Demand
Scott Main580f0142011-12-15 16:47:26 -08002parent.title=Improving Layout Performance
Scott Main569ed222011-12-02 13:49:44 -08003parent.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="{@docRoot}resources/articles/layout-tricks-stubs.html">Using ViewStubs</a></li>
28</ul>
29
30</div>
31</div>
32
33
34<p>Sometimes your layout might require complex views that are rarely used. Whether
35they are item details, progress indicators, or undo messages, you can reduce memory usage and speed
36up rendering by loading the views only when they are needed.</p>
37
38
39<h2 id="ViewStub">Define a ViewStub</h2>
40
41<p>{@link android.view.ViewStub} is a lightweight view with no dimension and doesnt draw anything
42or participate in the layout. As such, it's cheap to inflate and cheap to leave in a view hierarchy.
43Each {@link android.view.ViewStub} simply needs to include the {@code android:layout} attribute to
44specify the layout to inflate.</p>
45
46<p>The following {@link android.view.ViewStub} is for a translucent progress bar overlay. It should
47be visible only when new items are being imported into the application.</p>
48
49<pre>
50&lt;ViewStub
51 android:id="@+id/stub_import"
52 android:inflatedId="@+id/panel_import"
53 android:layout="@layout/progress_overlay"
54 android:layout_width="fill_parent"
55 android:layout_height="wrap_content"
56 android:layout_gravity="bottom" /&gt;
57</pre>
58
59
60<h2 id="Load">Load the ViewStub Layout</h2>
61
62<p>When you want to load the layout specified by the {@link android.view.ViewStub}, either set it
63visible by calling {@link android.view.View#setVisibility setVisibility(View.VISIBLE)} or call
64{@link android.view.ViewStub#inflate()}.</p>
65
66<pre>
67((ViewStub) findViewById(R.id.stub_import)).setVisibility(View.VISIBLE);
68// or
69View importPanel = ((ViewStub) findViewById(R.id.stub_import)).inflate();
70</pre>
71
72<p class="note"><strong>Note:</strong> The {@link android.view.ViewStub#inflate()} method returns
73the inflated {@link android.view.View} once complete. so you don't need to call {@link
74android.app.Activity#findViewById findViewById()} if you need to interact with the layout.</p>
75
76<p>Once visible/inflated, the {@link android.view.ViewStub} element is no longer part of the view
77hierarchy. It is replaced by the inflated layout and the ID for the root view of that layout is
78the one specified by the {@code android:inflatedId} attribute of the ViewStub. (The ID {@code
79android:id} specified for the {@link android.view.ViewStub} is valid only until the {@link
80android.view.ViewStub} layout is visible/inflated.)</p>
81
82<p class="note"><strong>Note:</strong> One drawback of {@link android.view.ViewStub} is that it
83doesnt currently support the {@code &lt;merge/&gt;} tag in the layouts to be inflated.</p>
84
85
86