如果高度是动态的,如何将每个项目的高度放入 getItemLayout 中?

Mor*_*ton 9 react-native react-native-flatlist react-native-sectionlist

我想用来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 };
  }}
/>
Run Code Online (Sandbox Code Playgroud)