How do disable paging by swiping with finger in ViewPager but still be able to swipe programmatically?


How to Disable Swiping with Finger in ViewPager
š Welcome to our blog post! Today, we are going to address a common issue with ViewPager in Android: disabling paging by swiping with your finger, while still allowing programmatically controlled swipes.
ā Have you ever encountered a situation where you wanted to give your users the ability to navigate through ViewPager using buttons, but you didn't want them to be able to swipe through the pages with their fingers? Well, you're in luck! We have an easy solution for you.
The Challenge
š Let's paint the picture: you have a ViewPager containing multiple pages, and below it, you have a set of buttons. When a button is clicked, you want the ViewPager to immediately switch to the corresponding page programmatically using mPager.setCurrentItem(position);
. However, you want to disable the ability to swipe between pages using finger gestures.
The Solution
š” To achieve this functionality, we need to create a custom ViewPager class and override its onTouchEvent
method to block touch events. Let's dive into the steps:
1ļøā£ First, extend the ViewPager class and create a custom class, let's call it NonSwipeableViewPager
.
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
public class NonSwipeableViewPager extends ViewPager {
private boolean isSwipeEnabled = false;
public NonSwipeableViewPager(Context context) {
super(context);
}
public NonSwipeableViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
return isSwipeEnabled && super.onTouchEvent(event);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
return isSwipeEnabled && super.onInterceptTouchEvent(event);
}
public void setSwipeEnabled(boolean swipeEnabled) {
isSwipeEnabled = swipeEnabled;
}
}
2ļøā£ In your XML layout file, replace the existing ViewPager tag with your custom NonSwipeableViewPager tag.
<com.example.yourpackage.NonSwipeableViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
3ļøā£ Next, in your activity or fragment, find the NonSwipeableViewPager and set setSwipeEnabled(false)
to disable swiping with fingers.
NonSwipeableViewPager viewPager = findViewById(R.id.viewPager);
viewPager.setSwipeEnabled(false);
Conclusion
š Congratulations! You've successfully disabled swiping with fingers in the ViewPager while retaining the ability to programmatically control the swipes using buttons.
š¤ Ready to implement this solution in your own project? It's as easy as 1, 2, 3. Give it a try, and let us know if you encounter any issues or have any additional questions.
š Remember to share this blog post with your friends and colleagues who might find it useful. Happy coding!
Take Your Tech Career to the Next Level
Our application tracking tool helps you manage your job search effectively. Stay organized, track your progress, and land your dream tech job faster.
