Android Layout Tricks #3: Optimize with stubs
来源:互联网 发布:淘宝卖家恶意不发货 编辑:程序博客网 时间:2024/06/05 10:49
Sharing and reusing layouts is very easy with Android thanks to the <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 calledViewStub, which brings you all the benefits of the <include />
without polluting your user interface with rarely used views.
A ViewStub
is a dumb and lightweight view. It has no dimension, it does not draw anything and does not participate in the layout in any way. This means aViewStub
is very cheap to inflate and very cheap to keep in a view hierarchy. AViewStub
can be best described as a lazy include. The layout referenced by aViewStub
is inflated and added to the user interface only when you decide so.
The following screenshot comes from the 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 an 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 aViewStub
:
When the user initiates the import process, the ViewStub
is inflated and replaced by the content of the layout file it references:
To use a ViewStub
all you need is to specify an android:id
attribute, to later inflate the stub, and anandroid: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 included file. Finally, the layout parameters specified on the stub will be applied to the roof of the included 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 the inflate() method. You can also simply change the visibility of the stub to VISIBLE or INVISIBLE and the stub will inflate. Note however that the inflate()
method has the benefit of returning the rootView
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 removed from the view hierarchy. As such, it is unnecessary to keep a long-lived reference, for instance in an class instance field, to aViewStub
.
A ViewStub
is a great compromise between ease of programming and efficiency. Instead of inflating views manually and adding them at runtime to your view hierarchy, simply use aViewStub
. It's cheap and easy. The only drawback of ViewStub
is that it currently doesnot support the <merge /> tag.
Happy coding!
- Android Layout Tricks #3: Optimize with stubs
- Android Layout Tricks #3: Optimize with stubs(Android 布局技巧3:使用stub优化)
- Android Layout Tricks #3: Optimize, Part 1
- Android Layout Tricks #3: Optimize by merging
- Android Layout Tricks #3: Optimize by merging
- Android Layout Tricks #3: Optimize, Part 1
- Android Layout Tricks #4: Optimize, Part 2
- Android Layout Tricks #3: Optimize by merging(Android 布局技巧3:使用merge优化)
- Android Layout Tricks #1
- Android Layout Tricks #1(Android 布局技巧1)
- linux network optimize with sysctl
- linux network optimize with sysctl
- linux network optimize with sysctl
- Optimize map performamce with mapPartitions
- Android Tricks
- Tricks with delegates
- Android Layout Tricks #2: Reusing layouts(Android 布局技巧2:重用布局)
- Android Layout Tricks #1--因为空间被墙,所以转载过来
- sql servser按条件分类统计
- 黑马程序员——Java基础 GUI
- Ubuntu下NS3安装步骤
- 题目1056:最大公约数
- UITableViewCell的选中时的颜色设置
- Android Layout Tricks #3: Optimize with stubs
- BP 算法的一种直观的解释
- PKI常见证书格式和转换
- MFC 串口编程总结
- oracle函数参数中=>的含义
- 获取request请求路径信息
- 统计单词出现的次数
- (算法设计技巧与分析)Majority
- LeetCode #58 length of Last Word