Search code examples
androidandroid-fragmentsandroid-viewpagerandroid-4.0-ice-cream-sandwich

Android 4.0.x and 4.1.x viewPager bug?


i have a problem with viewPager on android API 15 and 16, my app si basically simple remote control for set top boxes with three screens where on the first screen you can choose which box you want to control, second and third are just screens with controls. Now the problem is that everything works pefectly on every other version of android, but on API 15 and 16, when the app starts, i can not uses the creen with boxes, when i tap anywhere it detects actions on the second screen which are controls, when i swipe to third screen and then back, onCreateView for first screen is called again and everything works perfectly. Here are images of first and second screens enter image description here second and here is my code for 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;
        }
    }

and DepthPageTransformer which i am also using for animation:

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);
            }
        }
    }

Solution

  • so i resolved my problem, it was transformPage method in DepthPageTransformer wich i downloaded from developers.google.com i modified it like this:

    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);
            }
    

    and everything works perfectly.