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?
-
Answer is here, see support nested scrollable element part: https://developer.android.com/training/animation/vp2-migration – Jam Feb 18 '20 at 16:26
-
Did you find a fix. I am having the same issue – Yasiru Nayanajith Apr 16 '22 at 06:42
3 Answers
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

- 331
- 4
- 15
Try view_pager.setUserInputEnabled(false), it's worked for me.
from https://medium.com/@ankurg22/viewpager2-on-the-outside-recyclerview-inside-a005adb5d63d

- 33
- 6
-
1I converted my app from Viewpager to viewpager2. Each contains RV in fragment. Your answer fixed the scrolling issue. – Rohaitas Tanoli Jun 30 '22 at 07:20
-
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);
}
}

- 2,974
- 3
- 29
- 36
-
1This 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