1. Preparatory work
Each view, just img Different, the others are the same
<?xml version= "1.0" encoding= "Utf-8"?><framelayout xmlns:android="Http://schemas.android.com/apk/res/android" Android:layout_width="Match_parent"android:layout_height="Match_parent" Android:orientation="vertical" > <imageview android: Layout_width = "match_parent" android:layout_ Height = "match_parent" android:background
= "#000000" //write a picture of each page you want to display android:src= "@drawable/image1" android:scaletype =< Span class= "Hljs-value" > "Fitcenter" /> <TextViewandroid:layout_width="Wrap_content"android:layout_height= "Wrap_content" android:layout_gravity="Bottom|center"android:text="swipe left Next"android: TextColor="#f0f0f0"/> </framelayout>
2. Main layout. Xml
<linearlayout 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"tools:context=". Mainactivity ">//here to do compatible <android. Support. V4. View. ViewpagerAndroid:id="@+id/pager"Android:layout_width="Wrap_content"android:layout_height="Wrap_content"android:layout_gravity="Center">//Here's the boot bar <android.. Support. V4. View. PagertabstripAndroid:id="@+id/tab"Android:layout_width="Wrap_content"android:layout_height="Wrap_content"android:layout_gravity="Top"> </android. Support. V4. View. Pagertabstrip> </android. Support. V4. View. Viewpager></LinearLayout>
3.activity.java inside the Code
Public class mainactivity extends Activity { PrivateViewpager Pager;//define a generic to store each view Privatelist<view>viewlist;PrivatePagertabstrip tab;@Override protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.layout.activity_main);//InitializeViewlist =NewArraylist<view> (); Pager = (Viewpager) Findviewbyid (R.id.pager); tab = (Pagertabstrip) Findviewbyid (R.id.tab);//Convert each XML layout file to a viewView View1 = View.inflate ( This, R.layout.view1,NULL); View View2 = View.inflate ( This, R.layout.view2,NULL); View view3 = View.inflate ( This, R.LAYOUT.VIEW3,NULL); View view4 = View.inflate ( This, R.LAYOUT.VIEW4,NULL);//Put the view in the array spaceViewlist.add (View1); Viewlist.add (VIEW2); Viewlist.add (VIEW3); Viewlist.add (VIEW4);//Set the properties of the boot bar, see the APITab.setbackgroundcolor (Color.Black); Tab.settextcolor (Color.White); Tab.setdrawfullunderline (false); Tab.settabindicatorcolor (Color.White);//This is a Viewpager adapter.Mypageradapter adapter =NewMypageradapter (viewlist);//Set the conversion animation, or do not set the conversion animation, so the effect of switching is very rigidPager.setpagetransformer (true,NewDepthpagetransformer ());//This is another conversion animation with different effects //pager.setpagetransformer (True, New Zoomoutpagetransformer ());Pager.setadapter (adapter); }}
4.viewpager adapter, there is nothing to note, the corresponding parameters are remembered how to fill the line, if you want to set the title add a Getpagetitle method, define a titlelist, the title of each page is written into the list
Public class mypageradapter extends pageradapter{ Privatelist<view>viewlist; Public Mypageradapter(list<view>viewlist) { This. viewlist=viewlist; }@Override Public int GetCount() {//TODO auto-generated method stub returnViewlist.size (); }@Override Public Boolean Isviewfromobject(View arg0, Object arg1) {//TODO auto-generated method stub returnARG0==ARG1; }@Override PublicObjectInstantiateitem(ViewGroup container,intPosition) {//TODO auto-generated method stubContainer.addview (Viewlist.get (position));returnViewlist.get (position); }@Override Public void Destroyitem(ViewGroup container,intPosition, Object object) {//TODO auto-generated method stubContainer.removeview (Viewlist.get (position)); }}
Principle:
Convert the XML file into a view put into the list generic, with Pageradapter adapter binding viewlist data source rendered, want to add animation Setpagetransformer parameter first is true, the second is an animation class, The API can also be found in one is Depthpagetransformer, one is Zoomoutpagetransformer
Android First Knowledge Viewpager