9

我想用getItemLayout<FlatList />效率更高。

但是当用于onLayout获取不稳定的列表高度时,可能是 128 或 230 或 183。

所以设置itemHeight为 128 成getItemLayout不能正常工作。

有没有其他方法可以让我itemHeight的动态高度进入我的getItemLayout

onLayout(event) {
  const {x, y, height, width} = event.nativeEvent.layout;
  console.log('height:', height); // the height is dynamic
}

renderItem = ({ item }) => {
  return (
    <View onLayout={(event) => this.onLayout(event)} style={styles.floorView} style={styles.itemView}>
      <Text>{item.name}</Text>
    </View>
  );
}

<FlatList
  data={DUMMY_LIST}
  ref={(ref) => { this.flatListRef = ref; }}
  renderItem={this.renderItem}
  numColumns={1}
  keyExtractor={(item, index) => index.toString()}
  getItemLayout={(data, index)=> {
  return { length: itemHeight, offset: itemHeight * index , index };
  }}
/>
4

0 回答 0