Fragment supports the use of different activity and can handle its own input events and life cycle methods. Can be seen as a child activity.
First look at the layout:
1<LinearLayoutXmlns:android= "Http://schemas.android.com/apk/res/android"2Xmlns:tools= "Http://schemas.android.com/tools"3Android:layout_width= "Match_parent"4Android:layout_height= "Match_parent"5Tools:context=". Viewpageractivity ">67<Android.support.v4.view.ViewPager8android:id= "@+id/viewpager" android:layout_width = "wrap_content" 10 = "wrap_content" 11 Android:layout_gravity= "center" >12 </android.support.v4.view.viewpager>13 14 </linearlayout>
Main file: viewpageractivity inheritance Fragmentactivity
1PackageCom.example.viewpagerdemo;23ImportJava.util.ArrayList;4ImportJava.util.List;56ImportAndroid.os.Bundle;7ImportAndroid.support.v4.app.Fragment;8Importandroid.support.v4.app.FragmentActivity;9ImportAndroid.support.v4.view.ViewPager;1011ImportCom.example.adapter.fragmentAdapter;1213PublicClass ViewpageractivityExtendsfragmentactivity {1415@Override16ProtectedvoidOnCreate (Bundle savedinstancestate) {17Super. OnCreate (Savedinstancestate);18Setcontentview (R.layout.activity_view_pager);list<fragment> fragments =New arraylist<fragment>();20for (int i = 0; I < 3; i++) {Fragment f =new Buttonfragment (); Fragments.add (f); }24 viewpager VP = ( Viewpager) Findviewbyid (R.id.viewpager); 25 fragmentadapter fa = new Fragmentadapter (Getsupportfragmentmanager (), 26 Span style= "color: #000000;" > fragments); 27 Vp.setadapter (FA); }30}
New Fragmentadapter inheritance Fragmentpageradapter does not write the implementation method
1PackageCom.example.adapter;23ImportJava.util.List;45ImportAndroid.support.v4.app.Fragment;6ImportAndroid.support.v4.app.FragmentManager;7ImportAndroid.support.v4.app.FragmentPagerAdapter;89PublicClass FragmentadapterExtendsFragmentpageradapter {10Private list<fragment>fragments;11PrivateFragmentmanager FM;1213Public Fragmentadapter (Fragmentmanager FM, list<fragment>Fragments) {14Super(FM);15This.fragments =fragments;1617}1819PublicFragmentadapter (fragmentmanager FM) {20Super(FM);21stThis.fm =Fm22}2324@Override25public Fragment GetItem (int arg0) {26 return Fragments.get (arg0); 27 }28 29 @Override 30 public int GetCount () {31 Span style= "color: #0000ff;" >return Fragments.size (); }33 34}
Define the fragment content to be displayed, and place a button in it
1PackageCom.example.viewpagerdemo;23ImportAndroid.os.Bundle;4ImportAndroid.support.v4.app.Fragment;5ImportAndroid.view.LayoutInflater;6ImportAndroid.view.View;7ImportAndroid.view.ViewGroup;8ImportAndroid.widget.TextView;910PublicClass Buttonfragmentextends Fragment {11 Span style= "color: #0000ff;" >private View rootview; 12 13 @Override 14 public15 Bundle savedinstancestate) {16 Rootview = Inflater.inflate (R.layout.guide1, container, false); 17 return rootview;18 Span style= "color: #000000;" >}19}
The implementation of 3 contains a button can be switched left and right
Viewpager+fragment implementation of Android in the screen left and right switch (Advanced article)