Today, let's look at Viewpager, which is a very useful control. Gossip does not say, directly into the subject:
To construct the Viewpager, first of all, note that Viewpager is present in the V4 Compatibility Pack of Android:
Android.jar is our main SDK, and Android.support.v4.jar is the Compatibility pack that Android has created for the high-version SDK-compatible low-version system. Viewpager is a control, and textview a level, its initial method to use to adapter, we naturally associate to the ListView. The biggest difference between this and the ListView is that it can be initialized with 3 adapter:
(1) Pageradapter: For initializing view,
(2) Fragmentpageradapter: Used to initialize Fragment,
(3) Fragmentstatepageradapter: Similar to Fragmentpageradapter, but provides more comprehensive creation and destruction capabilities.
Next we will introduce each one, first put a:
1. To use Viewpager, you first need to introduce controls in the 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= "Match_parent " android:layout_height= "Match_parent" > <android.support.v4.view.pagertabstrip android:id= "@+id/ Pagertabstrip " android:layout_width=" match_parent " android:layout_height=" wrap_content "> < /android.support.v4.view.pagertabstrip> </android.support.v4.view.viewpager></linearlayout >
Viewpager is our control, Pagertabstrip is the title.
2. Once the control is introduced, as with the ListView, we need to add data to it. Just introduced, add data a total of 3 adapter, first to introduce the simplest kind of pageradapter.
We need to inherit the pageradapter and then rewrite the method inside:
Package Com.xiaoming.demo01_viewpager;import Java.util.list;import Android.support.v4.view.pageradapter;import Android.util.log;import Android.view.view;import Android.view.viewgroup;public class MyPagerAdapter extends Pageradapter {private static final String TAG = "Mypageradapter";p rivate list<view> mviewlist;private list< String> mtitlelist;public mypageradapter (list<view> viewlist, list<string> titlelist) {this.mViewList = Viewlist;this.mtitlelist = Titlelist;} @Overridepublic int GetCount () {return mviewlist.size ();} @Overridepublic Boolean isviewfromobject (view view, Object obj) {return view==obj;} @Overridepublic Object Instantiateitem (viewgroup container, int position) {Container.addview (Mviewlist.get) ); LOG.D (TAG, "Instantiateitem," + "position:" +position); return mviewlist.get (position);} @Overridepublic void Destroyitem (ViewGroup container, int position, object object) {Container.removeview (mviewlist.get (position)); LOG.D (TAG, "Destroyitem," + "position:" +position);} @Overridepublic charsequence getpagetitle (int position) {return mtitlelist.get (position);}}
Mviewlist is the view list that is passed in to be displayed, initialized in mainactivity:
Mtitlelist is the title to be displayed for each page that is passed in, initialized in mainactivity:
Here are a few ways to rewrite it:
(1) GetCount (): Get total Pages
(2) Isviewfromobject (view view, Object obj): Determines whether the view is the specified obj, directly referring to the official Api:return view==obj;
(3) Instantiateitem (ViewGroup container, int position): Used to initialize the contents of the container:
First added to the container: Container.addview (mviewlist.get (position));
Then return the displayed View:return mviewlist.get (position);
(4) Destroyitem (ViewGroup container, int position, Object object): Removes the object and destroys the object:
Container.removeview (Mviewlist.get (position));
(5) Getpagetitle (int position): Gets the title of the corresponding page: return mtitlelist.get (position);
The 5 methods are simple, and after rewriting we can load the Viewpager:
Mviewpager = (Viewpager) Findviewbyid (R.id.viewpager); Mypageradapter myadapter = new Mypageradapter (viewlist, titlelist); Mviewpager.setadapter (Myadapter);
So it's done. If you want to listen to viewpager changes in time, you can give mainactivity implementation onpagechangelistener, and then rewrite these three methods, we here with the simplest toast to prompt:
@Overridepublic void onpagescrollstatechanged (int arg0) {} @Overridepublic void onpagescrolled (int arg0, float arg1, int a RG2) {} @Overridepublic void onpageselected (int arg0) {showtoast ("current is the first" + arg0 + "page");}
3. When you're done with Pageradapter, we'll cover viewpager content with fragment filling:
As with view, you first need to initialize a list of fragment (as far as fragment is not introduced here):
Then write a adapter inheritance to Fragmentpageradapter:
package com.xiaoming.demo01_viewpager;import Java.util.list;import Android.support.v4.app.fragment;import Android.support.v4.app.fragmentmanager;import Android.support.v4.app.fragmentpageradapter;public class Myfragmentpageadapter extends Fragmentpageradapter { Private list<fragment> mlistfragment;private list<string> mlisttitle;public MyFragmentPageAdapter ( Fragmentmanager fm,list<fragment> listfragment, list<string> listtitle) {super (FM); this.mListFragment = Listfragment;this.mlisttitle = ListTitle;} @Overridepublic Fragment getItem (int position) {return mlistfragment.get (position);} @Overridepublic int GetCount () {return mlistfragment.size ();} @Overridepublic charsequence getpagetitle (int position) {return mlisttitle.get (position);}}
The implementation of the method is mainly two less: Instantiateitem () and Destroyitem (), because in the use of fragment and Viewpager combination, the first load of Viewpager, will be all the fragment loaded into the And know that the fragment in the fragment list will not be destroyed until Viewpager is destroyed. However, when using Pageradapter, a system that loads 3 view in memory dynamically creates and deletes more than 3 parts. Face reading Code also has log, you can refer to a look.
Used in mainactivity:
Myfragmentpageadapter fadapter = new Myfragmentpageadapter (Getsupportfragmentmanager (), fragmentlist, titlelist); Mviewpager.setadapter (Fadapter);
It is important to note that the first parameter of initialization fragmentpageadapter is Fragmentmanager, which is used to manage fragmnet. There are two cases of obtaining fragmentmanager:
(1) If you use import android.support.v4.app.Fragment; It is the fragment in the V4 package that needs to use the activity's Getsupportfragmentmanager () to get Fragmentmanager.
(2) If you use import android.app.Fragment; Is the genuine fragmnet, compatibility is not so good, the direct use of activity Getfragmentmanager () to get Fragmentmanager.
4. Fill with Fragmentstatepageradapter:
Package Com.xiaoming.demo01_viewpager;import Java.util.list;import Android.support.v4.app.fragment;import Android.support.v4.app.fragmentmanager;import Android.support.v4.app.fragmentstatepageradapter;import Android.view.viewgroup;public class Myfragmentstatepageadapter extends Fragmentstatepageradapter {private List< fragment> mlistfragment;private list<string> mlisttitle;public myfragmentstatepageadapter (FragmentManager Fm,list<fragment> listfragment, list<string> listtitle) {super (FM); this.mlistfragment = listFragment; This.mlisttitle = ListTitle;} @Overridepublic Fragment getItem (int position) {return mlistfragment.get (position);} @Overridepublic int GetCount () {return mlistfragment.size ();} @Overridepublic charsequence getpagetitle (int position) {return mlisttitle.get (position);} @Overridepublic void Destroyitem (ViewGroup container, int position, object object) {Super.destroyitem (container, Position, object);} @Overridepublic Object Instantiateitem (ViewGroup arg0, int arg1) {return Super.instantiateitem (arg0, arg1);}}
Using the same method as using Fragmentpageradapter is exactly the same, that is, the multiple write two methods, and all are used by the implementation of the parent class:
@Overridepublic void Destroyitem (ViewGroup container, int position, object object) {Super.destroyitem (container, Position, object);} @Overridepublic Object Instantiateitem (viewgroup arg0, int arg1) {return Super.instantiateitem (arg0, arg1);}
So fragmentstatepageradapter more than Fragmentpageradapter function is the ability to dynamically manage the display and destruction of fragment.
To summarize:
Fill in the contents of Viewpager in a total of 3 ways:
(1) Use Pageradapter, the simplest, convenient, and view combination. The content of pages in Viewpager can be created and destroyed dynamically.
(2) Use of Fragmentpageradapter, using fragmnet as Viewpager content, fragment life cycle more controllable, so more recommended. However, the contents of pages in Viewpager cannot be created and destroyed dynamically. But that's not necessarily a good thing!
(3) Fragmentstatepageradapter like the combination of Pageradapter and fragmentpageradapter, and the use of fragment, but also to achieve dynamic loading content.
All right, it's over!!
The following code can be affixed:
http://download.csdn.net/detail/u013647382/8329741
Viewpager How to use