Jithin Sunny
Jithin Sunny

Reputation: 3402

Disable child viewPager swipe inside parent viewPager

I want to disable swipe of child viewpager which is in a parent viewpager.

I currently use this custom child viewpager

public class CustomViewPager extends ViewPager {

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

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

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

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

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {

    return true;
}
}

This disable swipe of child viewpager,but the parent viewpager swiping get disabled at that portion.

Upvotes: 2

Views: 1772

Answers (2)

Oleksandr Bodashko
Oleksandr Bodashko

Reputation: 131

To disable ViewPager scrolling without affecting parent Events, override canScrollHorizontally() method:

@Override
    public boolean canScrollHorizontally(int direction) {
        //Disable horizontal scrolling
        if (enabled) {
            return super.canScrollHorizontally(direction);
        } else {
            return false;
        }
    }

Upvotes: 5

Alex Yau
Alex Yau

Reputation: 101

You should return false from both onInterceptTouchEvent and onTouchEvent in the child ViewPager. Returning True from onTouchEvent will tell the parent that the view has handled the event successfully, which is not what you want.

@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;
}

Upvotes: 1

Related Questions