Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding out scroll direction in react-native listview/scrollview

Is there a way to find out the scroll direction of react-native's listview/scrollview components?

Native iOS components seem to be able to do it by calculating the offset from scrollViewWillBeginDragging and scrollViewDidScroll, but there seems to be no bindings for these.

like image 661
Secret Avatar asked Apr 20 '16 14:04

Secret


People also ask

How do I get the scroll view position in React Native?

To get the current scroll position of ScrollView in React Native, we can set the onScroll prop of the ScrollView to a function that takes the scroll event object as an argument.

Can scroll in ScrollView React Native?

ScrollViews can be configured to allow paging through views using swiping gestures by using the pagingEnabled props. Swiping horizontally between views can also be implemented on Android using the ViewPager component. On iOS a ScrollView with a single item can be used to allow the user to zoom content.

How do I change the direction of ScrollView in React Native?

If someone will run into this in the future: There isn't any 'built-in' property that will set ScrollView's direction to RTL at the moment. However That's what worked for me: set flexDirection: 'row-reverse' to ScrollView's style, which will order your items from right to left.

How do I view horizontal scroll in React Native?

In the ScrollView, we can scroll the components in both direction vertically and horizontally. By default, the ScrollView container scrolls its components and views in vertical. To scroll its components in horizontal, it uses the props horizontal: true (default, horizontal: false).


2 Answers

I had problems with alexp's solution to accurately specify the direction when the difference between the old and the new offset was very small. So I filtered them out.

  _onScroll = event => {     const currentOffset = event.nativeEvent.contentOffset.y;     const dif = currentOffset - (this.offset || 0);      if (Math.abs(dif) < 3) {       console.log('unclear');     } else if (dif < 0) {       console.log('up');     } else {       console.log('down');     }      this.offset = currentOffset;   }; 
like image 45
Johannes Filter Avatar answered Sep 19 '22 19:09

Johannes Filter


You can use the onScroll event of a ScrollView and check the contentOffset in the event callback:

https://rnplay.org/apps/FltwOg

'use strict';  var React = require('react-native'); var {   AppRegistry,   StyleSheet,   ScrollView,   Text,   View, } = React;  var SampleApp = React.createClass({   offset: 0,   onScroll: function(event) {     var currentOffset = event.nativeEvent.contentOffset.y;         var direction = currentOffset > this.offset ? 'down' : 'up';     this.offset = currentOffset;     console.log(direction);   },   render: function() {     return (       <View style={styles.container}>         <ScrollView style={styles.scroller} onScroll={this.onScroll}>           <Text>Scrollable content here</Text>         </ScrollView>       </View>     )   } });  var styles = StyleSheet.create({   container: {     flex: 1,     marginTop: 50,   },   scroller: {     height: 5000,   }, });  AppRegistry.registerComponent('SampleApp', () => SampleApp); 
like image 77
alexp Avatar answered Sep 20 '22 19:09

alexp