API Documentation:
The function of the Viewpager is to slide the view (unlike the Viewflipper's auto-slide, which is to be manually slid), just as the lanucher slides around.
So we can use it to make an app boot interface.
As follows:
Implementation steps:
1. Create Viewpager in the main interface layout.
2. Load the view of each interface.
3, set Pageradapter.
Attention:
When you implement a pageradapter, you must overwrite at least the following methods:
instantiateItem(ViewGroup, int)
destroyItem(ViewGroup, int, Object)
getCount()
isViewFromObject(View, Object)
Implementation code:
Mainactivity:
public class Mainactivity extends Activity {private Viewpager mviewpager;private View view1, View2, View3;private list< View> viewlist; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.viewpager); Initview (); Pageradapter mpageradapter = new Pageradapter () {@Override public boolean isviewfromobject (View arg0, Object arg1) {return arg0 = = Arg1; } @Override public int getcount () {return viewlist.size (); } @Override public void Destroyitem (ViewGroup container, int position, object object) { Container.removeview (Viewlist.get (position)); } @Override public int getitemposition (Object object) {return Super.getitemposition (object ); } @Override Public Object instantiateitem (viewgroup container, int position) { Container.addview (Viewlist.get (position)); return Viewlist.get (position); } }; Mviewpager.setadapter (Mpageradapter);} void Initview () {Mviewpager = (Viewpager) Findviewbyid (r.id.viewpager); viewlist = new arraylist<view> (); Layoutinflater layoutinflate = Getlayoutinflater (). from (this); view1 = Layoutinflate.inflate (R.LAYOUT.LAYOUT1, NULL); View2 = layoutinflate.inflate (r.layout.layout2, null); VIEW3 = Layoutinflate.inflate (R.LAYOUT.LAYOUT3, NULL); Viewlist.add (View1); Viewlist.add (VIEW2); Viewlist.add (VIEW3);p ageradapter);}
2. Main layout: Main.xml
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "Match_ Parent " android:layout_height=" match_parent " android:orientation=" vertical "> < Android.support.v4.view.ViewPager android:id= "@+id/viewpager" android:layout_width= "Match_parent " android:layout_height= "Match_parent" android:layout_gravity= "center" > </ Android.support.v4.view.viewpager> </LinearLayout>
3, each loaded sub-layout, here only one, the other two layout code just like the name is different.
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:id=" @+id/layout1 " android:layout_width=" match_parent " android:layout_height=" Match_parent " android:orientation=" vertical "> <imageview android:id=" @+id/iamge " Android : layout_width= "match_parent" android:layout_height= "match_parent" android:src= "@drawable/A"/></ Linearlayout>
The above is an entry-level example, followed by a few more in-depth technical examples.
Android's Viewpager