1. The Viewpager feature in Android is similar to the ScrollView in iOS, enabling the most basic page left and right swipe function. This class exists in Google's Compatibility Pack, so remember to include "Android-support-v4.jar" in Builldpath when referencing
2. In the implementation viewpager need to bind an adapter (Adapter), in the adapter returns the number of Page view and the specific form. This is similar to the implementation of TableView in iOS, you must implement Numberofcell and Cellforrow in the UITableView proxy, one for returning the total amount of data, and the other for returning each specific data.
3. Viewpager is usually used in conjunction with fragment. Add multiple fragment (pages) to the Viewpager to achieve left and right sliding.
4. Look at the simplest Viewpager code: Suppose we now have three fragment, their back color is Red,green, blue.
1) We add a viewpager to the XML file, the code is as follows
<relativelayout 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:paddingleft= "@dimen/activity_horizontal_margin" android:paddingright= "@dimen/activity_ Horizontal_margin " android:paddingtop=" @dimen/activity_vertical_margin " android:paddingbottom=" @dimen/ Activity_vertical_margin " tools:context=". MyActivity "> <android.support.v4.view.viewpager android:id=" @+id/mviewpager " android:layout _width= "Match_parent" android:layout_height= "match_parent" > </ Android.support.v4.view.viewpager></relativelayout>
2) in activtiy. This viewpager is used in Java. Now that we have viewpager, we need a adapter, usually we inherit fragmentpageradapter from the function that defines our adapter in order to rewrite the number of returns and item. Define an array in Adaoter, add the above fragment (if you want to dynamically add an added interface OK), the code is as follows
public static class Tabadapter extends Fragmentpageradapter implements Viewpager.onpagechangelistener {private arraylist<fragment> FRG = new arraylist<fragment> (); Public Tabadapter (fragmentmanager FM) {super (FM); Countingfragment f1 = new Countingfragment (); Frg.add (F1); Cursorfragment F2 = new Cursorfragment (); Frg.add (F2); Fragmentpagersupport F3 = new Fragmentpagersupport (); Frg.add (F3); Notifydatasetchanged (); } @Override public Fragment getItem (int i) {return frg.get (i); } @Override public int getcount () {return frg.size (); } @Override public void onpagescrolled (int i, float V, int. i2) {} @Override public voi d onpageselected (int i) {} @Override public void onpagescrollstatechanged (int i) {}}}In order to listen to the event of Viewpager movement change, we implemented the Viewpager.onpagechangelistener, its implementation of three functions to see the name will know what meaning. GetCount returns the number of fragment Viewpager, GetItem returns the item at each location.
3) associated Viewpgaer and adapter
Mviewpager = (Viewpager) Findviewbyid (R.id.mviewpager); Mtabadapter = new Tabadapter (Getsupportfragmentmanager ()); Mviewpager.setadapter (Mtabadapter);
Getsupportfragmentmanager returns a fragment manager, which isFragmentpageradapter the required parameters.
Through the above steps we have implemented a simple viewpager that can slide right and left.
Introduction to some properties
- when we need to use a key to control the left and right slide of the Viewpager, the properties
| boolean |
arrowscroll (int direction) Forward page parameter 1, page backward parameter 2 |
- Let the Viewpager not respond to sliding switching events, such as allowing nested sub-viewpager to not respond to a sliding switch event, just in response to a click-toggle event, handing the swipe event to the parent Viewpager. , using
| Boolean |
canscrollhorizontally(int direction) Check If this view can is scrolled horizontally in a certain direction. |
@Override Public boolean canscrollhorizontally (int direction) { //TODO auto-generated method stub return false; }
Android Viewpager should and tricks