30 March 2009
Android layout tricks #3: optimize with stubs
Http://android-developers.blogspot.com/2009/03/android-layout-tricks-3-optimize-with.html
Sharing and reusing layouts is very easy with Android thanks to
<Include/> tag, sometimes even too easy and you might end up with user interfaces that contain a large number of views, some of which are rarely used. thankfully, Android offers a very special widget called
Viewstub, which brings you all the benefits of
<include />
Without polluting your user interface with rarely used views.
AViewStub
Is a dumb and lightweight view. It has no dimension, it does not draw anything and does not participant in the layout in any way. This means
ViewStub
Is very cheap to inflate and very cheap to keep in a view hierarchy.
ViewStub
Can be best described asLazy include. The layout referenced by
ViewStub
Is inflated and added to the user interface only when you decide so.
The following screenshot comes from
Shelves application. The main purpose of the activity shown in the screenshot is to present the user with a browsable list of books:
The same activity is also used when the user adds or imports new books. during such an operation, shelves shows extra bits of user interface. the screenshot below shows the progress bar and cancel button that appear at the bottom of the screen during
Import:
Because importing books is not a common operation, at least when compared to browsing the list of books, the import panel is originally represented by
ViewStub
:
When the user initiates the import process,ViewStub
Is inflated and replaced by the content of the layout file it references:
To useViewStub
All you need is to specifyandroid:id
Attribute, to later inflate the stub, and
android:layout
Attribute, to reference what layout file to include and inflate. A stub lets you use a third attribute,
android:inflatedId
, Which can be used to override the ID of the root of the specified ded file. finally, the layout parameters specified on the stub will be applied to the roof the defined layout. here is an example:
<ViewStub android:id="@+id/stub_import" android:inflatedId="@+id/panel_import" android:layout="@layout/progress_overlay" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" />
When you are ready to inflate the stub, simply invoke
Inflate () method. You can also simply change the visibility of the stub
Visible or
Invisible and the stub will inflate. Note however thatinflate()
Method has the benefit of returning the root
View
Of the inflate layout:
((ViewStub) findViewById(R.id.stub_import)).setVisibility(View.VISIBLE);// orView importPanel = ((ViewStub) findViewById(R.id.stub_import)).inflate();
It is very important to remember that after the stub is inflated, the stub is
RemovedFrom the View hierarchy. As such, it is unnecessary to keep a long-lived reference, for instance in an class instance field, to
ViewStub
.
AViewStub
Is a great compromise between ratio of programming and efficiency. Instead of inflating views manually and adding them at runtime to your view hierarchy, simply use
ViewStub
. It's cheap and easy. The only drawbackViewStub
Is that it currently does
NotSupport
<Merge/> tag.
Happy coding!
Posted byromain guyat2: 00
PMLabels: How-to, optimization, user
Interface