SoFunction
Updated on 2025-04-10

Android implements ViewPager infinite loop effect (II)

This example shares with you the second way to implement the infinite loop effect of ViewPager on Android for your reference. The specific content is as follows

Principle: Set getCount to infinite size in Adapter

package ;

import ;
import .;
import ;
import ;
import ;

import ;
import .LoopViewPagerAdapter2;
import ;

import ;
import ;

/**
 * Created by XiaoMai on 2016/10/9 17:44.
 * 
 */
public class LoopViewPagerDemo2Activity extends BaseActivity {

  /**
    * Time to update the picture
    */
  private static final int UPDATE_TIME = 3 * 1000;

  private ViewPager mViewPager;

  private LoopViewPagerAdapter2 mAdapter;

  /**
    * Instruction Point
    */
  private LinearLayout mLinearLayoutPonits;

  /**
    * Image resource collection
    */
  private int[] mImageIds;

  /**
    * Picture collection
    */
  private List<ImageView> mImageList;

  /**
    * The index of the current ad
    */
  private int mCurrentPageIndex;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    (savedInstanceState);

  }

  @Override
  protected void initVariables() {
    ();
    /**
      * Initialize the resource id of the image
      */
    mImageIds = new int[]{
        .ic_launcher,
        .ic_launcher,
        .ic_launcher,
        .ic_launcher,
        .my_toast,
        .my_toast,
        .my_toast,
        .my_toast};
    /**
      * Add ImageView
      */
    mImageList = new ArrayList<>();
    //Add two additional pictures     layoutParams =
        new (
            .MATCH_PARENT,
            .WRAP_CONTENT);
    for (int i = 0; i < ; i++) {
      ImageView imageView = new ImageView(mContext);
      (layoutParams);
      (imageView);
    }
  }

  @Override
  protected void initViews() {
    mLinearLayoutPonits = (LinearLayout) findViewById(.activity_ll_container);
    mViewPager = (ViewPager) findViewById(.activity_loop_viewpager);
    mAdapter = new LoopViewPagerAdapter2(mImageList, mImageIds);
    (mAdapter);
    int middle = () / 2;
    (middle - middle % ());
  }

  @Override
  protected int getContentLayout() {
    return .act_loopviewpager;
  }
}

Code:

package ;

import .;
import ;
import ;
import ;

import ;

/**
 * Created by XiaoMai on 2016/10/9 17:53.
 */
public class LoopViewPagerAdapter2 extends PagerAdapter {

  private List<ImageView> mImageList;

  private int[] mImageIds;

  public LoopViewPagerAdapter2(List<ImageView> mImageList, int[] mImageIds) {
     = mImageList;
     = mImageIds;
  }

  @Override
  public int getCount() {
    return Integer.MAX_VALUE;
  }

  @Override
  public boolean isViewFromObject(View view, Object object) {
    return view == object;
  }

  @Override
  public Object instantiateItem(ViewGroup container, int position) {
    ImageView imageView = (position % ());
    (mImageIds[position % ]);
    (imageView);
    return (position % ());
  }

  @Override
  public void destroyItem(ViewGroup container, int position, Object object) {
    ((position % ()));
  }
}

act_loopviewpager.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:andro
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical">

  <include
    android:
    layout="@layout/my_app_bar" />

  <.
    android:
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:layout_below="@id/titleBar" />

  <LinearLayout
    android:
    android:layout_width="match_parent"
    android:orientation="horizontal"
    android:gravity="center"
    android:background="@color/toast"
    android:layout_alignBottom="@id/activity_loop_viewpager"
    android:visibility="gone"
    android:layout_height="40dp"/>
</RelativeLayout>

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.