react-native icon indicating copy to clipboard operation
react-native copied to clipboard

[ScrollView] Add support for nested scrollable views.

Open alloy opened this issue 10 years ago • 47 comments

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:

nested scrollview

Proof of Concept

For my app I wrote some 🐒-patches that do the following:

  1. When a RCTScrollView is added to a view hierarchy, it registers for a RCTScrollEvent notification.
  2. When a RCTScrollView receives a native scroll callback it first does its own JS scroll event dispatching and afterwards it posts a RCTScrollEvent notification for any child RCTScrollView instances to pick-up.
  3. The child RCTScrollView instance creates a new RCTScrollEvent object that offsets the scrollview’s contentOffset to reflect its location inside the parent RCTScrollView.
  4. The child RCTScrollView then 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 NSNotificationCenter because 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 the UIScrollView delegate that can dispatch events to multiple delegates (example).
  • While I have not yet used the callbacks for a ListView component in the current version of our app, I have tested and verified that the onEndReached callback gets triggered, thus I see no reason why the removeClippedSubviews optimisation 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 ScrollView into a Scrollable container. That container could then create the ScrollView if not inside a parent ScrollView.
  • 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

alloy avatar Jun 09 '16 11:06 alloy

As pointed out in earlier comments, now nested scrolling is possible on both platforms via nestedScrollEnabled (which needs to be explicitly set for Android to use it, and as Ivan pointed out you need to have it set in the inner list).

kelset avatar Sep 19 '18 14:09 kelset

@kelset The OP is not about being able to scroll in nested scrollviews, though, that’s just something that many people have been discussing here but is off-topic.

alloy avatar Sep 19 '18 19:09 alloy

Hey @alloy thanks for the clarification, but then I'm confused by:

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.

I mean, it seems to me that nestedScrollEnabled obtains that 🤔 Or there is a key "under the surface" difference that I'm missing? Was it more about reaching something closer to this?

he suggested abstracting the scroll position/event part from ScrollView into a Scrollable container

Anyway I'll reopen :)

kelset avatar Sep 20 '18 08:09 kelset

Yeah it’s a nuanced issue 😄 It’s about having scrollviews inside a scrollview, which would disable the nested scrollviews from scrolling independently but still receive scroll events as if there were scrolling their own content. If you look at the GIF in the OP, you’ll see that per tab there’s only 1 large scrolling view, but there are actually some nested scrollviews in that hierarchy.

alloy avatar Sep 20 '18 09:09 alloy

I discussed this issue with @alloy in person. This is a valid proposal and definitely a change we'd like to have in React Native but over the course of almost three years nobody has put themselves forward to actually implement it and we haven't had a need for this at Facebook either, hence I'm going to close this issue.

However, if you feel passionate about adding the right APIs without breaking existing code, please feel free to keep discussing the concrete implementation here and start sending PRs that gets us closer to the goal :)

cpojer avatar Mar 19 '19 15:03 cpojer

Try this, It worked for me <ScrollView {...parentProps}> <ScrollView {...childProps} nestedScrollEnabled={true}>

nishaQueppelin avatar Feb 07 '20 05:02 nishaQueppelin