The same annoying behavior occurs when the pages contain a ScrollView or a NestedScrollView, hinting at which component is causing these problems: the parent RecyclerView. In my opinion this makes ViewPager2, in its current state, practically unusable compared to the legacy ViewPager. The result is the same with any kind of ViewPager2 adapter, including FragmentStateAdapter. This time the ViewPager2 takes over and intercepts the gesture, unless it’s perfectly vertical (which is difficult to achieve). Vertical scroll is expected, but the horizontal ViewPager2 reacts instead. Here’s what happens when you try to scroll a vertical RecyclerView inside a parent horizontal ViewPager2 (which internally uses a RecyclerView): Horizontal pagers are present in almost every app I’m aware of, and the pages often contain a vertical RecyclerView or some form of ScrollView. Introducing ViewPager2Īt this point you may think that nested RecyclerViews are rare so the severity of this issue is low.īut it turns out that the new ViewPager2 layout, which officially replaces the legacy ViewPager, uses a RecyclerView internally. And it usually does it pretty well, but not in this particular case where only a perfectly horizontal gesture will be recognized. Most of the time when using our phones, we are actually performing such imprecise diagonal gestures because we expect the system to understand our intent based on the current context. It’s actually diagonal, even if you can see that the vertical distance of the gesture is small compared to its horizontal distance and its intent was clearly horizontal. This actually happens when the scroll gesture is not perfectly horizontal. Many times, even though an horizontal gesture is performed on a child RecyclerView, the parent RecyclerView kicks in and intercepts the touch event instead of the child RecyclerView, resulting in a small vertical scroll instead of an horizontal scroll. Horizontal scroll is expected, but vertical scroll kicks in instead. Notice that attempting to scroll the nested horizontal RecyclerViews often ends up with unexpected results. This kind of layout with scrollable horizontal items inside a vertical list is commonly found in Netflix-like video streaming apps with scrollable movie posters inside sections, or the Google Play Store app. Suppose we have a RecyclerView with a vertical LinearLayoutManager and each item of this vertical list consists of a RecyclerView with an horizontal LinearLayoutManager. In practice this works well with the legacy ViewPager which only scrolls horizontally, ignoring vertical gestures.īut there is a problem with RecyclerView, and by extension ViewPager2. What about nested scrolling in the opposite direction? This should be supported out-of-the-box, since a vertical scrolling view is not supposed to intercept horizontal gestures and vice versa. However, Google currently provides a workaround to implement nested scrolling in a ViewPager2. Note that RecyclerView doesn’t implement NestedScrollingParent3, so using a scrollable child inside a RecyclerView which scrolls in the same direction is not supported. For example, this mechanism is used when a RecyclerView is placed inside a CoordinatorLayout next to an app bar which will automatically slide up or collapse when a scroll gesture is initiated. A single scroll event may be consumed by both the parent and child with a seamless transition. Depending on its internal logic, the parent optionally consumes the scroll before or after the child, until they both reach their available scroll distance. In a nutshell, the scrolling child intercepts a scroll event and initiates a nested scroll in cooperation with the scrolling parent. RecyclerView also supports nested scrolling by implementing the NestedScrollingChild3 interface, which means it can cooperate with a parent view implementing NestedScrollingParent3 when both are configured to scroll in the same direction. Most implementations allow to choose between horizontal and vertical scrolling and a LayoutManager could technically support both at the same time. One core feature of RecyclerView is the externalization of its layout engine to components called LayoutManagers, which are no longer limited to vertical scrolling only. It’s more capable and flexible than its ListView predecessor but this also leads to more complexity and new problems. R ecyclerView is one of the main building blocks of Android user interfaces today.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |