SoFunction
Updated on 2025-04-11

Android ViewPager achieves image turnover effect

Many apps have this effect, especially some e-commerce apps, which turn the page to the right every few seconds on the top to display the next picture, for promotion or content display. Let’s simply imitate it today, and it also comes with a small function of automatic jumping (there are several small dots at the bottom, and the state of the dots is also changing when the picture moves). It is not difficult to implement it with a timer.

import ; 
import ; 
 
import ; 
import ; 
import ; 
import ; 
import ; 
import .; 
import .; 
import .; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
 
public class MainActivity extends Activity implements OnPageChangeListener { 
  
 protected static final String TAG = "MainActivity"; 
 private List<ImageView> imageViewList; 
 private ViewPager mViewPager; 
 private final int[] imageResIDs = { 
   , 
   , 
   , 
   , 
    
 }; 
 private final String[] imageDescriptions = { 
   "Gong Li is not vulgar, so I can't be vulgar", 
   "The tree is back! Singing classic old songs to attract thousands of people to sing", 
   "Revealing the secret of how Beijing movies are upgraded", 
   "LeTV TV version delivery", 
   "The hot-blooded loser's counterattack" 
 }; 
 private TextView tvImageDescription; // Picture description private LinearLayout llPointGroup;  // Group of point controls private int previousPosition = 0;  // The previous position selected by viewpager private boolean isStop = false; // Control whether the child thread of the loop is stopped  
 private Handler handler = new Handler(){ 
 
  @Override 
  public void handleMessage(Message msg) { 
   (msg); 
    
   (() + 1); 
  } 
 }; 
 
 @Override 
 protected void onCreate(Bundle savedInstanceState) { 
  (savedInstanceState); 
  setContentView(.activity_main); 
   
  initView(); 
   
  new Thread(new Runnable() { 
 
   @Override 
   public void run() { 
    // Send a message to the main thread every two seconds to switch the viewpager interface    while(!isStop) { 
     (2000); 
      
     (0); 
    } 
    (TAG, "The loop thread has stopped"); 
   }}).start(); 
 } 
  
 @Override 
 protected void onDestroy() { 
  isStop = true; 
  (); 
 } 
 
 private void initView() { 
  mViewPager = (ViewPager) findViewById(); 
  llPointGroup = (LinearLayout) findViewById(.ll_point_group); 
  tvImageDescription = (TextView) findViewById(.tv_image_description); 
   
  imageViewList = new ArrayList<ImageView>(); 
   
  ImageView iv; 
  View pointView; 
  LayoutParams params; 
  for (int i = 0; i < ; i++) { 
   iv = new ImageView(this); 
   (imageResIDs[i]); 
   (iv); 
    
   // Add points according to the picture   pointView = new View(this); 
   params = new LayoutParams(5, 5); 
    = 5; 
   (params); 
   (false); 
   (.point_bg); 
   (pointView); 
  } 
   
  MyPagerAdapter mAdapter = new MyPagerAdapter(); 
  (mAdapter); 
  (this); 
   
  int item = (Integer.MAX_VALUE / 2) - ((Integer.MAX_VALUE / 2) % ()); 
  (item);  // Set the position of the currently selected item   
  (imageDescriptions[previousPosition]); 
  (previousPosition).setEnabled(true); // The first point is selected } 
  
  
 class MyPagerAdapter extends PagerAdapter { 
 
  @Override 
  public int getCount() { 
   return Integer.MAX_VALUE; 
  } 
 
  /**
    * If the moving object and the incoming object are the same, it will return true, which means the multiplexed view object.
    * false Use object object
    */ 
  @Override 
  public boolean isViewFromObject(View arg0, Object arg1) { 
   return arg0 == arg1; 
  } 
 
  /**
    * The position of the object that needs to be destroyed is transmitted in
    */ 
  @Override 
  public void destroyItem(ViewGroup container, int position, Object object) { 
   // Remove the object with the specified position   ((position % ())); 
  } 
 
  /**
    * Load the view object at the position position
    */ 
  @Override 
  public Object instantiateItem(ViewGroup container, int position) { 
   // Add an object with the specified position   ((position % ())); 
   return (position % ()); 
  } 
 } 
 
 
 @Override 
 public void onPageScrollStateChanged(int arg0) { 
  // TODO Auto-generated method stub 
   
 } 
 
 @Override 
 public void onPageScrolled(int arg0, float arg1, int arg2) { 
  // TODO Auto-generated method stub 
   
 } 
 
 /**
   * Callback when viewpager page is switched
   * @param position is the page currently displayed
   */ 
 @Override 
 public void onPageSelected(int position) { 
  (imageDescriptions[position % ()]); 
  (position % ()).setEnabled(true); 
  (previousPosition).setEnabled(false); 
   
  previousPosition = position % (); 
 } 
} 

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.