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

Hi @hemantasapkota , I tried your patch, but I'm getting this error: Tried to register two views with the same name AndroidHorizontalScrollView

I'm currently using [react, react-native] => [16.0.0-alpha.12, 0.49.3], I ran both patches by the way.

Thanks for your help dude!

linoleum00 avatar Oct 27 '17 16:10 linoleum00

@linoleum First off, probably should use 16.0.0-beta.5 to avoid dependency issues -- unless you still have the React.PropTypes or React.createClass code. Second, If you want to get rid of that error, you can just add a uniqueId function to the AndroidHorizontalScrollView name and it will disappear.

If you guys don't want to maintain huge patches for this on Android, you can instead copy over ReactScrollView.java and ReactScrollViewManager.java from react native and change a few lines (especially if you are in that situation where you can't patch java code when creating builds):

  • In ReactScrollView.java, extend NestedScrollView instead of ScrollView (also this line: NestedScrollView.class.getDeclaredField("mScroller");); add your missing imports from the ReactAndroid/.../scroll/ directory; comment out the conflicting onAttachedToWindow function.
  • In ReactScrollViewManager.java, change REACT_CLASS to be another name such as RCTNestedScrollView and import missing dependencies.

Also in ScrollView.js, patch the one case where it's a vertical, android scrollview and change it from RCTScrollView to RCTNestedScrollView

If someone else doesn't do this first, I'm probably going to try and open a PR to support android nested scrollviews and turn it on through a prop or something.

PS: don't forget to add new ReactScrollViewManager() to your createViewManagers function

kpink224 avatar Oct 30 '17 14:10 kpink224

@linoleum00 @kpink224

mrkm avatar Oct 31 '17 01:10 mrkm

Hi all,

https://snack.expo.io/rk93R9BCW

or

https://snack.expo.io/S11vIpHA- hacky way

https://gist.github.com/ashrithks/8d97f928d92643468a26e29c4d2dbb67

ashrithks avatar Oct 31 '17 06:10 ashrithks

@ashrithks Awesome example to show how to get this working with android.

The advantage of the NestedScrollView though is that it elegantly handles passing of the responder to the parent if you reach the bounds of the inner scrollview. Plus you don't have to flood the bridge with scroll events or set state every time since it's all happening on the native layer.

kpink224 avatar Oct 31 '17 15:10 kpink224

@ashrithks i tried example app. It looks awesome. Thanks.

Edit: At our currently developing application, Android setting state very late when you touch nested list. I dont know what is the problem at Android side.

volkansahin45 avatar Nov 27 '17 14:11 volkansahin45

I haven’t followed all of the Android related chatter precisely, but if someone is maintaining a native solution that works similar to my iOS one, then we should maybe get in touch and chat about a unified solution that could go into React Native proper. Please ping me if that’s the case.

cc @kpink224 @ashrithks @hemantasapkota

alloy avatar Nov 27 '17 14:11 alloy

I can recommend https://github.com/kmagiera/react-native-gesture-handler for a better way to handle multiple ScrollViews on Android.

pfeiffer avatar Nov 27 '17 14:11 pfeiffer

I have following structure

<Scrollview> (made scrollenbaled =false and refreshcontrol is enabled) 
      <Scrollview> 
      horizontal scrolling -- this is working 
     </Scrollview> 
     -Tabnavigator
           -tab1
              -listview (not scrolling)
           -tab2
              -listview (not scrolling)
       </Tabnavigator>
 </Scrollview>

I do i make the listview scroll ?

chetankotkar avatar Dec 08 '17 13:12 chetankotkar

<FlatList
  data = {[{}, {}, {}]}
  horizontal
  pagingEnabled
  keyExtractor = {(item, index) => index}
  renderItem = {({item, index}) => {
    if (index == 0) {
      return (
        <View style = {{width}}>
          <View style = {styles.header}>
            <Text>{`Page: ${index}`}</Text>
          </View> 
          <ScrollView
            horizontal
            pagingEnabled
          >
            <View style = {styles.header}>
              <Text>ChildPage: 1</Text>
            </View>
            <View style = {styles.header}>
              <Text>ChildPage: 2</Text>
            </View>
          </ScrollView>
        </View>
      )
    } else {
      return (
        <View style = {styles.header}>
          <Text>{`Page: ${index}`}</Text>
        </View>
      )
    }
  }}
/>

When scrollview is embedded in a item of flatlist, it works in iOS, but not working in android.

Any help? thanks

ZionChang avatar Jan 22 '18 10:01 ZionChang

@jenskuhrjorgensen i'm experiencing the same issue!! Did you find any solution?? :(

rekha110254 avatar Mar 26 '18 09:03 rekha110254

@rekha110254 Nope - I was so lucky that the design changed instead! 🍀 😄

jenskuhrjorgensen avatar Mar 26 '18 10:03 jenskuhrjorgensen

@chetankotkar i'm experiencing same issue!! Did u find any solution? :/

rekha110254 avatar Mar 26 '18 10:03 rekha110254

@ashrithks ur method is buggy, it doesnt work after u scroll down and then go up. tried it on a real device.

wamry avatar Apr 12 '18 20:04 wamry

Anyone has the solution to this?

I have to use nested FlatList inside ScrollView, but onEndReached keeps firing without reaching the end...

b1acKr0se avatar May 04 '18 08:05 b1acKr0se

Mine was ScrollView is rendered inside FlatList. The ScrollView scrolling effect doesnt work anymore but the FlatList is working fine.

Any solution for this?

alz10 avatar May 30 '18 09:05 alz10

#19543 any fix pls?

alz10 avatar Jun 04 '18 09:06 alz10

I have found this nestedScrollEnabled but seems like its not working https://facebook.github.io/react-native/docs/scrollview#nestedscrollenabled https://snack.expo.io/HJsi9gDmQ ?

ButuzGOL avatar Jul 14 '18 04:07 ButuzGOL

nestedScrollEnabled works, first of all make sure your RN version is >= 0.56.0, because this property was introduced in 0.56.0. Add nestedScrollEnabled for inner/child ScrollView component.

ivanzotov avatar Aug 02 '18 05:08 ivanzotov

@ivanzotov its not working.. its all waste of time upgrading to 0.56

ZeroCool00 avatar Aug 18 '18 05:08 ZeroCool00