Viewstub Lazy Load Control

Source: Internet
Author: User

First, we introduced the <include> tag, the label can again layout file reference another layout file, this method is fixed in the layout file import, not very convenient to use.
The functionality of the viewtsub is similar to that of <include>, and it also implements referencing another layout. But the only difference is that viewstub does not immediately load the reference layout file, only the Viewstub.inflate or viewstub.setvisibility (View.visibile) method is called to load the referenced control.

Second, let's take a real-viewstub explanation:

In the Main.xml file:

1<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"2Xmlns:tools= "Http://schemas.android.com/tools"3Android:layout_width= "Match_parent"4android:layout_height= "Match_parent"5android:orientation= "Vertical" >6<!--statically load layout files--7<include8Android:id= "@+id/include1"9Android:layout_width= "Fill_parent"Tenandroid:layout_height= "Wrap_content" Onelayout= "@layout/next"/> A<!--load layout files dynamically-- -<viewstub -Android:id= "@+id/viewstub" theAndroid:layout_width= "Fill_parent" -android:layout_height= "Wrap_content" -android:layout= "@layout/next"/> -<LinearLayout +Android:layout_width= "Fill_parent" -android:layout_height= "Wrap_content" +Android:layout_marginend= "1DP" Aandroid:orientation= "Horizontal" > at<Button -Android:id= "@+id/button1" -Android:layout_width= "0DP" -android:layout_height= "Wrap_content" -android:layout_weight= "1" -android:text= "Loading"/> in<Button -Android:id= "@+id/button2" toAndroid:layout_width= "0DP" +android:layout_height= "Wrap_content" -android:layout_weight= "1" theandroid:text= "Hide"/> *</LinearLayout> $</LinearLayout>

In the Next.xml file:

1 <?xml version= "1.0" encoding= "Utf-8"?>2 <relativelayout xmlns:android= "/http Schemas.android.com/apk/res/android "3     android:layout_width=" Match_parent "4     android: layout_height= "Match_parent" >5     <ratingbar6         android:id= "@+id/ RatingBar1 "7         android:layout_width=" wrap_content "8         android:layout_height=" Wrap_ Content "/>9 </RelativeLayout>

In the. java file:

1 Privateviewstub stub;2     PrivateButton Button1,button2;3 @Override4     protected voidonCreate (Bundle savedinstancestate) {5         Super. OnCreate (savedinstancestate);6 Setcontentview (r.layout.main);7stub =(viewstub) Findviewbyid (r.id.viewstub);8Button1 =(Button) Findviewbyid (r.id.button1);9Button2 =(Button) Findviewbyid (r.id.button2);TenButton1.setonclicklistener (NewOnclicklistener () { One              A @Override -              Public voidOnClick (View v) { -                 //TODO Auto-generated method stubs theView view =stub.inflate (); Inflate () is a Viewstub object that loads Next.xml into the Viewstub container, and only infalte this statement can implement lazy loading controls -Relativelayout layout =(relativelayout) view; The view return value is the View object of the Next.xml file, which is obtained by casting the layout object in the Next.xml file, and the child controls are available through the layout object Ratingbar -              -Ratingbar bar =(Ratingbar) Layout.findviewbyid (R.ID.RATINGBAR1); +Bar.setnumstars (3); -Toast.maketext (mainactivity. This, "Layout.getid ();" +View.getid (), Toast.length_long). Show (); +             } A         }); at          -Button2.setonclicklistener (NewOnclicklistener () { -              - @Override -              Public voidOnClick (View v) { -                 //TODO Auto-generated method stubs in stub.setvisibility (stub. GONE);//Hide Layout -             } to         }); +          -Stub.setoninflatelistener (NewOninflatelistener () { the              * @Override $              Public voidoninflate (viewstub stub1, View inflated) {Panax Notoginseng                 //TODO Auto-generated method stubs -Toast.maketext (mainactivity. This, "Getlayoutresource" +Stub.getlayoutresource (), Toast.length_long). Show ();//getresource//The value is next The. XML (r.layout.next) ID value
 *
Operation Result:
Note: Viewstub can not be repeated inflate, can only inflate once note:

Viewstub Lazy Load Control

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.