【问题标题】:React Native's Animated.ScrollView is not allowing me to programmatically scroll to a certain positionReact Native Animated.ScrollView 不允许我以编程方式滚动到某个位置
【发布时间】:2019-08-06 23:03:24
【问题描述】:

在升级到最新版本的 React-Native 和 Expo 之前,此代码块正在运行。它正在使用的版本是"expo": "^32.0.0"

我以前能够以编程方式移动 Animated.ScrollView 的子项,但现在我无法再这样做了。这是我正在测试的测试代码块。

import React, { Component } from 'react';
import {
  StyleSheet,
  Text,
  View,
  Animated,
  TouchableOpacity,
  SafeAreaView,
  ScrollView
} from 'react-native';

export default class App extends React.Component {
  constructor(props) {
    super(props);
  }
  handleMove = () => {
    this.scroller.getNode().scrollTo({
      x: 200,
      y: 0,
      animated: false
    });
  };
  render() {
    return (
      <SafeAreaView style={styles.container}>
        <Text>Open up App.js to start working on your app!</Text>
        <Animated.ScrollView
          horizontal
          ref={scroller => {
            this.scroller = scroller;
          }}
        >
          <View
            style={{
              height: 100,
              width: 100,
              backgroundColor: 'red',
              margin: 5
            }}
          />
        </Animated.ScrollView>
        <TouchableOpacity onPress={this.handleMove}>
          <Text>Move</Text>
        </TouchableOpacity>
      </SafeAreaView>
    );
  }
}

const styles = StyleSheet.create({
  container: {}
});

升级后的代码块不再适用于最新版本。我正在测试的当前版本是:

"dependencies": {
    "expo": "^34.0.1",
    "react": "16.8.3",
    "react-dom": "^16.8.6",
    "react-native": "https://github.com/expo/react-native/archive/sdk-34.0.0.tar.gz",
    "react-native-web": "^0.11.4"
  },

这样做的正确方法是什么?

我添加了一份零食来帮助说明我的问题。 ' https://snack.expo.io/@louis345/brave-banana

【问题讨论】:

    标签: react-native scrollview


    【解决方案1】:

    我能够通过向滚动视图添加一个道具来解决我的问题。 scrollToOverflowEnabled={true}

    现在一切都像以前一样工作了。我希望这对将来的某人有所帮助。

    【讨论】:

    • 通过仔细查看您的代码,我注意到您在 ref 上调用了 getNode()。我做到了,它现在正在工作!不过这很奇怪,我在文档中找不到它,而且我通常无需通过 getNode() 即可访问我的 ref 函数。不过感谢您的回答!
    • 它实际上甚至可以在我身边没有scrollToOverflowEnabled={true} 的情况下工作。使用世博会 35。
    • 问题是这个解决方案只适用于IOS...有安卓的解决方案吗?
    • 你的解决方案只适用于IOS
    【解决方案2】:

    我能够通过使用 1 秒的简单 setTimeout 来完成这项工作。

    这是我的代码现在的样子:

    setTimeout(() => {
      this.scrollView.scrollTo({
        x: DEVICE_WIDTH * current_index,
        y: 0,
        animated: false
      });
    }, 1)
    

    类似于 Micheal 上面的建议,这很可能是由于 React 中已弃用的 componentWillMount 未在 ScrollView 中正确更新而导致的安装问题。

    【讨论】:

      【解决方案3】:

      你可以试试这个

      <Animated.ScrollView // <-- Use the Animated ScrollView wrapper
        scrollEventThrottle={1} // <-- Use 1 here to make sure no events are ever missed
        onScroll={Animated.event(
          [
            {
              nativeEvent: {
                contentOffset: {x: 200,y: 0},
              },
            },
          ],
          {useNativeDriver: true}, // <-- Add this
        )}>
        {content}
      </Animated.ScrollView>
      

      【讨论】:

      • 如何以编程方式执行此操作?
      • 你可以在内容中放你想要的
      • 我刚刚尝试了解决方案,但无法让动画正常工作。
      【解决方案4】:

      从 0.57 升级到 0.60 后,我遇到了类似的问题。

      似乎随着安装东西的时间(在 RN 版本之间)发生了一些变化。我将再次检查我的代码(几天后),因为我怀疑我正在使用一些即将被弃用的生命周期函数。它们现在被认为是“不安全”的使用可能是由于一些时间问题(疯狂猜测)。

      我只需将 scrollTo 设置为 100 毫秒超时,就可以让我的代码正常工作。这表明滚动视图在第一次尝试时没有完全安装。我实际上对我的用例的超时很好,但我还是想解决这个问题,因为我不喜欢 hacky 解决方案。

      【讨论】:

      • 我回家后会核实。谢谢。
      • 这对我不起作用,我可以通过其 ref 获取 ScrollView(因此它肯定已安装)但由于某种原因升级后 scrollTo 不起作用。您是否注意到其他任何可能为您解决此问题的方法?
      猜你喜欢
      • 2012-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-16
      • 1970-01-01
      • 2019-12-31
      • 1970-01-01
      相关资源
      最近更新 更多