Apa itu <ViewStub/>
ViewStub adalah sebuah view yang tak terlihat dan berukuran nol dan sangat cocok untuk lazily inflate layout resources saat run time. Jadi ViewStub ini sangatlah irit memori sehingga sangat pas sekali untuk optimasi layout. Begitulah kira-kira.
Perbedaan <ViewStub/> dan <Include/>
Lalu apa bedanya dengan include. Sekilas memang sama, tapi sakjane beda lah.
The <include /> will just include the xml contents in your base xml file as if the whole thing was just a single big file. It’s a nice way to share layout parts between different layouts.
The <ViewStub/> is a bit different because it is not directly included, and will be loaded only when you actually use it/need it, ie, when you set its visibility to VISIBLE (actually visible) or INVISIBLE (still not visible, but its size isn’t 0 anymore). This a nice optimization because you could have a complex layout with tons of small views or headers anywhere, and still have your Activity load up really fast. Once you use one of those views, it’ll be loaded.
Kemudian gambaran betapa ngiritnya ViewStub ini dapat kita lihat pada gambar berikut:
Penggunaan ViewStub
0 1 2 3 4 |
<ViewStub android:id="@+id/stub" android:inflatedId="@+id/subTree" android:layout="@layout/mySubTree" android:layout_width="120dip" android:layout_height="40dip" /> |
Dengan ViewStub ini kita dapat melakukan inflating ketika dibutuhkan saja.
0 1 |
ViewStub stub = (ViewStub) findViewById(R.id.stub); View inflated = stub.inflate(); |
Referensi:
http://developer.android.com/reference/android/view/ViewStub.html
http://android-developers.blogspot.co.id/2009/03/android-layout-tricks-3-optimize-with.html