3

Environment:

"react": "16.9.0",
"react-native": "0.61.5",
"react-native-gesture-handler": "^1.5.2",
"react-native-reanimated": "^1.4.0",
"react-native-tab-view": "^2.11.0"

Problem:

How to stop scrolling sliding when switching to another tab?

Example:

When I scroll the content and remove my finger from the screen, the content continues to scroll, that is, slide. I need that when I switch to another tab, this scroll slide stops instantly. That is, the content in that tab did not scroll in the background.

enter image description here

App.js

import * as React from 'react';
import {FlatList, Text, View, StyleSheet, Dimensions} from 'react-native';
import {TabView, SceneMap} from 'react-native-tab-view';

const DATA = Array.from(Array(1000).keys());

const renderFlatList = () => {
  const renderItem = ({item, index}) => {
    return <Text>{index}</Text>;
  };

  const keyExtractor = (item, index) => index.toString();

  return (
    <FlatList data={DATA} renderItem={renderItem} keyExtractor={keyExtractor} />
  );
};
const FirstRoute = () => (
  <View style={[styles.scene, {backgroundColor: '#ff4081'}]}>
    {renderFlatList()}
  </View>
);

const SecondRoute = () => (
  <View style={[styles.scene, {backgroundColor: '#673ab7'}]}>
    {renderFlatList()}
  </View>
);

export default class TabViewExample extends React.Component {
  state = {
    index: 0,
    routes: [{key: 'first', title: 'First'}, {key: 'second', title: 'Second'}],
  };

  render() {
    return (
      <View style={styles.container}>
        <TabView
          navigationState={this.state}
          renderScene={SceneMap({
            first: FirstRoute,
            second: SecondRoute,
          })}
          onIndexChange={index => this.setState({index})}
          initialLayout={{width: Dimensions.get('window').width}}
        />
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    marginTop: 36,
  },
  scene: {
    flex: 1,
  },
});
Sergey
  • 61
  • 7
  • 1
    can you explain it to some gif/video example of the issue? it is a bit vague – Rizwan Atta Dec 06 '19 at 09:28
  • @Rizwanatta I explained the problem in more detail, could you help me with it? – Sergey Dec 07 '19 at 04:32
  • if you are using react-navigation! you can use the component on Focus leave! to detect and ask the scrollView to stop scroll – Rizwan Atta Dec 07 '19 at 08:34
  • @Rizwanatta Yes, I use react-navigation. How can I ask the FlatList to stop scrolling? I could not find such a method in the documentation – Sergey Dec 07 '19 at 08:42
  • using getScrollableNode() function you get the scrollView and you can use ScrollToIndex(0) when you are asking react-navigation to take you to other screen – Rizwan Atta Dec 07 '19 at 09:56
  • @Rizwanatta Your solution scrolls the list up to the first item, I don't need it. I need the scrolling to stop immediately when I switch to another tab – Sergey Dec 07 '19 at 10:14

1 Answers1

0

Per the comment by OP @Sergey React Navigation is used. It may be a problem with react-native-tab-view itself. However, React Navigation bottom tabs do swap without waiting for the scroll to finish.

https://snack.expo.dev/@trajano/scrolling-and-tab-switch shows this in action. You can do momentum scroll (and I slowed down the deceleration) so you can swap to the next tab

Archimedes Trajano
  • 35,625
  • 19
  • 175
  • 265