Devlight / InfiniteCycleViewPager

Infinite cycle ViewPager with two-way orientation and interactive effect.
Apache License 2.0
5.75k stars 827 forks source link

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

Closed HokoFly closed 1 year ago

HokoFly commented 5 years ago

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.