initialScrollIndex不适用于FlatList反应原生

sir*_*ous 8 react-native react-native-flatlist

我遇到了FlatList的initialScrollIndex问题 - 只是忽略了initialScrollIndex的参数,并显示了第一个项目.

https://snack.expo.io/Bk1mkK0zZ

小智 8

我使用了 getItemLayout ,一切都很顺利

const getItemLayout=(data, index)=> {
    return { length: wp(100), offset:  wp(100)* index, index };
}

<FlatList
        horizontal
        pagingEnabled
        initialScrollIndex={1}
        snapToAlignment={"start"}
        decelerationRate={"fast"}
        showsHorizontalScrollIndicator={false}
        data={routes}
        getItemLayout={getItemLayout}
        renderItem={rowItem}/>
Run Code Online (Sandbox Code Playgroud)


Spl*_*iid 7

我遇到了完全相同的问题,这就是为什么我找到了您的问题。经过大量测试,我发现了一种解决方法,该方法似乎可行。而不是使用initialScrollIndex我使用的功能scrollToIndex,一旦名单被渲染。将其应用于您的代码,它看起来像

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

const WIDTH = Dimensions.get('window').width;
const HEIGHT = Dimensions.get('window').height;

export default class App extends Component {

  render() {
    const data = [{id: 0},{id: 1},{id: 2},{id: 3},{id: 4}];

     return (
      <View onLayout={() => this.onLayout()}>
        <FlatList
          ref={el => this.list = el}
          data={data}
          renderItem={this.renderItem}
          keyExtractor={item => item.id}

          pagingEnabled={true}
          horizontal={true}
          showsHorizontalScrollIndicator={false}

          getItemLayout={this.getItemLayout}

          debug={true}
        />
      </View>
    )
  }

  onLayout() {
    this.list.scrollToIndex({index: 2})
  }

  renderItem = ({ item }) => {
    return (
      <View style={{flex: 1, backgroundColor: 'lightblue', width: WIDTH, height: HEIGHT, justifyContent: 'center', alignItems: 'center'}}>
        <Text style={{color: 'white', fontSize: 300, fontWeight: 'bold'}}>{item.id}</Text>
      </View>
    );
  };

  getItemLayout = (data, index) => (
    {length: WIDTH, offset: WIDTH * index, index}
  );
}
Run Code Online (Sandbox Code Playgroud)

希望对你有效。

顺便说一句,放入this.list.scrollToIndex({index: 2})componentDidMount出于某种原因对我不起作用,这就是为什么我onLayout改用了

  • 奇怪的是,传入 `initialScrollIndex` 和 `getItemLayout` 仍然导致问题。我很惊讶它没有得到更好的修复或记录!当我使用这两个道具渲染 FlatList 时,它渲染正常,但是当我向上或向下滚动时,项目不会渲染。 (2认同)

Dar*_*jic 5

使用 RN 0.61.5

我已经通过设置解决了这个问题:

  • onContentSizeChange每当更改平面列表数据并呈现所有项目时,它将处理滚动到索引(initialNumToRender如果需要,您可以通过设置处理初始渲染时将呈现的项目数量)

  • onScrollToIndexFailed使用scrollToIndex功能时必须使用的:

<FlatList
    ref={ref => (this.flatList = ref)}
    data={dataArray}
    style={style}
    eyExtractor={(item, index) => String(index)}
    onContentSizeChange={() => {
        if (this.flatList && this.flatList.scrollToIndex && dataArray && dataArray.length) {
            this.flatList.scrollToIndex({  index: dataArray.length - 1 });
        }
    }}
    onScrollToIndexFailed={() => {}}
    renderItem={({ item, index }) => {
        return (
            <Text>Stackoverflow Answer</Text>
        );
    }}
/>
Run Code Online (Sandbox Code Playgroud)

  • 哇,使用`onContentSizeChange`是在SectionList中对我有用的唯一技巧。谢谢! (2认同)