layoutinflater function and useRole:
1. For an interface that is not loaded or wants to be loaded dynamically, it needs to be loaded using inflate.
2, for an already loaded activity, you can use the implementation of this Activiyt Findviewbyid method to obtain the interface elements.
Method:
When you want to create a screen in Android, beginners are generally creating a new class, inheriting the activity base class, and then using the Setcontentview method in OnCreate to load an interface that is defined in XML.
In fact, the activity inside the use of Layoutinflater to load the interface, through
getsystemservice (context.layout_inflater_service) Method can be obtained by a layoutinflater, or by
Layoutinflater inflater =getlayoutinflater (); To get it. Then use the Inflate method to load the XML in Layout,
<Relativelayoutxmlns: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" > <ViewswitcherAndroid:id= "@+id/viewswitcher1"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent" > </Viewswitcher> <ButtonAndroid:id= "@+id/button2"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_alignparentbottom= "true"Android:layout_alignparentleft= "true"Android:onclick= "Prev"Android:text= "<" /> <ButtonAndroid:id= "@+id/button1"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_alignparentbottom= "true"Android:layout_alignparentright= "true"Android:onclick= "Next"Android:text= ">" /> </Relativelayout>
PackageCom.example.viewswitcher;Importjava.util.ArrayList;Importandroid.app.Activity;Importandroid.graphics.drawable.Drawable;ImportAndroid.os.Bundle;ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;ImportAndroid.widget.BaseAdapter;ImportAndroid.widget.GridView;ImportAndroid.widget.ImageView;ImportAndroid.widget.TextView;ImportAndroid.widget.ViewSwitcher;Importandroid.widget.ViewSwitcher.ViewFactory; Public classMainactivityextendsActivity { Public Static Final intNumber_per_screen=12; Public Static classdataitem{ PublicString Dataname; Publicdrawable drawable; } PrivateArraylist<dataitem> items=NewArraylist<dataitem>(); Private intScreenno=-1; Private intScreencount; Viewswitcher switcher; Layoutinflater Inflater; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Inflater=layoutinflater.from (mainactivity. This); //Create 40 Applications for(inti=0;i<40;i++) {String label=""+i; Drawable drawable=getresources (). getdrawable (R.drawable.ic_launcher); DataItem Item=NewDataItem (); Item.dataname=label; Item.drawable=drawable; Items.Add (item); } Screencount=items.size ()%number_per_screen==0?items.size ()/NUMBER_PER_SCREEN:items.size ()/number_per_screen+1; Switcher=(Viewswitcher) Findviewbyid (r.id.viewswitcher1); Switcher.setfactory (Newviewfactory () {@Override PublicView Makeview () {returnInflater.inflate (R.layout.slidelistview,NULL); } }); Next (NULL); } Public voidNext (View v) {if(screenno<screencount-1) {Screenno++; Switcher.setinanimation ( This, R.anim.slide_in_right); Switcher.setoutanimation ( This, R.anim.slide_out_left); (GridView) Switcher.getnextview ()). Setadapter (adapter); Switcher.shownext (); } } Public voidprev (View v) {if(screenno>0) {Screenno--; Switcher.setinanimation ( This, Android. R.anim.slide_in_left); Switcher.setoutanimation ( This, Android. R.anim.slide_out_right); (GridView) Switcher.getnextview ()). Setadapter (adapter); Switcher.showprevious (); } } PrivateBaseadapter adapter=NewBaseadapter () {@Override Public intGetCount () {if(Screenno==screencount-1 && items.size ()%number_per_screen!=0){ returnItems.size ()%Number_per_screen; } returnNumber_per_screen; } @Override PublicDataItem GetItem (intposition) { returnItems.get (screenno*number_per_screen+position); } @Override Public LongGetitemid (intposition) { returnposition; } @Override PublicView GetView (intposition, View Convertview, ViewGroup parent) {View View=Convertview; if(convertview==NULL) {View=inflater.inflate (R.layout.labelicon,NULL); } ImageView ImageView=(ImageView) View.findviewbyid (R.id.imageview); Imageview.setimagedrawable (GetItem (position). drawable); TextView TextView=(TextView) View.findviewbyid (R.id.textview); Textview.settext (GetItem (position). Dataname); returnview; } };}
Here the use of Viewswitcher, as well as animation effects, there is a system to bring, but also created by themselves. XML ellipsis
Here's an introduction to the XML meaning of animation effects
<Setxmlns:android= "Http://schemas.android.com/apk/res/android"> <TranslateAndroid:fromxdelta= "0"Android:toxdelta= "100%"android:duration= "+"/> <AlphaAndroid:fromalpha= "0.0"Android:toalpha= "1.0"android:duration= "+" /></Set>
which
Translate position transfer animation effect
integer value:
&NBSP ; Fromxdelta property is the position of the x-coordinate at the start of the animation
Toxdelta Property is the position of the x-coordinate at the end of the animation
Fromydelta properties for animations The position at the start of the y-coordinate is
Toydelta property is the position of the y-coordinate at the end of the animation
& nbsp NOTE: ,
&NB Sp No designation, &NBSP,
&N Bsp default is to use their own relative reference
Long value:
duration Properties for animation duration
Description: The time is measured in milliseconds
In these properties you can also add% and P, for example:
Android:toxdelta= "100%", which means 100% of itself, starting from view's own position.
Android:toxdelta= "80%p", which represents 80% of the parent view, is referenced by its parent view.
Android 24th Lesson--viewswitcher