【问题标题】:FlatList inside ScrollView doesn't scrollScrollView 内的 FlatList 不滚动
【发布时间】:2022-01-27 17:42:28
【问题描述】:

我有 4 个FlatLists,maxHeightScrollView 中设置为200

<ScrollView>
  <FlatList/>
  <FlatList/>
  <FlatList/>
  <FlatList/>
</ScrollView>

当我尝试滚动 FlatList 时,它不会滚动,但 ScrollView 会滚动。我该如何解决这个问题?

完整源代码

import { Component, default as React } from 'react';
import { FlatList, ScrollView, Text } from 'react-native';

export  class LabScreen extends Component<{}> {
  render() {
    return (
      <ScrollView>
        {this.renderFlatList('red')}
        {this.renderFlatList('green')}
        {this.renderFlatList('purple')}
        {this.renderFlatList('pink')}
      </ScrollView>
    );
  }

  getRandomData = () => {
    return new Array(100).fill('').map((item, index) => {
      return { title: 'Title ' + (index + 1) };
    });
  };

  renderFlatList(color: string) {
    return (
      <FlatList
        data={this.getRandomData()}
        backgroundColor={color}
        maxHeight={200}
        marginBottom={50}
        keyExtractor={(item, index) => index.toString()}
        renderItem={({ item }) => <Text>{item.title}</Text>}
      />
    );
  }
}

snack.expo link

【问题讨论】:

    标签: react-native


    【解决方案1】:

    我们可以为子 FlatList/ScrollView 组件使用内置的 nestedscrollenabled 属性。

    <FlatList nestedScrollEnabled />
    

    这仅适用于 Android(iOS 默认支持嵌套滚动)。

    【讨论】:

    • 感谢它的帮助,添加到父 和子
    • 我在 iOS 上需要这个用于 ScrollView 中的 FlatList。
    • 不适合我。
    • 对我也不起作用...?
    • 2021 年不工作:(
    【解决方案2】:

    我遇到了一个非常相似的问题,直到我遇到了一个几乎完整的解决方案,这是对 react-native 项目的一个 GitHub 问题的非常有用的评论:https://github.com/facebook/react-native/issues/1966#issuecomment-285130701

    问题在于父组件是唯一注册滚动事件的组件。解决方案是根据印刷机的位置根据上下文决定哪个组件实际上应该处理该事件。

    您需要稍微修改您的结构以:

    <View>
      <ScrollView>
        <View>
          <FlatList />
        </View>
        <View>
          <FlatList />
        </View>
        <View>
          <FlatList />
        </View>
        <View>
          <FlatList />
        </View>
      </ScrollView>
    </View>;
    

    我唯一需要更改 GitHub 评论的地方是使用 this._myScroll.contentOffset 而不是 this.refs.myList.scrollProperties.offset

    我已经修改了您的完整工作示例,允许滚动内部 FlatLists。

    import { Component, default as React } from "react";
    import { View, FlatList, ScrollView, Text } from "react-native";
    
    export default class LabScreen extends Component<{}> {
      constructor(props) {
        super(props);
        this.state = { enableScrollViewScroll: true };
      }
    
      render() {
        return (
          <View
            onStartShouldSetResponderCapture={() => {
              this.setState({ enableScrollViewScroll: true });
            }}
          >
            <ScrollView
              scrollEnabled={this.state.enableScrollViewScroll}
              ref={(myScroll) => (this._myScroll = myScroll)}
            >
              {this.renderFlatList("red")}
              {this.renderFlatList("green")}
              {this.renderFlatList("purple")}
              {this.renderFlatList("pink")}
            </ScrollView>
          </View>
        );
      }
    
      getRandomData = () => {
        return new Array(100).fill("").map((item, index) => {
          return { title: "Title " + (index + 1) };
        });
      };
    
      renderFlatList(color: string) {
        return (
          <View
            onStartShouldSetResponderCapture={() => {
              this.setState({ enableScrollViewScroll: false });
              if (
                this._myScroll.contentOffset === 0 &&
                this.state.enableScrollViewScroll === false
              ) {
                this.setState({ enableScrollViewScroll: true });
              }
            }}
          >
            <FlatList
              data={this.getRandomData()}
              backgroundColor={color}
              maxHeight={200}
              marginBottom={50}
              keyExtractor={(item, index) => index.toString()}
              renderItem={({ item }) => <Text>{item.title}</Text>}
            />
          </View>
        );
      }
    }
    

    希望你觉得这很有用!

    【讨论】:

    • 没问题!我必须自己解决同样的问题,所以我想我不妨分享一下解决方案。
    • 您在这些元素的样式上使用了哪些值?
    • @EricLeFort 2 件事:1. 第二个答案要好得多 2. 在你的代码中:" this.setState({ enableScrollViewScroll: false }); if (this._myScroll.contentOffset === 0 && this.state.enableScrollViewScroll === false) {" setState 是异步函数,并且不能保证在 this.state.enableScrollViewScroll 上获得 false 值之后的那一行,事实上几乎可以肯定它不会有假值
    • 这段代码运行良好,但是当我切换到另一个视图时,它不会滚动,但是当我再次滚动时它会滚动
    • 即使在 2019 年,据我所知,这也是唯一的解决方法。尝试使用“nestedscrollenabled”它无法正常工作
    【解决方案3】:

    尝试将 FlatList 设置为嵌套

    nestedScrollEnabled={true}

    【讨论】:

    • 重复答案:D
    • 所有 ScrollViews 祖先都必须这样做。此外,FlatList 上的“可滚动”功能取决于高度,它设置为:style ={{ height: Dimensions.get('window').height / 1.5}}
    【解决方案4】:

    我解决了嵌套FlatList 无法通过简单地导入FlatList 在android 上滚动项目的问题

    import { FlatList } from 'react-native-gesture-handler';
    

    如果这不起作用,也尝试导入ScrollView

    import { ScrollView } from 'react-native';
    // OR
    import { ScrollView } from 'react-native-gesture-handler';
    

    您需要尝试使用这些导入,至少它在我的情况下有效。

    【讨论】:

    • 我是从 react-native 导入的,这导致了我不变的违规:withNavigation。改成react-native-gesture-handler 解决了!
    • 非常感谢。
    【解决方案5】:

    这是需要零配置的最简单的答案。它就像一个魅力

    <ScrollView horizontal={false}
        <ScrollView horizontal={true}>
            <Flatlist
              ...
            />
        </ScrollView>
    </ScrollView>
    

    【讨论】:

    • 是的,我同意!
    【解决方案6】:

    使用带有 flex:1 的 View 而不是 ScrollView 对我有用。

    【讨论】:

    • 你读过这个问题吗?你给出的答案是不可能的。
    • 别人怎么说都无所谓。在遵循其他答案数小时后,这对我来说非常有效。
    【解决方案7】:

    使用 ma​​p 代替 Flatlist,结果相同且不会破坏应用程序

    Minha conta
       {
          buttonsProfile.map(button => (
             <ArrowButton 
                  key={button.key}
                  title={button.title}
                  iconName={button.icon} 
                  toogle={button.toogle}
                  onPress={() => {navigation.navigate(button.route)}}
              />
          ))
        }
                     
    

    【讨论】:

    • 有一些示例代码可以分享吗?
    • 看下面的例子,把内容当成滚动条
    • 这仅适用于小列表,大列表很容易耗尽内存。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-04
    • 2013-09-19
    • 1970-01-01
    相关资源
    最近更新 更多