Reputation: 11975
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
Upvotes: 73
Views: 45961
Reputation: 1
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.
Upvotes: 0
Reputation: 865
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;
});
Upvotes: 0
Reputation: 61
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)
Upvotes: 1
Reputation: 2594
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);
}
...
}
Upvotes: 1
Reputation: 750
Here's another solution:
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.
Upvotes: 55
Reputation: 11975
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.
Upvotes: 191