73

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

3
  • You mean you want to remove the swipe gestures capability too, I understand. If so, try to create a new project for 4.0 + in Eclipse, it will let you create an empty project with both swipeable and non-swipeable tabs, check the differences, it's the fastest way and you can see a lot of code. Commented Feb 4, 2013 at 10:50
  • well, actually we can't horizontally swipe now, since cliente didn't like it, so I just wanna remove the animations (swiping is already disabled) Commented Feb 4, 2013 at 10:53
  • see my answer here - stackoverflow.com/a/54571100/4891063 Commented Feb 7, 2019 at 10:28

6 Answers 6

191

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.

Sign up to request clarification or add additional context in comments.

4 Comments

@JayVDiyk Example. on Button Click call it like this: mViewPager.setCurrentItem(position, false);
Anyone doing this might also be interested in this as well: stackoverflow.com/a/13437997/3075340
Thanks for clarification...I used it with true; thinking that smoothness will remove the hanging. I misused it .... :(
Note current implementation of setCurrentItem(int item) uses smoothScroll true (except on first layout)
55

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.

3 Comments

Why do we want to subclass ViewPager when it already provides this overload?
@user1032613 because current implementation of setCurrentItem(int item) uses smoothScroll true (except on first layout)
In viewpager2, the viewpager class is final hence it cannot be overrriden.
1

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

Comments

1

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) 

Comments

0

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

mBottomNavigation.setOnTabSelectedListener((position, wasSelected) -> { viewPager.setCurrentItem(position, false); return true; }); 

Comments

0

Viewpager2 with TabLayout

new TabLayoutMediator(mBinding.tab, mBinding.viewPager,false,false, new TabLayoutMediator.TabConfigurationStrategy() { @Override public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) { switch (position) { case 0: tab.setText("Buy rate"); break; case 1: tab.setText("Sell rate"); break; } } }).attach(); 

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.