Android 4.0.x 和 4.1.x viewPager 错误?

Android 4.0.x and 4.1.x viewPager bug?

我在 android API 15 和 16 上使用 viewPager 时遇到问题,我的应用程序基本上是简单的遥控器,用于具有三个屏幕的机顶盒,您可以在第一个屏幕上选择哪个机顶盒你想要控制,第二个和第三个只是带有控件的屏幕。现在的问题是,在 android 的所有其他版本上一切正常,但在 API 15 和 16 上,当应用程序启动时,当我点击它检测到的任何地方时,我无法使用带有框的屏幕第二个屏幕上的操作是控件,当我滑动到第三个屏幕然后返回时,再次调用第一个屏幕的 onCreateView 并且一切正常。这是第一屏和第二屏的图像 这是我的 viewPager 代码:

public class SectionsPagerAdapter extends FragmentPagerAdapter {

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            // getItem is called to instantiate the fragment for the given page.
            // Return a PlaceholderFragment (defined as a static inner class below).
            DisplayMetrics metrics = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(metrics);
            switch (position) {
                case 0:
                    return boxesFragment;
                case 1:
                    return miscpadFragment;
                case 2:
                    if (metrics.densityDpi == DisplayMetrics.DENSITY_MEDIUM) {
                        return volPadFragment;
                    } else return numberPadFragment;
                case 3:
                    return numberPadFragment;
            }
            return null;
        }

        @Override
        public int getCount() {
            DisplayMetrics metrics = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(metrics);
            switch(metrics.densityDpi){
                case DisplayMetrics.DENSITY_MEDIUM:
                    if (isTablet) {
                        return 2;
                    } else return 4;
                case DisplayMetrics.DENSITY_HIGH:
                    if (isTablet) {
                        return 2;
                    } else return 3;
                case DisplayMetrics.DENSITY_XHIGH:
                    if (isTablet) {
                        return 2;
                    } else return 3;
                case DisplayMetrics.DENSITY_XXHIGH:
                    return 3;
                case DisplayMetrics.DENSITY_TV:
                    return 2;
            }
            return 3;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            Locale l = Locale.getDefault();
            switch (position) {
                case 0:
                    return getString(R.string.title_section1).toUpperCase(l);
                case 1:
                    return getString(R.string.title_section2).toUpperCase(l);
                case 2:
                    return getString(R.string.title_section3).toUpperCase(l);
            }
            return null;
        }
    }

和我也用于动画的 DepthPageTransformer:

public class DepthPageTransformer implements ViewPager.PageTransformer {
        private static final float MIN_SCALE = 0.8f;

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

            if (position < -1) { // [-Infinity,-1)
                // This page is way off-screen to the left.
                view.setAlpha(0);

            } else if (position <= 0) { // [-1,0]
                // Use the default slide transition when moving to the left page
                view.setAlpha(1);
                view.setTranslationX(0);
                view.setScaleX(1);
                view.setScaleY(1);

            } else if (position <= 1) { // (0,1]
                // Fade the page out.
                view.setAlpha(1 - position);

                // Counteract the default slide transition
                view.setTranslationX(pageWidth * -position);

                // Scale the page down (between MIN_SCALE and 1)
                float scaleFactor = MIN_SCALE
                        + (1 - MIN_SCALE) * (1 - Math.abs(position));
                view.setScaleX(scaleFactor);
                view.setScaleY(scaleFactor);

            } else { // (1,+Infinity]
                // This page is way off-screen to the right.
                view.setAlpha(0);
            }
        }
    }

所以我解决了我的问题,它是我从 developers.google.com 下载的 DepthPageTransformer 中的 transformPage 方法,我将其修改如下:

public void transformPage(View view, float position) {
            float translationX;
            float scale;
            float alpha;

            if (position >= 1 || position <= -1) {
                // Fix for https://code.google.com/p/android/issues/detail?id=58918
                translationX = 0;
                scale = 1;
                alpha = 1;
            } else if (position >= 0) {
                translationX = -view.getWidth() * position;
                scale = -0.2f * position + 1;
                alpha = Math.max(1 - position, 0);
            } else {
                translationX = 0.5f * view.getWidth() * position;
                scale = 1.0f;
                alpha = Math.max(0.1f * position + 1, 0);
            }

            view.setTranslationX( translationX);
            view.setScaleX(scale);
            view.setScaleY(scale);
            view.setAlpha(alpha);
        }

一切都很完美。