[ScrollView] Add support for nested scrollable views.
For Discussion
In larger complex view hierarchies it may happen that a ScrollView-based component is nested inside another ScrollView.
Currently a nested ScrollView won’t receive scroll events, only the outer parent ScrollView will. In order to receive them, you would have to push scroll event callbacks down each child component of the parent ScrollView and all of those would have to send them down to their children, just in case a component somewhere down the tree needs to receive such events.
The developer and user of that component shouldn’t need to jump through hoops to have ScrollView callbacks work, but should be able to write isolated components that are unaware of the presence of other ScrollView instances in the view hierarchy and have React Native deal with the details of dispatching events to all components that require them.
Examples
Contrived
Multiple ListView instances as part of a larger scrolling view:
<ScrollView>
<Header />
<ListView />
<ListView />
<Footer />
</ScrollView>
Often the advice is to add views that are supposed to be above or below a ListView as a header or footer of the ListView, but that doesn’t solve the case where both ListView instance have to scroll in tandem as part of the parent ScrollView.
Real-world
My real world example is a ‘tab view’ component that displays a ScrollView-based component that needs to paginate.
You can run the example app that’s contained in the repo to see it for yourself, or in our production app that is currently in the store, but here’s a short demonstration of the ‘WORKS’ grid component being nested inside the top-level ScrollView and still paginating onEndReached as expected:

Proof of Concept
For my app I wrote some 🐒-patches that do the following:
- When a
RCTScrollViewis added to a view hierarchy, it registers for aRCTScrollEventnotification. - When a
RCTScrollViewreceives a native scroll callback it first does its own JS scroll event dispatching and afterwards it posts aRCTScrollEventnotification for any childRCTScrollViewinstances to pick-up. - The child
RCTScrollViewinstance creates a newRCTScrollEventobject that offsets the scrollview’scontentOffsetto reflect its location inside the parentRCTScrollView. - The child
RCTScrollViewthen dispatches the JS scroll event as normal and the component doesn’t know any better than that it has scrolled as normal.
Notes:
- Please don’t judge my implementation as anything other than a POC of the high-level functionality.
- I used
NSNotificationCenterbecause there’s a potential 1-to-many relationship. I haven’t noticed any discernible lag in delivery, but there’s also no guarantee. A possible alternative would be to use a proxy object as theUIScrollViewdelegate that can dispatch events to multiple delegates (example). - While I have not yet used the callbacks for a
ListViewcomponent in the current version of our app, I have tested and verified that theonEndReachedcallback gets triggered, thus I see no reason why theremoveClippedSubviewsoptimisation wouldn’t work either. At least that’s my goal, as I will need this soon.
Discussion
- I’m looking for suggestions as to how this should be implemented for it to be acceptable in React Native.
- When I discussed this with @javache offline he suggested abstracting the scroll position/event part from
ScrollViewinto aScrollablecontainer. That container could then create theScrollViewif not inside a parentScrollView. - I want to spend the time to write the iOS implementation.
- I currently do not target Android and thus don’t feel like I’m the right person to implement it for that platform at the moment, at least not from the get-go.
/cc @javache @nicklockwood
👍 Beautiful issue. Good job
@facebook-github-bot label Good First Task
I don't think that's a good first task @charpeni ?
I agree, this will require some more significant changes in how we create scrollviews and dispatch scroll events.
I'm running into this same issue. In iOS it seems to work as expected for me, but in Android the inner ScrollView never receives horizontal pan gestures if it is contained in a ScrollView that can also scroll horizontally, e.g. a zoomed image in a gallery
@alloy just ran into this, any progress on the issue?
The provided monkey-patch still works perfect in production with latest RN. I haven’t had much spare time to discuss and cleanup this patch until last week when we shipped a new major version of our app.
@javache Can we catch-up sometime soon to talk it through?
Guys, adding TouchableWithoutOpacity inside nested ScrollView worked like a charm for me on android: Checkout my answer on SO (couldn't make the markup right in here..) Maybe it's not the case though...
With 0.43.0-rc.2 I'm running into the same problem when attempting to wrap a ScrollView around the new FlatList component; the onEndReached event won't triggered as expected
@ilyadoroshin TouchableWithoutFeedback component worked for me on iOS.
Has anyone other than @alloy found a workaround? Even if it's a monkeypatch approach, I am trying to resolve this.
@ilyadoroshin @restlessCalm FYI The issue described in the linked SO post is about a completely different scenario than what this ticket is about.
@AlanFoster @andrerfneves Did you try my monkey patches in latest RN and/or with FlatList? I haven’t upgrade to the latest yet, but the internal scrollview API hasn't’t changed much over the months, so should probably be ok.
@alloy how would you go about using such monekypatches? Not entirely familiar with iOS development. My issue is actually the same but on the Android platform. Any thoughts?
Ahh I see. No sorry I have no input on Android right now, only iOS. As to how to integrate these monkey patches, you can take a look at our OSS project they reside in.
Any solution to nested scroll views on Android?
I have a vertical SectionList inside a vertical ScrollView with RefreshControl. When the bottom of the outer ScrollView is reached I can scroll down in the inner SectionList. But I can never scroll up in the inner SectionList because of the outer ScrollView's RefreshControl.
Same here. Any workaround? i have something like this:
<ScrollView>
<FlatList onEndReach={this.fetchNewPage} />
</ScrollView>
Greetings,
For anyone looking for a workaround for android, I have a solution that was tested successfully.
The core of the fix is to use NestedScollView instead of ScrollView. There's a library that already does this - https://github.com/mohtada-h/react-native-nested-scrollview, however it's not being maintained to support the latest RN.
I've created a patch that can be applied directly from the root of a react native project. You can find the patch file here:
https://raw.githubusercontent.com/hemantasapkota/react-native-nested-scrollview/master/root.patch
Summary of the fix:
npm install react-native-nested-scrollview --save
# From the root of your RN project
wget https://github.com/hemantasapkota/react-native-nested-scrollview/blob/master/root.patch
# Apply the patch
patch -p0 < root.patch | 2>/dev/null
Final step would be to use NestedScrollView instead of ScrollView in your source project.
@hemantasapkota hello dude, i get this error with your solution:
react-native-nested-scrollview\android\src\main\java\com\mohtada\nestedscrollview\ReactNestedScrollViewManager.j
ava:37: error: ReactNestedScrollViewManager is not abstract and does not override abstract method flashScrollIndicators(ReactNestedScrollView) in ScrollComman
dHandler
public class ReactNestedScrollViewManager
package.js
"dependencies": {
"react": "16.0.0-beta.5",
"react-native": "0.49.3",
"react-native-html-parser": "^0.0.5",
"react-native-loading-spinner-overlay": "^0.5.2",
"react-native-navigation": "^1.1.236",
"react-native-nested-scrollview": "^0.0.3",
"react-native-platform-touchable": "^1.1.1",
"react-native-scrollable-tab-view": "^0.8.0",
"react-native-smart-loading-spinner-overlay": "^1.0.2"
},
Hey @dviluk, seems like you're using [react, react-native] => [16.0.0-beta.5, react native 0.49.3] The patch that I posted was for [react, react-native] => [16.0.0-alpha.12, 0.48.4].
Between 0.48.4 & 0.49.3, RN has added a new method to the ScrollCommandHandler interface. To get the build working, use this patch.
--- node_modules/react-native-nested-scrollview/android/src/main/java/com/mohtada/nestedscrollview/ReactNestedScrollViewManager.java
+++ node_modules/react-native-nested-scrollview/android/src/main/java/com/mohtada/nestedscrollview/ReactNestedScrollViewManager.java
@@ -157,6 +157,10 @@ public class ReactNestedScrollViewManager
}
@Override
+ public void flashScrollIndicators(ReactNestedScrollView scrollView) {
+ }
+
+ @Override
public @Nullable Map getExportedCustomDirectEventTypeConstants() {
return createExportedCustomDirectEventTypeConstants();
}
Copy the contents to a file, (example: rn-0.49.3.patch ) and execute the following:
patch -p0 < rn-0.49.3.patch
Hope that's helpful. PS: You'll still need to apply the first patch i posted in the earlier thread.