InfiniteCycleViewPager icon indicating copy to clipboard operation
InfiniteCycleViewPager copied to clipboard

Fix bringToFront leads to a wrong children index in the scrolling viewpager

Open HokoFly opened this issue 6 years ago • 0 comments

When scrolling a ViewPager or redrawing the dirty rectangle of a ViewPager, sometimes one page disappears. It is caused when the page transformer call the bringToFront function, the children view index will be changed. See the viewpager source code.

protected void onPageScrolled(int position, float offset, int offsetPixels) {
        ...
        dispatchOnPageScrolled(position, offset, offsetPixels);

        if (mPageTransformer != null) {
            final int scrollX = getScrollX();
            final int childCount = getChildCount();
            for (int i = 0; i < childCount; i++) {
                final View child = getChildAt(i);
                final LayoutParams lp = (LayoutParams) child.getLayoutParams();

                if (lp.isDecor) continue;
                final float transformPos = (float) (child.getLeft() - scrollX) / getClientWidth();
                mPageTransformer.transformPage(child, transformPos);
            }
        }

        mCalledSuper = true;
    }

When the viewpager is scrolling, final View child = getChildAt(i); will be called, and the index may be changed by calling bringToFront function right now.

HokoFly avatar Feb 11 '19 09:02 HokoFly