SoFunction
Updated on 2025-04-10

Android implements ViewPager infinite loop effect (I)

This example shares the specific code for Android to implement the infinite loop of ViewPager for your reference. The specific content is as follows

Method 1:

Implementation principle:

Suppose there are 3 pictures, 1, 2, 3, respectively, then create 5 pictures. The order of these 5 pictures is: 3, 1, 2, 3, 1, 2, 3 are the pictures we want to slide, the leftmost 3 and the rightmost 1 are the pictures we added. At the beginning, picture 1 is displayed. When the picture slides to the left, it is 1, 2, 3 in turn. When the third picture continues to slide to the left, we will add more picture 1. At this time, set the current index to the position where the real picture 1 is.

package ;

import ;
import ;
import .;
import ;
import ;
import ;
import ;
import ;

import ;
import ;
import ;
import ;

import ;
import ;

/**
 * Created by XiaoMai on 2016/10/7 17:19.
 * 
 */
public class LoopViewPagerDemoActivity extends BaseActivity {

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

 private ViewPager mViewPager;

 private LoopViewPagerAdapter mLoopViewPager;

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

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

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

 /**
   * Instruction Point
   */
 private LinearLayout mLinearLayoutPoints;

 private Handler mHandler = new Handler();

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

 @Override
 protected void onResume() {
  ();
  (myRunnable, UPDATE_TIME);
 }

 @Override
 protected void onPause() {
  ();
  (myRunnable);
 }

 Runnable myRunnable = new Runnable() {
  @Override
  public void run() {
   (mCurrentPageIndex + 1);
  }
 };

 @Override
 protected void initVariables() {
  ();
 }

 @Override
 protected void initViews() {
  mLinearLayoutPoints = (LinearLayout) findViewById(.activity_ll_container);
  mViewPager = (ViewPager) findViewById(.activity_loop_viewpager);
 }

 @Override
 protected void loadData() {
  ();
  (new Runnable() {
   @Override
   public void run() {
    /**
      * Initialize the resource id of the image
      */
    mImageIds = new int[]{
      .ic_launcher,
      .ic_launcher,
      .my_toast,
      .my_toast};
    /**
      * Add ImageView
      */
    mImageList = new ArrayList<>();
    //Add two additional pictures    int pagerCount =  + 2;
     layoutParams =
      new (
        .MATCH_PARENT,
        .WRAP_CONTENT);
    for (int i = 0; i < pagerCount; i++) {
     ImageView imageView = new ImageView(mContext);
     (layoutParams);
     (imageView);
    }
    //Diameter of dot    int diameter = Utils.dip2px(mContext,10f);
     params =
      new (diameter, diameter);
    int margin = Utils.dip2px(mContext,5f);
    (margin,margin,margin,margin);
    ();
    for (int i = 0; i < (); i++) {
     View view = new View(mContext);
     (params);
     if (i != 0 && i != () - 1) {
      (.circle_normal);
     }
     (view);
    }
    ();
    (new () {
     @Override
     public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

     }

     @Override
     public void onPageSelected(int position) {
      (myRunnable);
      (myRunnable, UPDATE_TIME);
      mCurrentPageIndex = position;
      if (position == 0) {
       // When the view is in the first, set the page number to the last picture.       mCurrentPageIndex = ;
      } else if (position ==  + 1) {
       // When the view is at the last, set the page number to the first picture.       mCurrentPageIndex = 1;
      } else {
      }
      /**
        * When the view is in the first or last, the values ​​of pageIndex and position are not equal.
        * So you need to change the current item of viewPager.
        */
      if (position != mCurrentPageIndex) {
       /**
         * The second parameter must be set to false, which means transitioning immediately and not giving the user a visual effect.
         */
       (mCurrentPageIndex, false);
       return;
      }
      for (int i = 1; i < () - 1; i++) {
       if (i != mCurrentPageIndex){
        (i).setBackgroundResource(.circle_normal);
       }else {
        (mCurrentPageIndex).setBackgroundResource(
          .circle_select);
       }
      }

     }

     @Override
     public void onPageScrollStateChanged(int state) {

     }
    });
    /**
      * When the ViewPager is pressed, cancel the automatic scrolling
      * On the contrary, turn on automatic sliding.
      */
    (new () {
     @Override
     public boolean onTouch(View v, MotionEvent event) {
      if(() == MotionEvent.ACTION_DOWN){
       (myRunnable);
      }else if (() == MotionEvent.ACTION_UP){
       (myRunnable, UPDATE_TIME);
      }
      return false;
     }
    });
    mLoopViewPager = new LoopViewPagerAdapter(mImageList, mImageIds);
    (mLoopViewPager);
    /**
      * Because the 0th item is the added image, the current item must be set to 1 during initialization.
      */
    (1);
    dissMissProgressDialog();
   }
  }, 3 * 1000);

 }

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

package ;

import .;
import ;
import ;
import ;

import ;

/**
 * Created by XiaoMai on 2016/10/7 17:23.
 */
public class LoopViewPagerAdapter extends PagerAdapter {

 private List<ImageView> mImageList;

 private int[] mImageIds;

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

 @Override
 public int getCount() {
  return mImageList == null ? 0 : ();
 }

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

 @Override
 public Object instantiateItem(ViewGroup container, int position) {
  if (position == 0) {
   // If it is the 0th item, set its image content to the last image content   (position).setImageResource(mImageIds[-1]);
  } else if (position == (() - 1)) {
   // If it is the last item, set its image content to the first image content   (position).setImageResource(mImageIds[0]);
  } else {
   // This is a normal picture   (position).setImageResource(mImageIds[position - 1]);
  }
  ((position));
  return (position);
 }

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

Layout file

<?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.