Objective
Follow the recent blog's route and continue to talk about Android UI development. Today, let's talk about Viewstub controls, lazy load controls. In this blog, you'll learn about common properties, methods, and considerations for viewstub. Finally, we will demonstrate the use of Viewstub with a simple demo.
Viewstub
Before introducing Viewstub, you can look at the <include/> tag, which is a way to include other layout resources in a particular layout, in the development of many platforms, there are similar concepts, so the benefits do not need to elaborate, The main is the layout control that can edit the interface in different files.
Viewstub, lazy load control. Viewstub is an invisible, 0-size view that can be inflate into a layout resource by lazy loading mode during program run. When a viewstub inflate () method is called or set to display, the viewstub uses the set view to be loaded and replaces the current viewstub position. Therefore, viewstub exists at the view level until the setvisibility (int) or inflate () method is called, otherwise the control is not loaded, so it consumes less resources. It is also often called "lazy include".
The advantage of viewstub is that in some scenarios, it is not necessary to show all the content, you can hide some view views, when users need to show the time to load into the current layout, this time can be used to viewstub this control, which can reduce the consumption of resources , so that the initial loading speed becomes faster.
In Viewstub, there are two properties that need to be used, and Android also provides the appropriate Getter/setter method for it:
- Android:inflateid: Overrides the ID of the Viewstub parent layout control.
- Android:layout: Sets the ID of the layout control viewstub is inflate.
For Viewstub, the event that it needs to listen to is only one, the event that is triggered when the load is Viewstub.oninflatelistener, in this event, a oninflate () method needs to be implemented, the following is the signature of this method:
Oninflate (viewstub stub, View inflated);
In the Oninflate () method of the Viewstub.oninflatelistener event, the stub is the Viewstub control that is currently being inflated, and the inflated parameter is the currently expanded view view, where you can do some extra work.
In the process of using viewstub, one thing needs special attention. For a Viewstun, when the setvisibility (int) or inflate () method is called, the viewstub is replaced with the specified view in the layout, so inflate viewstub over again, If you want to show again after being hidden, you will not be able to use the inflate () method, but you can use the setvisibility (int) method again to set it to visible, which is the difference between the two methods. When inflate () is called, the parent layout control object is returned.
Sample Demo
Here is a demo to explain the above mentioned content. In this demo, you will simply use the <include/> tab and define a viewstub control that loads a Ratingbar, provides buttons to load and hides the Ratingbar, Finally, a button is provided to manipulate the dynamically loaded Ratingbar.
The inflate control code that is defined:
1 <?xml version= "1.0" encoding= "Utf-8"?> 2 <linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "3 android:layout_width=" match_parent "4 android:layout_height=" Match_parent "5 Android:o rientation= "Vertical" > 6 7 <ratingbar 8 android:id= "@+id/ratingbar1" 9 android:layout_width = "Wrap_content" android:layout_height= "wrap_content"/>11 </LinearLayout>
Layout code:
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Android:orien tation= "vertical" android:paddingbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_ Horizontal_margin "android:paddingright=" @dimen/activity_horizontal_margin "android:paddingtop=" @dimen/activity_ Vertical_margin "tools:context=". Mainactivity > <!--loading a control with an ID of start using the include tag--<include android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" layout= "@layout/start"/> <linearlayout android:layout_ Width= "Wrap_content" android:layout_height= "wrap_content" android:background= "#FFCCDD" android:orient ation= "Vertical" > <!--define a viewstub to its parent layout with a specified ID of Inflatedstart--<viewstub Androi D:id= "@+id/stub" Android:inflatedid= "@+id/inflatedstart" android:layout_width= "Wrap_content" Android:layout_h eight= "Wrap_content" android:layout= "@layout/start"/> </LinearLayout> <button Android : id= "@+id/btn1" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:text = "Dynamically add Layout"/> <button android:id= "@+id/btn2" android:layout_width= "Wrap_content" Android:layo ut_height= "Wrap_content" android:text= "Dynamic Hide Layout"/> <button android:id= "@+id/btn3" Android:lay Out_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "Operation Stubinflate Controls"/></Line Arlayout>
Implementation code:
1 package Com.bgxt.viewstubdemo; 2 3 Import Android.os.Bundle; 4 Import Android.annotation.SuppressLint; 5 Import android.app.Activity; 6 Import Android.view.Menu; 7 Import Android.view.View; 8 Import Android.view.View.OnClickListener; 9 Import android.view.viewstub;10 Import android.view.viewstub.oninflatelistener;11 import android.widget.button;12 Import android.widget.linearlayout;13 import android.widget.ratingbar;14 import android.widget.toast;15 Class Mainactivity extends Activity {btn1 private Button, btn2,btn3;18 private viewstub viewstub;19 @Ove Rride21 protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Etcontentview (R.layout.activity_main); 24//Get control, bind event BTN1 = (Button) Findviewbyid (R.ID.BTN1); 26 BTN2 = (Button) Findviewbyid (r.id.btn2), Btn3 = (button) Findviewbyid (R.ID.BTN3); viewstub = (viewstub ) Findviewbyid (r.id.stub); viewstuB.setoninflatelistener (Inflatelistener); Btn1.setonclicklistener (click); Btn2.setonclicklistener (click ): Btn3.setonclicklistener (click),}34 oninflatelistener inflatelistener=new oninflateliste NER () {PNS @Override38 public void Oninflate (viewstub stub, View inflated) {+//In Flaye viewstub when displayed in Toast.maketext (Mainactivity.this, "Viewstub is loaded!", Toast.length_short). Show (); 41 }42};43 the private View.onclicklistener click = New Onclicklistener () {@Override46 public void OnClick (View v) {V.getid ()) {$ case r.id.btn1:49 try {50 If not been inflate, use inflate expansion of LinearLayout layout= (linearlayout) viewstub.inflate (); Ratingbar bar= (Ratingbar) Layout.findviewbyid (R.ID.RATINGBAR1); S (4); 54} catch (Exception e) {55//If using inflate expansion error, it means that it has been inflated, using the Setvisibility method to display the Tub.setvisibility (view.visible); break;59 r.id.b tn2:61//Hide ViewStub62 viewstub.setvisibility (view.gone); Case R.ID.BTN3:65//operation is inflate control, need to get the current layout of the object 66//Then through this object to find the control inflate. 67//Because otherwise in this example, the control introduced by the include tag is found LinearLayout linearlayout= (linearlayout) Findviewbyid (R . Id.inflatedstart); Ratingbar rbar= (Ratingbar) Linearlayout.findviewbyid (R.ID.RATINGBAR1); 70 float numstart=rbar.getrating (); numstart++;72 if (numstart>4) 73 { numstart=0;75}76 rbar.setrating (Numstart); ; 78}79 80}81 };82 @Override84 Public boolean Oncreateoptionsmenu (Menu menu) {$//inflate the menu; this adds I TEMs to the Action bar if it is present.86 getmenuinflater (). Inflate (R.menu.main, menu); return true;88 }89 90}
Effect Show:
SOURCE download
Please support the original, respect for the original, reproduced please indicate the source. Thank you.
The viewstub of Android--ui