React Native FlatList的scrollToEnd()不起作用

Jus*_*wan 0 javascript android reactjs react-native

我想制作一个浮动操作按钮来自动将 FlatList 滚动到底部。我的 FlatList 组件描述如下:

<FlatList
  ref={(ref) => { this.flatListRef = ref; }}
  contentContainerStyle={this.state.barangFiltered.length === 0 && { paddingTop: 10, justifyContent: 'center', alignItems: 'center' }}
  showsVerticalScrollIndicator={false}
  data={this.state.barangFiltered}
  keyExtractor={(item, index) => `barang-${index}`}
  renderItem={({ item, index }) => <Barang
    item={item}
    index={index}
    navigation={this.props.navigation}
    duplicateBarang={(item) => this._duplicateBarang(item)}
    deleteBarang={(item) => this._deleteBarang(item)}
  />}
  ListEmptyComponent={<Text style={{ fontSize: 16, fontWeight: '400' }}>List is empty</Text>}
  getItemLayout={(data, index) => { return {length: 200, index, offset: 200 * index} }}
/>
Run Code Online (Sandbox Code Playgroud)

以及按钮组件:

<TouchableOpacity
  activeOpacity={0.7}
  style={styles.fab}
  onPress={() => this.flatListRef.scrollToEnd()}
>
  <Ionicons name="ios-add" size={32} color="#0035C9" />
</TouchableOpacity>
Run Code Online (Sandbox Code Playgroud)

但是,每当我按下按钮时,它不会执行任何操作,而不会返回任何错误。这里出了什么问题?

Our*_*abi 5

对于那些仍在寻找解决方案的人来说,scrollToEnd()它不起作用,因为它是在对FlatList. 因为它继承了ScrollView这里最好的方法是像这样scrollToEnd()调用:onContentSizeChange

<FlatList ref = "flatList" onContentSizeChange={()=> this.refs.flatList.scrollToEnd()} />