【问题标题】:How to Scroll Right To Columns in React Native FlatList (2018)如何在 React Native FlatList (2018) 中向右滚动到列
【发布时间】:2018-04-15 23:35:14
【问题描述】:

我正在寻找一种在这样的表格上滚动视口的方法,除了每个单元格的大小完全相同:

我目前正在使用FlatListnumColumns 参数来制作表格并将视口滚动到该表格上。

这是一个小吃示例 - RegularGridExample:

import React from 'react';
import { FlatList, Text, View } from 'react-native';

const numRows = 10,
  numColumns = 10,
  width = 100,
  height = 100,
  cells = [...Array(numRows * numColumns)].map((_, cellIndex) => {
    const rowIndex = Math.floor(cellIndex / numRows),
      colIndex = cellIndex % numColumns;
    return {
      key: `${colIndex},${rowIndex}`,
      rowIndex,
      colIndex,
      styles: {
        width,
        height,
        backgroundColor: 'green',
        borderColor: 'black',
        borderWidth: 1,
      },
    };
  });

export default class RegularGridExample extends React.Component {

  render() {
    return (
      <FlatList
        data={cells}
        renderItem={this.renderItem}
        numColumns={numColumns}
        horizontal={false}
        columnWrapperStyle={{
          borderColor: 'black',
          width: numColumns * width,
        }}
      />
    );
  }

  renderItem = ({ item: { styles, rowIndex, colIndex } }) => {
    return (
      <View style={styles}>
        <Text>r{rowIndex}</Text>
        <Text>c{colIndex}</Text>
      </View>
    );
  };
}

此示例将正确滚动以显示视口下方的行,但不会滚动以显示视口之外的列。如何启用滚动视口以显示FlatList 的列?

更新 1

我不认为这可以通过嵌套的FlatLists 轻松解决,这是我在使用上述numColumns 方法之前尝试的第一件事。这里的用例是将视口移动到比视口大的网格上,而不仅仅是在视口内滚动一行。

更新 2

我正在寻找一种虚拟化解决方案。虽然上面的线框使用文本,但我真正感兴趣的用例是浏览一个 tile 服务器,该服务器在 50MB+ 大图像的一部分上导航。将它们全部加载到滚动视图中会太慢。

不相关的堆栈溢出帖子

【问题讨论】:

  • 目前,您需要双向滚动,但 numColumns 样式无法实现。您可能需要两个 FlatLists 或两个 ScrollViews 或两者的混合,只要合适。

标签: react-native react-native-flatlist


【解决方案1】:

这不能使用FlatList 方法完成,因为使用了numColumns,它显式设置了horizontal={false},因此禁用滚动水平方向

这是使用nested ScrollViews的解决方法

export default class RegularGridExample extends React.Component {
    render() {
        const generatedArray = [1,2,3,4,5,6]

        return (
            <ScrollView horizontal>
                <ScrollView >
                    {generatedArray.map((data, index) => {
                       return <View style={{flexDirection: 'row'}} >
                           {generatedArray.map((data, index) => {
                              return  <View style={{height: 100, width: 100, backgroundColor: 'red', borderWidth: 1, borderColor: 'black'}} />
                           })}
                       </View>
                    })}
                </ScrollView>
            </ScrollView>
        );
    }
}

【讨论】:

  • 感谢您的帖子。不幸的是,我不认为嵌套 FlatLists 解决了用例:大于 X 和 Y 上的视口的网格需要能够将整个视口 移到网格中任一维度。使用嵌套的FlatLists,垂直滑动可以正确地上下移动视口,但水平滑动只能滚动一行。
  • 是的,但是由于您使用的是 numColumns 道具,因此水平滚动被锁定,并且无法在文档中提到的 Flatlist 中进行。同时我会想办法。
  • 有趣的是,我在文档中没有看到任何暗示水平滚动被numColumns prop 锁定的内容。你能提供一个链接吗?这是the numColumns documentation I've seen“只能使用horizo​​ntal={false} 呈现多列,并且会像flexWrap 布局一样曲折。所有项目都应具有相同的高度 - 不支持砖石布局。” 这并不意味着我会禁用滚动。
  • horizo​​ntal={false} 在 FlatList 继承的 ScrollView 属性上意味着它不能水平滚动。 当为 true 时,滚动视图的子视图水平排列成一行,而不是垂直排列成一列。默认值为 false。
  • 这里是指向ScrollView.horizontal 的链接以及您提到的报价。我想我明白你的意思 - 通过将其标记为 horizontal={false} 我正在积极禁用水平方向的滚动。感谢您向我指出这一点。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-03-23
  • 1970-01-01
  • 1970-01-01
  • 2021-05-10
  • 1970-01-01
  • 1970-01-01
  • 2020-07-07
相关资源
最近更新 更多