Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vertical ViewPager2 with RecyclerView Scrolling Issue

I'm using a ViewPager2 with two fragments in a vertical orientation. When the user swipes down to the second fragment, there is a RecyclerView that scrolls content in the same vertical direction.

The issue is that when I scroll the contents of the RecyclerView, sometimes the ViewPager2 catches the scroll events and sometimes the RecyclerView catches the scroll events.

I would like it so that when the user is scrolling to the top of the RecyclerView, the ViewPager only swipes back up to the first fragment when the user has reached the top of the contents in the RecyclerView.

I've tried using recyclerView.isNestedScrollingEnabled = false without much luck. I also tried putting the RecyclerView into a NestedScrollView, but that is not recommended because the RecyclerView then creates every single ViewHolder it needs for the dataset and that is obviously not efficient.

like image 394
Calvin Rai Avatar asked Mar 11 '20 16:03

Calvin Rai


1 Answers

So...I was able to figure it out by just reading some documentation 😅. I'll post the answer here so that it helps anyone else having a similar issue:

Since ViewPager2 does not supported nested scroll views very well, unlike NestedScrollView, we need to wrap our nested scrollview with a custom wrapper in our layout to be able to handle the touch and swipe events that are getting intercepted by our nested scroll views parent. In our case, the child would be the RecyclerView and the parent would be the ViewPager2.

You can find the wrapper class here. Simply add it to your project and then wrap your scrollable view in it, similar to below:

    <NestedScrollableHost
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/my_recycler_view"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical" />

    </NestedScrollableHost>

There are a couple things to note here: The documentation says that this solution will not work for scrollable views that are within other scrollable views within the ViewPager. This solution only works for immediate scroll views of the ViewPager.

Another note is that the wrapper class uses the requestDisallowInterceptTouchEvent() to make sure that the child scrollable view tells the parent not to scroll if the child needs to scroll instead.

like image 62
Calvin Rai Avatar answered Sep 19 '22 12:09

Calvin Rai