4

I'm using ViewPager2 with FragmentStateAdapter to bind fragments. Each of my fragment has 3 recycler views laid out vertically. The problem is I'm not able to scroll the recycler view inside the fragments and also when I try to scroll the content vertically, the view pager changes tabs. How to fix this?

rko
  • 203
  • 3
  • 10

3 Answers3

3

Actually ViewPager2 Doesn't support nested Scrollable elements, So to support a scroll view inside a ViewPager2 object with the same orientation, you must call requestDisallowInterceptTouchEvent() or wrap your nested scrolling view inside this class.

Also, you can read this article to know about how to Support nested scrollable elements inside ViewPager2

Ali
  • 331
  • 4
  • 15
1

Try view_pager.setUserInputEnabled(false), it's worked for me.

from https://medium.com/@ankurg22/viewpager2-on-the-outside-recyclerview-inside-a005adb5d63d

javaing
  • 33
  • 6
1

I have faced the same issue I have three pages with vertical recyclerview and each page is a separate fragment inside viewpager2.

what i am doing is calculating the scroll in x and y direction and i am disabling the touch if scroll in y direction is more.

public class CustomScrollHost extends FrameLayout {
    private float xDistance, yDistance, lastX, lastY;

    public CustomScrollHost(@NonNull Context context) {
        super(context);
    }

    public CustomScrollHost(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomScrollHost(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public CustomScrollHost(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                xDistance = yDistance = 0f;
                lastX = ev.getX();
                lastY = ev.getY();
                getParent().requestDisallowInterceptTouchEvent(true);
                break;
            case MotionEvent.ACTION_MOVE:
                final float curX = ev.getX();
                final float curY = ev.getY();
                xDistance += Math.abs(curX - lastX);
                yDistance += Math.abs(curY - lastY);
                lastX = curX;
                lastY = curY;
                Log.d("CustomScrollHost", "xDistance " + xDistance);
                Log.d("CustomScrollHost", "yDistance " + yDistance);
                getParent().requestDisallowInterceptTouchEvent(yDistance > xDistance);
        }
        return super.onInterceptTouchEvent(ev);
    }

}
r4jiv007
  • 2,974
  • 3
  • 29
  • 36
  • 1
    This is the only solution that worked for me. Not even the NestedScrollableHost provided by Google worked. So thanks a lot!! – moyo Jan 19 '23 at 14:24