How to Disable Swiping in ViewPager While Allowing Programmatic Swiping

How to Disable Swiping in ViewPager While Allowing Programmatic Swiping

6 June 2024 Stephan Petzl Leave a comment Tech-Help

ViewPager is a popular component in Android development used to create swipeable views. However, there are cases where you might want to disable the swipe gesture while still allowing programmatic swiping. This article will guide you through the necessary steps to achieve this.

Subclassing ViewPager

One effective approach is to subclass ViewPager and override specific touch event methods. This method ensures that swiping by touch is disabled, but programmatic swiping remains functional.

Step-by-Step Implementation

Follow these steps to create a custom ViewPager that disables swipe gestures:

1. Create a Custom ViewPager Class

Add the following class to your src folder:

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.animation.DecelerateInterpolator;
import android.widget.Scroller;
import java.lang.reflect.Field;

public class NonSwipeableViewPager extends ViewPager {

    public NonSwipeableViewPager(Context context) {
        super(context);
        setMyScroller();
    }

    public NonSwipeableViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        setMyScroller();
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        // Never allow swiping to switch between pages
        return false;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // Never allow swiping to switch between pages
        return false;
    }

    // For smooth scrolling
    private void setMyScroller() {
        try {
            Class viewpager = ViewPager.class;
            Field scroller = viewpager.getDeclaredField("mScroller");
            scroller.setAccessible(true);
            scroller.set(this, new MyScroller(getContext()));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public class MyScroller extends Scroller {
        public MyScroller(Context context) {
            super(context, new DecelerateInterpolator());
        }

        @Override
        public void startScroll(int startX, int startY, int dx, int dy, int duration) {
            super.startScroll(startX, startY, dx, dy, 350 /*1 sec*/);
        }
    }
}

    

2. Update Your Layout File

Make sure to use your custom ViewPager in your layout file:

<com.yourcompany.NonSwipeableViewPager
    android:id="@+id/view_pager"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1" />

    

Alternative: Using ViewPager2

If you are using ViewPager2, the process is even simpler. ViewPager2 includes a built-in method to disable user-initiated scrolling:

viewPager2.setUserInputEnabled(false);

    

To enable user-initiated scrolling again, use:

viewPager2.setUserInputEnabled(true);

    

Conclusion

Disabling swipe gestures in ViewPager while allowing programmatic swiping can be easily achieved by subclassing ViewPager or using the built-in properties of ViewPager2. Implementing these solutions can enhance the user experience by providing a controlled navigation flow within your app.

Enhance Your Testing Workflow

Managing complex UI interactions can be challenging, especially when it comes to testing. Our product, Repeato, can simplify this process. Repeato is a no-code test automation tool for iOS and Android, leveraging computer vision and AI to create, run, and maintain automated tests. This allows you to focus on developing your app rather than spending extensive time on test creation and maintenance. Additionally, it enables non-technical colleagues or QAs to handle test automation tasks efficiently.

For more information, visit our documentation or check out our latest updates on our blog.

Like this article? there’s more where that came from!