3

I have a ViewPager with 3 Fragments contained in it. One of the Fragments contains another ViewPager. which makes it a cascaded ViewPager inside a ViewPager.

I need to prevent the inner ViewPager from swiping/paging while allow it to pass the scroll gestures to the contained RecyclerView.

Yes I'm well aware that a ViewPager inside a ViewPager is not a great idea, yet in this case the view pager should no swipe but pass the gestures down to the child views. So there should not be a problem of two swipe mechanism in the same directions.

I tried to use the following NonSwipeableViewPager code for the inner view pager:

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) {
        // Never allow swiping to switch between pages
        return false;
    }

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

And this indeed stops the ViewPager from paging/swiping but also prevents it from passing the touch events down to the RecyclerView.

How can I change this ViewPager to consume the swiping/paging gesture yet pass the scrolling gesture to the RecyclerView?

halfer
  • 19,824
  • 17
  • 99
  • 186
Emil Adz
  • 40,709
  • 36
  • 140
  • 187
  • I won't help you in your issue, but just tell, that ViewPager in ViewPager is bad idea - try to change your design. (It is the same story as making scrollview with horizontal recyclerview inside) – Mateusz Pryczkowski Jul 05 '16 at 11:04
  • You right in most cases, but in this case, where I don't wan't the inner view pager to be swappable, it's shouldn't be a problem. so it's basically a fragment container that give me a nice swiping animation when I press a tab, but does not change page by swiping – Emil Adz Jul 05 '16 at 11:17
  • This worked for me https://stackoverflow.com/a/7814054/7118110 – Deepak Kumar Dec 03 '19 at 17:12

0 Answers0