private void scrollToPage(int indexPage, boolean reset) {
        int periodLength = getPeriodLength(getWidth());
        if (mPager.mCurrentPosition != indexPage) {
            long currentTime = System.currentTimeMillis();
            // If it scroll too fast, just go back to the current position or it looks weird.
            boolean changeable = Math.abs(getScrollX() - mPager.mCurrentPosition * periodLength) <= 2 * periodLength
                    && currentTime - mLastScrollTime > 300;
            if (changeable || reset) {
                mPager.updateCurrentPosition(indexPage);
                mLastScrollTime = currentTime;
                if (!mOnPageChangeListeners.isEmpty()) {
                    for (OnPageChangeListener onPageChangeListener : mOnPageChangeListeners) {
                        onPageChangeListener.onPageSelected(getLoopIndex(indexPage));
                    }
                }
            }
        }
        int dx = mPager.mCurrentPosition * periodLength - getScrollX();
        if (Math.abs(dx) < periodLength * 3 / 2) {
            mScroller.startScroll(getScrollX(), 0, dx, 0, Math.abs(dx));
            invalidate();
        } else {
            scrollTo(mPager.mCurrentPosition * periodLength, 0);
        }
        requestLayout();
    }
A banner view that can automatically or manually scroll in a loop
Created by Marksss on 2019/8/12.
imitate/src/main/java/com/engineer/imitate/ui/widget/opensource/InfiniteBannerView.java