更新支持库后,ViewPager显示不正确的页面

时间:2016-02-27 17:49:30

标签: android android-viewpager android-support-library

我将Google支持库更新为rev。 5x523.2.0显示错误的网页浏览量。
ViewPager使用自定义页面转换器:

ViewPager

并且有页边距:

private class ZoomOutPageTransformer implements ViewPager.PageTransformer {

    private static final float MIN_SCALE = 0.85f;

    private float mCenterOffset;
    private boolean mIsFixedCenterOffset = false;

    public void transformPage(View view, float position) {
        final int pageWidth = view.getWidth();
        final int pageHeight = view.getHeight();

        if(!mIsFixedCenterOffset) {
            mCenterOffset = position;
            mIsFixedCenterOffset = true;
        }

        position -= mCenterOffset;

        if (position <= 1) {
            final float scaleFactor = Math.max(MIN_SCALE, 1 - Math.abs(position));
            final float vertMargin = pageHeight * (1 - scaleFactor) / 2;
            final float horzMargin = pageWidth * (1 - scaleFactor) / 2;

            if (position < 0) {
                view.setTranslationX(horzMargin - vertMargin / 2);
            } else {
                view.setTranslationX(-horzMargin + vertMargin / 2);
            }

            view.setScaleX(scaleFactor);
            view.setScaleY(scaleFactor);
        }
    }
}

现在,它看起来如下:
enter image description here 我甚至不知道bug在哪里......如何解决?我需要页面填充父空间并仅在滚动时缩小。
LogCat中的警告:

  

W / FragmentManager:moveToState:DayFragment的片段状态{24ec179#2 id = 0x7f0c00b4}未内联更新;预期状态3发现2

ADDED 我意识到了什么是错误。 ViewPager viewPager = (ViewPager) v.findViewById(R.id.vpDaySlider); mAdapter = new DayPagerAdapter(getActivity(), fm, mWeekNumber); viewPager.setAdapter(mAdapter); viewPager.addOnPageChangeListener(this); viewPager.setPageTransformer(false, new ZoomOutPageTransformer()); Point display = SettingsManager.getDisplaySize(getContext()); final int padding = (int)(display.x * 0.1); viewPager.setPageMargin(padding); 修复了页面的第一个位置。并且它必须是0或接近零,但在更新后的第一个位置值&gt; 1(例如我手机上的1.1)但下一个(第二个)值是实际页面位置(对于我的手机:0)。
因为发生了什么?我认为这是ZoomOutPageTransformer的错误行为。

1 个答案:

答案 0 :(得分:3)

我修复了这个错误:

private static class ZoomOutPageTransformer implements ViewPager.PageTransformer {

        private static final float MIN_SCALE = 0.85f;

        private ViewPager mViewPager;
        private float mPositionFixer;
        private boolean isSetFixer = false;

        public  ZoomOutPageTransformer(ViewPager viewPager) {
            mViewPager = viewPager;
        }

        public void transformPage(View view, float position) {
            final int pageWidth = view.getWidth();
            final int pageHeight = view.getHeight();

            if(!isSetFixer) {
                final int mClientWidth = mViewPager.getMeasuredWidth() -
                        mViewPager.getPaddingLeft() - mViewPager.getPaddingRight();
                mPositionFixer = ((float)mViewPager.getPaddingStart()) / mClientWidth;
                isSetFixer = true;
            }

            position -= mPositionFixer;

            if (position <= 1) {
                final float scaleFactor = Math.max(MIN_SCALE, 1 - Math.abs(position));
                final float vertMargin = pageHeight * (1 - scaleFactor) / 2;
                final float horzMargin = pageWidth * (1 - scaleFactor) / 2;

                if (position < 0) {
                    view.setTranslationX(horzMargin - vertMargin / 2);
                } else {
                    view.setTranslationX(-horzMargin + vertMargin / 2);
                }

                view.setScaleX(scaleFactor);
                view.setScaleY(scaleFactor);
            }
        }
    }

虽然我无法理解支持库rev中的这个错误。 23.2.0。