React Native - FlatList 无法到达底部
React Native - FlatList unable to reach bottom
我有一个键盘始终处于打开状态的面板,因为我不希望用户关闭它。在该面板中,我有一个如下所示的 FlatList:
<KeyboardAvoidingView style={{ flex: 1 }}>
<FlatList
// This keeps the keyboard up and disables the user's ability to hide it.
keyboardShouldPersistTaps="handled"
data={this.state.examples}
keyExtractor={(item, index) => index.toString()}
renderItem={this._renderItem}
contentContainerStyle={{ flex: 1}}
/>
</KeyboardAvoidingView>
到目前为止一切顺利,我已经实现了我想要的。但是,当键盘打开时 - 它隐藏了 FlatList 呈现的项目的底部。并且用户无法向上滚动并查看最后的项目,因为他们留在键盘后面。
如何在能够查看和滚动 FlatList 的全部内容的同时保持键盘打开(并禁用被关闭的能力)?
我也遇到过类似情况。我在右下角有一个底部浮动操作按钮,稍微隐藏了最后一项。
因此,我在列表的末尾添加了一个 假空白项目 以便我可以将其向上滚动一点。
这很简单也很棘手。我希望它也适用于您,如果您添加一些空白项目或一个足够宽的空白项目。
编辑 1:
假设您的数据数组是这样的:[{title: "Item 1"}, {title: "Item 2"}]
您必须将新的空白项目连接到数据数组,同时将其传递给 <FlatList>
,如下所示:
<FlatList
keyboardShouldPersistTaps="handled"
data={this.state.examples.concat({title:"\n\n\n\n\n"})}
keyExtractor={(item, index) => index.toString()}
renderItem={this._renderItem}
contentContainerStyle={{ flex: 1}}/>
调整“\n”的数量,直到您可以滚动列表使其可见。必须有最低金额。并确保您的 _renderItem
没有将项目高度设置为固定值。
您可以添加一个键盘监听事件来获取键盘的高度。
this.keyboardWillShowListener = Keyboard.addListener('keyboardWillShow', (e) => {
this.setState({ keyboardHeight: e.endCoordinates.height, keyboardShow: true })
Animated.timing(this.visibleHeight, {
duration: e.duration,
toValue: 1,
easing: Easing.inOut(Easing.ease)
}).start()
})
像这样查看代码
<Animated.View style={Platform.OS === 'android' ? { flex: 1 } : {
height: this.visibleHeight.interpolate({
inputRange: [0, 1],
outputRange: [height - this.NavHeaderHeight, height - this.state.keyboardHeight - this.NavHeaderHeight]
})
}
} >
/*Your FlatList*/
</Animated.View>
希望对你有用
我有一个键盘始终处于打开状态的面板,因为我不希望用户关闭它。在该面板中,我有一个如下所示的 FlatList:
<KeyboardAvoidingView style={{ flex: 1 }}>
<FlatList
// This keeps the keyboard up and disables the user's ability to hide it.
keyboardShouldPersistTaps="handled"
data={this.state.examples}
keyExtractor={(item, index) => index.toString()}
renderItem={this._renderItem}
contentContainerStyle={{ flex: 1}}
/>
</KeyboardAvoidingView>
到目前为止一切顺利,我已经实现了我想要的。但是,当键盘打开时 - 它隐藏了 FlatList 呈现的项目的底部。并且用户无法向上滚动并查看最后的项目,因为他们留在键盘后面。
如何在能够查看和滚动 FlatList 的全部内容的同时保持键盘打开(并禁用被关闭的能力)?
我也遇到过类似情况。我在右下角有一个底部浮动操作按钮,稍微隐藏了最后一项。
因此,我在列表的末尾添加了一个 假空白项目 以便我可以将其向上滚动一点。
这很简单也很棘手。我希望它也适用于您,如果您添加一些空白项目或一个足够宽的空白项目。
编辑 1:
假设您的数据数组是这样的:[{title: "Item 1"}, {title: "Item 2"}]
您必须将新的空白项目连接到数据数组,同时将其传递给 <FlatList>
,如下所示:
<FlatList
keyboardShouldPersistTaps="handled"
data={this.state.examples.concat({title:"\n\n\n\n\n"})}
keyExtractor={(item, index) => index.toString()}
renderItem={this._renderItem}
contentContainerStyle={{ flex: 1}}/>
调整“\n”的数量,直到您可以滚动列表使其可见。必须有最低金额。并确保您的 _renderItem
没有将项目高度设置为固定值。
您可以添加一个键盘监听事件来获取键盘的高度。
this.keyboardWillShowListener = Keyboard.addListener('keyboardWillShow', (e) => {
this.setState({ keyboardHeight: e.endCoordinates.height, keyboardShow: true })
Animated.timing(this.visibleHeight, {
duration: e.duration,
toValue: 1,
easing: Easing.inOut(Easing.ease)
}).start()
})
像这样查看代码
<Animated.View style={Platform.OS === 'android' ? { flex: 1 } : {
height: this.visibleHeight.interpolate({
inputRange: [0, 1],
outputRange: [height - this.NavHeaderHeight, height - this.state.keyboardHeight - this.NavHeaderHeight]
})
}
} >
/*Your FlatList*/
</Animated.View>
希望对你有用