Disabling animation in ViewPager

AndroidAnimationAndroid Viewpager

Android Problem Overview


I'd like to disable all the animations for the transitions in my custom ViewPager. This view pager contains four tabs -and each tab loads a Fragment- and what the view pager does is to switch the tabs: for example first tab is the index, second is map, etc.

The problem is that if, being the first tab chosen, I click on the fourth tab, I can see how the ViewPager goes through the second and third tab and stops on the fourth, and I don't want that to happen.

I tried to disable all the animations for this ViewPager trying to use a setAnimation to null every time the user chooses a new tab to be displayed, but it still doesn't work.

Any idea to achieve this, please? Thanks a lot in advance!

EDIT: I also tried to override onCreateAnimation for each Fragment but still not working

Android Solutions


Solution 1 - Android

I finally found out: the issue can be solved by just calling the mViewPager.setCurrentItem(position) with an extra parameter to false, which is the smooth scroll for the ViewPager. After this, the scroll will be done without any smoothing and thus the animations won't be seen.

Solution 2 - Android

Here's another solution:

  • Subclass the ViewPager (your custom ViewPager)
  • Override the two setCurrentItem methods

Code Snippet:

@Override
public void setCurrentItem(int item, boolean smoothScroll) {
    super.setCurrentItem(item, false);
}

@Override
public void setCurrentItem(int item) {
    super.setCurrentItem(item, false);
}

By setting smoothScroll to false, you're disabling the scroll animation.

Solution 3 - Android

I was searching for disabling the swipe animation even swipe by the user here is my implementation

1-Override Viewpager method onInterceptTouchEvent and onTouchEvent

2- create your own GestureDetector

3- detect the swipe gesture and use the setCurrentItem(item, false)

ViewPager

public class ViewPagerNoSwipe extends ViewPager {
    private final GestureDetector gestureDetector;
    private OnSwipeListener mOnSwipeListener;

    public void setOnSwipeListener(OnSwipeListener onSwipeListener) {
        mOnSwipeListener = onSwipeListener;
    }

    public ViewPagerNoSwipe(@NonNull Context context) {
        super(context);
        gestureDetector = new GestureDetector(context, new GestureListener());

    }

    public ViewPagerNoSwipe(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        gestureDetector = new GestureDetector(context, new GestureListener());


    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        return true;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        gestureDetector.onTouchEvent(ev);
        return false;
    }

    public class GestureListener extends GestureDetector.SimpleOnGestureListener {

        private static final int SWIPE_THRESHOLD = 100;
        private static final int SWIPE_VELOCITY_THRESHOLD = 100;

        @Override
        public boolean onDown(MotionEvent e) {
            return true;
        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            boolean result = false;
            try {
                float diffY = e2.getY() - e1.getY();
                float diffX = e2.getX() - e1.getX();
                if (Math.abs(diffX) > Math.abs(diffY)) {
                    if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
                        if (diffX > 0) {
                            if(mOnSwipeListener!=null)
                            mOnSwipeListener.onSwipeRight();
                        } else {
                            if(mOnSwipeListener!=null)
                                mOnSwipeListener.onSwipeLeft();
                        }
                        result = true;
                    }
                } else if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
                    if (diffY > 0) {
                        if(mOnSwipeListener!=null)
                            mOnSwipeListener.onSwipeBottom();
                    } else {
                        if(mOnSwipeListener!=null)
                            mOnSwipeListener.onSwipeTop();
                    }
                    result = true;
                }
            } catch (Exception exception) {
                exception.printStackTrace();
            }
            return result;
        }
    }

    public interface OnSwipeListener {

         void onSwipeRight();

        void onSwipeLeft();

        void onSwipeTop();

        void onSwipeBottom();
    }
}

the when you are set up the ViewPager set the swipeListener

postsPager.setOnSwipeListener(new ViewPagerNoSwipe.OnSwipeListener() {
            @Override
            public void onSwipeRight() {
                
              postsPager.setCurrentItem(postsPager.getCurrentItem() + 1,false);

            }

            @Override
            public void onSwipeLeft() {
                
            postsPager.setCurrentItem(postsPager.getCurrentItem() - 1, false);

            }
             ...
           }

Solution 4 - Android

In the tabselected listener, just set the second argument of the setCurrentItem to false to disable the smooth scrolling.

mBottomNavigation.setOnTabSelectedListener((position, wasSelected) -> {
       

       switch (position) {
            case 0:
                viewPager.setCurrentItem(0, false);
             
                return true;
            case 1:
                viewPager.setCurrentItem(1, false);
                
                return true;
            case 2:
                viewPager.setCurrentItem(2, false);
               
                return true;
        }
        return false;
    });

Solution 5 - Android

Resolved this by setting smooth scroll to false while assigning position to view pager. By this, smooth scroll will not be there + animations will be gone.

vpMain.setCurrentItem(position,false)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionnolomanView Question on Stackoverflow
Solution 1 - AndroidnolomanView Answer on Stackoverflow
Solution 2 - AndroidToniView Answer on Stackoverflow
Solution 3 - AndroidtamtomView Answer on Stackoverflow
Solution 4 - AndroidEngineerDannyView Answer on Stackoverflow
Solution 5 - AndroidNandita GandhiView Answer on Stackoverflow