Pageradapter Introduction
Pageradapter is the class in the ANDROID.SUPPORT.V4 package, its subclasses have Fragmentpageradapter, Fragmentstatepageradapter, These two adapter are fragment adapters, used to achieve the fragment sliding effect, the use of the two adapter and the difference is not introduced this time.
The Pageradapter is primarily a viewpager adapter, And Viewpager is also in the ANDROID.SUPPORT.V4 expansion package added a powerful control, you can implement the sliding effect of the control, such as our software common in the banner of the sliding effect, with the Viewpager can be achieved. Today, we mainly introduce how to use VIEWPAGR and rewrite pageradapter to achieve the sliding effect of common banner.
Introduction to Pageradapter Usage
First, if you inherit Pageradapter, you must override the following four methods at a minimum:
1. Instantiateitem (ViewGroup, int.)
2. Destroyitem (viewgroup, int, Object)
3. GetCount ()
4. Isviewfromobject (View, Object)
Below we describe the meaning of these four methods and how to use them in the form of a code:
Private classViewpageradapterextendsPageradapter {//get the number of controls to slide, here we take the sliding banner as an example, then this should be the number of ImageView of the displayed ad image@Override Public intGetCount () {returnimages.size (); } //to determine whether the same picture is displayed, here we compare two parameters to return@Override Public BooleanIsviewfromobject (View arg0, Object arg1) {returnarg0 = =arg1; } //Pageradapter only caches three images to be displayed, and if the sliding picture goes beyond the cached range, this method is called to destroy the image .@Override Public voidDestroyitem (ViewGroup view,intposition, Object object) {View.removeview (Images.get (position)); } //when the image to be displayed can be cached, this method is called to initialize the display image, and we will add the ImageView to the ViewGroup and return it as the return value .@Override PublicObject Instantiateitem (ViewGroup view,intposition) {View.addview (Images.get (position)); returnImages.get (position); } }
Pageradapter Summary in Android