I don't want ViewPager to allow me to swipe with my fingers.

Asked 2 years ago, Updated 2 years ago, 84 views

I have ViewPager on my app and there are 10 buttons. If you click the button, I want to go to the 4th page with mPager.setCurrentItem(3);. But first, I want to prevent you from swiping the page with your fingers. I just want to click the button on the page, what should I do?

android android-viewpager

2022-09-22 13:44

1 Answers

First, you need a subclass of ViewPager. OnTouchEvent can prevent subviews from being touched The onInterceptTouchEvent can be prevented from swiping as desired.

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;

public class NonSwipeableViewPager extends ViewPager {

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

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

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        // Swiped to prevent the page from changing. 
        return false;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Swipe to prevent pay change. 
        return false;
    }
}


2022-09-22 13:44

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.