当嵌套子滚动视图滚动到其视图末尾时,如何防止*锁定*父滚动视图滚动?

发布于 2025-01-09 18:21:32 字数 576 浏览 4 评论 0原文

在react-native项目中,有一个父级平面列表/滚动视图。在此父列表中的每个项目内,都存在一个嵌套的滚动视图。

预期行为:当父滚动视图被锁定时,父滚动视图永远不应该滚动,只允许子滚动视图滚动。当满足子滚动视图的外部边界时(意味着您已滚动到子视图的底部或一直返回到子视图的顶部),父视图保持锁定状态

实际发生的情况:使用父视图滚动锁定,一旦子滚动视图到达其滚动边界,将子滚动视图滚动到最底部/顶部将滚动父滚动视图

重现行为的步骤:

  1. 向下轻拂子滚动视图之一直到到达底部
  2. (即使你已经到达了下方边界)尝试向下滚动子视图,
  3. 观察父滚动视图现在滚动的事实,即使父滚动视图被锁定,

如何在嵌套时真正防止父滚动视图滚动 -滚动视图到达其视图的末尾?

只在 Android 上注意到这一点,但 iOS 上也可能存在。

这是一个最小的可重现示例: https://staging.snack.expo.dev/b3XuJ6iA3

In a react-native project, there is a parent flatlist/scrollview. inside each item in this parent list, there exists a nested scroll view.

Expected behavior: When the parent scroll view is locked, the parent should NEVER scroll, only the child scrollview should be allowed to scroll. and when the outer bound of the child scroll view is met (meaning youve scrolled to the bottom or all the way back to the top of child view), the parent remains locked

What actually happens: with parent view scroll-locked, scrolling the child scrollview to the very bottom/top will scroll the parent scroll view, once the child scroll view reaches its scroll-boundary

Steps to reproduce behavior:

  1. flick one of the child scroll views downward until it reaches the bottom
  2. (even though youve reached the downward boundary) attempt to scroll the child down even more
  3. observe the fact the parent scroll view now scrolls instead, even though the parent scroll view is locked

How can the parent scroll view be truly prevented from scrolling when nested-scroll-view reaches the end of its view?

only noticed this on android, but could possibly exist on ios as well.

here is a minimum reproducible example: https://staging.snack.expo.dev/b3XuJ6iA3

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

等风也等你 2025-01-16 18:21:32

您将多个 ScrollViews 嵌套在 FlatList 中,这是 ScrollView 的便捷包装器(与 文档) ,但您在子项中将 nestedScrollEnabled 设置为 true 滚动视图。您需要在父 ScrollViewFlatList)上设置它。

以下代码完全禁用父 FlatList 的滚动,同时允许在所有嵌套的 ScrollViews 中滚动。

const _renderItem = ({item, index}) => {
    return (
      <View style={{height: 300, borderColor: 'blue', borderWidth: 1, marginBottom: 20}}> 
        <Text> Non scroll View area </Text>
        <ScrollView contentContainerStyle={{height: 500, borderColor: 'red', borderWidth: 3}}></ScrollView>
      </View>
    )
  }
  return (
    <View style={styles.container}>
      <FlatList data={data} renderItem={_renderItem} scrollEnabled={false} nestedScrollEnabled={true}/>
    </View>
  );
}

这是代码的工作 snack ,其中包含上述更改,我已在 Android(三星 Galaxy S9)和 iOS(iPhone 12)上成功测试。

You are nesting multiple ScrollViews inside a FlatList, which is a convenience wrapper around ScrollView (compare with the documentation) , but you are setting nestedScrolEnabled to true in the child ScrollViews. You need to set it on the parent ScrollView (the FlatList).

The following code completely disables scrolling for the parent FlatList while allowing scrolling in all nested ScrollViews.

const _renderItem = ({item, index}) => {
    return (
      <View style={{height: 300, borderColor: 'blue', borderWidth: 1, marginBottom: 20}}> 
        <Text> Non scroll View area </Text>
        <ScrollView contentContainerStyle={{height: 500, borderColor: 'red', borderWidth: 3}}></ScrollView>
      </View>
    )
  }
  return (
    <View style={styles.container}>
      <FlatList data={data} renderItem={_renderItem} scrollEnabled={false} nestedScrollEnabled={true}/>
    </View>
  );
}

Here is a working snack of your code with the changes mentioned above which I have successfully tested on Android (Samsung Galaxy S9) and iOS (iPhone 12).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文