# Fixing RecyclerView nested scrolling in opposite direction

DevFeed: [Fixing RecyclerView nested scrolling in opposite direction](<https://devfeed.tech/articles/fixing-recyclerview-nested-scrolling-in-opposite-direction-25879.md>)

Original publisher: [Read original article](<https://bladecoder.medium.com/fixing-recyclerview-nested-scrolling-in-opposite-direction-f587be5c1a04?source=rss-54910f05af37------2>)

Author: Christophe Beyls

Published: 2020-03-26T15:24:30Z

Content type: article

Language: en

Sources: [Christophe Beyls](<https://devfeed.tech/sources/stories-by-christophe-beyls-on-medium.md>)

Topics: [recyclerview](<https://devfeed.tech/topics/recyclerview.md>), [User interface design](<https://devfeed.tech/topics/ui-design.md>)

Tags: [android](<https://devfeed.tech/tags/android.md>), [android-app-development](<https://devfeed.tech/tags/android-app-development.md>), [androiddev](<https://devfeed.tech/tags/androiddev.md>), [gestures](<https://devfeed.tech/tags/gestures.md>), [nested-scroll](<https://devfeed.tech/tags/nested-scroll.md>), [recyclerview](<https://devfeed.tech/tags/recyclerview.md>), [workaround](<https://devfeed.tech/tags/workaround.md>)

## AI overview

RecyclerView may intercept a diagonal gesture in its enabled scroll direction once that axis crosses touch slop, even when the gesture is mostly toward a nested child scrolling the other way. The proposed fix checks which axis dominates before intercepting when the parent scrolls in only one direction. The article implements this through an OnItemTouchListener for gesture detection and an OnScrollListener to track dragging state, packaged as a Kotlin extension; for ViewPager2, it accesses the internal RecyclerView and applies the extension. Subclassing RecyclerView is impractical because the interception code depends on private fields, and ViewPager2 would still use its own RecyclerView. The extension is described as a hack that may break in future library versions; the author says an upstream fix would be preferable.

## Source excerpt

Photo by Tabl-trai under Creative Commons licenceand making ViewPager2 usable RecyclerView is one of the main building blocks of Android user interfaces today. It's more capable and flexible than its ListView predecessor but this also leads to more complexity and new problems. 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. Most implementations allow to choose between horizontal and vertical scrolling and a LayoutManager could technically support both at the same time. 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. In a nutshell, the scrolling child intercepts a scroll event and initiates a nested scroll in cooperation with the scrolling parent. Depending on its internal logic, the parent optionally consumes the scroll before or after the child, until they both reach their available scroll distance. A single scroll event may be consumed by both the parent and child with a seamless transition. 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. Note that RecyclerView doesn't implement NestedScrollingParent3, so using a scrollable child inside a RecyclerView which scrolls in the same direction is not supported. However, Google currently provides a workaround to implement nested scrolling in a ViewPager2.The problem 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. In practice this works well with the legacy ViewPager which only scrolls horizontally, ignoring vertical gestures. But