Scott Main | 569ed22 | 2011-12-02 13:49:44 -0800 | [diff] [blame] | 1 | page.title=Loading Views On Demand |
Scott Main | 580f014 | 2011-12-15 16:47:26 -0800 | [diff] [blame] | 2 | parent.title=Improving Layout Performance |
Scott Main | 569ed22 | 2011-12-02 13:49:44 -0800 | [diff] [blame] | 3 | parent.link=index.html |
| 4 | |
| 5 | trainingnavtop=true |
| 6 | previous.title=Re-using Layouts with <include/> |
| 7 | previous.link=reusing-layouts.html |
| 8 | next.title=Making ListView Scrolling Smooth |
| 9 | next.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 |
| 35 | they are item details, progress indicators, or undo messages, you can reduce memory usage and speed |
| 36 | up 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 doesn’t draw anything |
| 42 | or participate in the layout. As such, it's cheap to inflate and cheap to leave in a view hierarchy. |
| 43 | Each {@link android.view.ViewStub} simply needs to include the {@code android:layout} attribute to |
| 44 | specify the layout to inflate.</p> |
| 45 | |
| 46 | <p>The following {@link android.view.ViewStub} is for a translucent progress bar overlay. It should |
| 47 | be visible only when new items are being imported into the application.</p> |
| 48 | |
| 49 | <pre> |
| 50 | <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" /> |
| 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 |
| 63 | visible 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 |
| 69 | View 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 |
| 73 | the inflated {@link android.view.View} once complete. so you don't need to call {@link |
| 74 | android.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 |
| 77 | hierarchy. It is replaced by the inflated layout and the ID for the root view of that layout is |
| 78 | the one specified by the {@code android:inflatedId} attribute of the ViewStub. (The ID {@code |
| 79 | android:id} specified for the {@link android.view.ViewStub} is valid only until the {@link |
| 80 | android.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 |
| 83 | doesn’t currently support the {@code <merge/>} tag in the layouts to be inflated.</p> |
| 84 | |
| 85 | |
| 86 | |