SoFunction
Updated on 2025-04-07

Android ViewPager small dot indicator

A very commonly used function, a ViewPager will automatically scroll, and there are a row of small dots black and white to indicate the current scrolling progress.

First write a ViewPager adapter. Here, for the convenience of all the elements inside this adapter are ImageView

import ;
import ;
import .;
import .;
import ;
import ;
import ;
import ;
import ;
import ;
 
 
 
/**
 * Created by Administrator on 2016/2/24.
 */
public class HomeHomeBannerAdapter extends PagerAdapter{
    private Context context;
    private ImageView[] eventImageViews;
    private String[] eventUrls;
    int destWidth,destHeight;
 
 
    public HomeHomeBannerAdapter(Context context, String[] eventUrls,int destWidth,int destHeight) {
         = context;
         = eventUrls;
         = destHeight;
         = destWidth;
        initImageViews();
    }
 
    /**
      * Initialize several pictures in the viewPager
      */
    private void initImageViews(){
        if(eventUrls==null)return;
        eventImageViews = new ImageView[];
        for (int i=0;i<;i++) {
            eventImageViews[i] = new ImageView(context);
            eventImageViews[i].setLayoutParams(new (destWidth, destHeight));
            eventImageViews[i].setPadding(0, 0, 0, 0);
            eventImageViews[i].setScaleType(.FIT_XY);
            (context,eventImageViews[i],eventUrls[i]);
        }//Show pictures    }
 
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        if (eventImageViews != null &&  > position && position >= 0)
            (eventImageViews[position]);
 
    }
 
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        (eventImageViews[position], 0);
        return eventImageViews[position];
    }
 
    @Override
    public int getCount() {return eventUrls==null?0:;}
 
    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == arg1;
    }
//ViewPager listener is integrated into internal class    static public class EventViewPagerChangeListener implements  {
        LinearLayout llGuideGroup;
        int oldEventPosition;
        int currentItem;
 
        public EventViewPagerChangeListener(LinearLayout llGuideGroup){
             = llGuideGroup;
        }
 
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}
 
        /**
          * Control the display of white or black for small dots
          * @param position
          */
        public void onPageSelected(int position) {
            (oldEventPosition).setBackgroundResource(.dot_normal);//Black dot            (position).setBackgroundResource(.dot_focused);//White dots            oldEventPosition = position;
            currentItem = position;
        }
 
        @Override
        public void onPageScrollStateChanged(int state) {
 
        }
    }
 
    /**
      * Timed tasks that control the automatic sliding of viewpager
      */
    public static class ScrollTask implements Runnable {
        EventViewPagerChangeListener listener;
        ViewPager vpEvent;
        int eventSize;
        Handler handler;
        public ScrollTask(EventViewPagerChangeListener listener,final ViewPager vpEvent, int eventSize){
             = listener;
             = vpEvent;
             = eventSize;
            handler = new Handler();
        }
        public void run() {
            if(listener==null||vpEvent==null||eventSize==0)return;
             = ( + 1) % eventSize;
            ("Alex","currentItem is"+);
            (new Runnable() {
                @Override
                public void run() {
                    (); // Switch pictures through Handler                }
            });
        }
    }
 
    public static class FixedSpeedScroller extends Scroller {
        private static final int mDuration = 400;
        private int eventCount;
 
        public FixedSpeedScroller(Context context, Interpolator interpolator,int eventCount) {
            super(context, interpolator);
             = eventCount;
        }
 
        @Override
        public void startScroll(int startX, int startY, int dx, int dy, int duration) {
            if (duration % 100 == 0 && duration > 0) {
                //"It's automatic stroke now"                if (duration / 100 == eventCount) (startX, startY, dx, dy, 1);//If it's the last one                else (startX, startY, dx, dy, mDuration);
 
            } else {
                // "Now it's manual stroke"                (startX, startY, dx, dy, 80);
            }
        }
    }
}

There is a listener in the inner class of the above adapter. This listener has a member LinearLayout llGuideGroup. This linear layout contains several small dots. The following is the definition of this layout:

<RelativeLayout
        android:
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:visibility="gone">
 
        <.
            android:
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:background="@color/black666666" />
 
        <LinearLayout
            android:
            android:layout_width="match_parent"
            android:layout_height="10dp"
            android:layout_alignParentBottom="true"
            android:layout_marginBottom="10dp"
            android:layout_centerHorizontal="true"
            android:gravity="center"
            android:orientation="horizontal" >
 
        </LinearLayout>
</RelativeLayout>

Set the number of small dots and initialize the listener

/**
      * Add small dots to a linear layout, and the specific control logic is in the listener
      * @param llGuideGroup
      * @param count How many small dots to add
      */
    public EventViewPagerChangeListener addViewPagerDots(LinearLayout llGuideGroup,ViewPager vpEvents,int count){
        if(llGuideGroup==null||vpEvents==null||count&lt;1)return null;
         lp = new (15, 15);
         = 5;
         = 5;
        for(int i=0;i&lt;count;i++){
            ImageView imageView = new ImageView(());
            (lp);
            (i==0?.dot_focused:.dot_normal);
            (imageView);
        }
        //The monitor that controls the display of small dots        EventViewPagerChangeListener listener = new EventViewPagerChangeListener(llGuideGroup);
        (listener);
        return listener;
    }

Timed page switching through multithreading

 listener = (,,);//Add small dots for indication                    // When the Activity is displayed, switch the image display every 3 seconds                    ScheduledExecutorService scheduledExecutorService = ();
                    (new (listener,,), 3, 3, );

Setting a custom scroller to reduce the switching speed of viewPager

/**
      * Set a custom scroller for ViewPager to reduce the default scrolling speed
      * @param vpEvent
      */
    public void setViewPagerScroller(ViewPager vpEvent){
        if(vpEvent==null)return;
        Field mField;
        Scroller mScroller;
        try {
            mField = ("mScroller");
            (true);
            mScroller = new ((), new AccelerateInterpolator(),());
            try {
                (vpEvent, mScroller);
            } catch (IllegalAccessException e) {
                ();
            }
        } catch (NoSuchFieldException e) {
            ();
        }
    }

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.