SoFunction
Updated on 2025-04-06

ViewPager usage guide in Android

viewPager is a control that is officially provided by Google to facilitate page sliding effect. It can be used directly or in combination with fragment. Let’s just talk about using it directly here.
The steps to use viewPager are as follows:

(1) Put the viewPager control in the layout
(2) Set the view loaded into the viewPager
(3) Write the adapter unique to viewPager
(4) Instantiate the viewPager and bind it to the adapter set in the previous step

This step also reflects the MVC idea. You can refer to my previous articlehttps:///article/

Here, for convenience, I created a new project to explain the use of viewPager

Step 1 Create a new viewPager in the layout

The layout file is as follows:

<.
    android:
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</.>

Set the view loaded into the viewPager

For convenience, display 2 views with different background colors and put them in the viewPager. The layout of each view is as follows:
 item_one

<LinearLayout xmlns:andro
       android:layout_width="match_parent"
       android:layout_height="match_parent"
  android:background="@android:color/holo_red_light">
</LinearLayout>

item_two

<LinearLayout xmlns:andro
       android:layout_width="match_parent"
       android:layout_height="match_parent"
  android:background="@android:color/holo_blue_bright">
</LinearLayout>

Write viewPager-specific adapter

Here we inherit the PagerAdapter of the viewPager, mainly rewrite the getCount method, destroyItem, instantiateItem, isViewFromObject, and. The code is as follows:

public class Adapter extends PagerAdapter{

    private List<View> views;

    public Adapter(List<View> views){
       = views;
    }
    @Override
    public int getCount() {
      return ();
    }

    @Override
    public void destroyItem(View arg0, int arg1, Object arg2) {
      ((ViewPager) arg0).removeView((arg1));
    }

    @Override
    public void finishUpdate(View arg0) {
    }
    @Override
    public Object instantiateItem(View arg0, int arg1) {
      ((ViewPager) arg0).addView((arg1), 0);
      return (arg1);
    }
    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
      return arg0 == (arg1);
//      return false;
    }
    @Override
    public void restoreState(Parcelable arg0, ClassLoader arg1) {
    }
    @Override
    public Parcelable saveState() {
      return null;
    }
    @Override
    public void startUpdate(View arg0) {
    }
  }

Instantiate the viewPager and bind the Adapter

Here, instantiate the viewPager to mpher , and then load the created view:item_one and item_two into the built View type array through LayoutInflater. Add the array to List<View>. Pass it into the adapter as a parameter. The code is as follows:

private ViewPager mpager;
  private List<View> myview = new ArrayList<>();
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    (savedInstanceState);
    setContentView(.activity_main);
    mpager = (ViewPager) findViewById(.view_pager);
    LayoutInflater mInflater = getLayoutInflater();
    View [] pagers = {(.item_one ,null),
              (.item_two , null)};

    for(int i = 0; i <  ; i++) {
      (pagers[i]);
    }
    Adapter ad = new Adapter(myview);
    (ad);
    (0);
  }

PS: Implement viewPager manual infinite loop

Here we use a way to set page jumps to achieve infinite loops. Add two views to the myviews array, add the last itemview at the 0th position, and add the first itemview at the last position. The code looks like this:

final View [] pagers = {(.item_one ,null),
              (.item_two , null),
              (.item_three,null)};

    for(int i = 0; i <  ; i++) {
      (pagers[i]);
    }
    (0,(.item_three , null));
    (4, (.item_one, null));

For the convenience of testing, here, an item was added to the previous one, which is three view loop playback.
Used to achieve connection. When sliding right to the last page, specify this page as the page where the first view is located, and when sliding left to the first page, set this page as the page where the last view is located. Note that the last view here is not the last one in the exponential array, but the last one of the three views.

Setting the instantiation object method of viewPager setOnPageChangeListener Although this method is not recommended to use, in order to implement this function, it is better to use it first. When Google updates the viewPager in the future, it will automatically implement the loop with attributes.
The code is as follows:

 (new () {
      @Override
      public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

      }

      @Override
      public void onPageSelected(int position) {
        int index = position;
        if(index == 0){
        //Note that the pagers array is not the myviews array          index = ;
        }else if(position ==  + 1){
          index = 1;
        }
        if(position != index){
          (index, false);
        }
      }

      @Override
      public void onPageScrollStateChanged(int state) {

      }
    });

Rewrite the instantiateItem method in Adapter to display the view

public Object instantiateItem(View arg0, int arg1) {
      if(arg1 == 0){
        ((ViewPager) arg0).removeView((() - 3));
        ((ViewPager) arg0).addView((() - 3), 0);
      }else if(arg1 == () - 1){
        ((ViewPager) arg0).removeView((0));
        ((ViewPager) arg0).addView((0), 0);

      }else{
        ((ViewPager) arg0).addView((arg1), 0);
      }
      return (arg1);
    }

In this way, infinite sliding can be achieved simply, but there will be bugs with blank pages when sliding, which may be because the view is removed first and the view is added.