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

时间:2015-01-28 09:08:59

标签: android android-fragments android-viewpager android-4.0-ice-cream-sandwich

我的android API 15和16上的viewPager有问题,我的应用程序基本上是简单的遥控器,用于三个屏幕的机顶盒,在第一个屏幕上你可以选择你要控制哪个盒子,第二个和第三个只是带控件的屏幕。现在的问题是,所有其他版本的android都可以完美地运行,但是在API 15和16上,当应用程序启动时,我无法使用带有框的creen,当我点击任何地方时它会检测到第二个屏幕上的操作是控件,当我滑动到第三个屏幕然后返回时,onCreateView再次调用第一个屏幕,一切都运行良好。以下是第一个和第二个屏幕enter image description here second的图像 这是我的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);
            }
        }
    }

1 个答案:

答案 0 :(得分:2)

所以我解决了我的问题,它是我从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);
        }

一切都很完美。