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?
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;
}
}
© 2024 OneMinuteCode. All rights reserved.