1. We all know that for Viewpager build adapters Inherit Pageradapter, how to build it. Viewpager loads the current page and the left and right pages of the current page by default. At the beginning the current page is subscript 0, so at the beginning of the default load page No. 0 (subscript, below) and page 1th. When you slide to the right, when the current page is page 1th, Viewpager will load page 2nd, when a total of 3 pages exist (page 0,1,2). Then slide to the right, when the current page is page 2nd, the No. 0 page is removed, the 3rd page is loaded and the 3rd page is removed when you slide the current page to the left as page 1th. You should know that.
Knowing the above principle, you can let Viewpager always load only 3 pages of the picture. Loading is executing instantiateitem (...). method, remove is the execution destroyitem (...). method can be used in the instantiateitem (...). Add a picture of a page in destroyitem (...). ) to retrieve a picture of a page. Attached code:
2. code example:
Package com.example.test;
Import java.util.ArrayList;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.support.v4.view.PagerAdapter;
Import Android.support.v4.view.ViewPager;
Import Android.view.View;
Import Android.widget.ImageView;
public class Testactivity extends activity{private Viewpager Mpager;
Private arraylist<view> views;
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Views=new arraylist<view> ();
Views.add (This) (new ImageView);
Views.add (This) (new ImageView);
Views.add (This) (new ImageView);
Views.add (This) (new ImageView);
Views.add (This) (new ImageView);
Views.add (This) (new ImageView);
Views.add (This) (new ImageView);
Views.add (This) (new ImageView);
Mpager=new Viewpager (this);
Mpager.setadapter (New Mypageradapter ());
Setcontentview (Mpager); Class Mypageradapter extends pageradapter{@Override public void Destroyitem (View container, int position, Object Object) {//In this recycling picture//...
Recycle code//Removal page ((Viewpager) container). Removeview (Views.get (position)); @Override public Object Instantiateitem (View container, int position) {//pictures loaded////...
Load picture code//Load page ((Viewpager) container). AddView (Views.get (position), 0);
return Views.get (position);
@Override public int GetCount () {//TODO auto-generated a stub return views.size (); @Override public boolean isviewfromobject (View arg0, Object arg1) {//TODO auto-generated a stub return FA
Lse }
}
}