Viewpager is an API that can be used by Android 3.0 or more.
One, what can viewpager do?
1, 5.0 in the sliding with Viewpager can be easily achieved.
2, to achieve similar to the Sina Weibo navigation guide interface.
3. Implement other navigation or menus.
Second, the use of Viewpager
1. Add layout
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "Fill_ Parent " android:layout_height=" fill_parent " android:orientation=" vertical ">< Android.support.v4.view.ViewPager android:id= "@+id/viewpager" android:layout_width= "Wrap_content " android:layout_height= "Wrap_content" android:layout_gravity= "center"/> </LinearLayout>
2. Load the layout file to be displayed (View)
Layoutinflater lf = Getlayoutinflater (). from (this); View1 = lf.inflate (r.layout.layout_0, null); View2 = lf.inflate (r.layout.layout_1, null); VIEW3 = lf.inflate (r.layout.layout_2, NULL);
3. Create a View collection
Mlistviews = new arraylist<view> (); Mlistviews.add (view1); Mlistviews.add (VIEW2); Mlistviews.add (VIEW3);
4. Create Pageradapter
Viewpager adapter is pageradapter, to implement pageradapter at least to implement the following methods:
(1) Instantiateitem (viewgroup, int)//Add
(2) Destroyitem (viewgroup, int, Object)//delete
(3) GetCount ()
(4) Isviewfromobject (View, Object)
Private class MyPageAdapter extends Pageradapter { private list<view> mlistviews; Public MyPageAdapter (list<view> mlistviews) {this.mlistviews = mlistviews;} /** * Delete Page card */ @Override public void Destroyitem (View container, int position, object object) { ( Viewpager) container). Removeview (Mlistviews.get (position)); } /** * Instantiate page card * / @Override public Object instantiateitem (View container, int position) { ( Viewpager) container). AddView (Mlistviews.get (position)); return Mlistviews.get (position); } /** * Returns the number of page cards */ @Overridepublic int GetCount () {return mlistviews.size ();} @Overridepublic boolean isviewfromobject (View arg0, Object arg1) {return arg0 = = arg1;//official Prompt to write} }
5. Configure the adapter
Mviewpager.setadapter (New MyPageAdapter (mlistviews));
The full code is as follows:
Package Com.example.viewpagetest;import Java.util.arraylist;import Java.util.list;import android.app.Activity; Import Android.os.bundle;import Android.support.v4.view.pageradapter;import Android.support.v4.view.ViewPager; Import Android.view.layoutinflater;import Android.view.view;public class Mainactivity extends Activity {private List <View> mlistviews;private View View1;private view View2;private view View3;private Viewpager Mviewpager; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Mviewpager = (Viewpager) Findviewbyid (R.id.viewpager); Load the tab to be displayed layoutinflater LF = Getlayoutinflater (). from (this); View1 = lf.inflate (r.layout.layout_0, NULL); View2 = lf.inflate (r.layout.layout_1, NULL); VIEW3 = lf.inflate (r.layout.layout_2, NULL); Mlistviews = new arraylist<view> (); Mlistviews.add (View1); Mlistviews.add (VIEW2); Mlistviews.add (VIEW3); Mviewpager.setadapter (New MyPageAdapter (mlistviews)); } private class MyPageAdapter extends Pageradapter {private list<view> mlistviews; Public MyPageAdapter (list<view> mlistviews) {this.mlistviews = mlistviews;} /** * Delete Page card */@Override public void Destroyitem (View container, int position, object object) {(ViewPage r) container). Removeview (Mlistviews.get (position)); }/** * Instantiate page Card */@Override public Object instantiateitem (View container, int position) {(Vie Wpager) container). AddView (Mlistviews.get (position)); return Mlistviews.get (position); }/** * Returns the number of page cards */@Overridepublic int getcount () {return mlistviews.size ();} @Overridepublic boolean isviewfromobject (View arg0, Object arg1) {return arg0 = = arg1;//official Prompt to write}}}
Individual page code:
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/ Res/android " android:orientation=" vertical " android:layout_width=" match_parent " android:layout_ height= "Match_parent" > <textview android:layout_width= "match_parent" android:layout_height= " Match_parent " android:text=" 1 " android:gravity=" center " android:textsize=" 120sp " android: Textcolor= "#33b5e5" /></linearlayout>
Operating effect: