【发布时间】:2019-06-03 03:32:48
【问题描述】:
我正在使用 React Native 的 ScrollView 和 FlatList。
打开键盘后,
我希望看到与打开键盘之前相同的屏幕。
我想我可以根据键盘状态使用scrollTo 方法
它不能正常工作。
有没有类似案例的典型实现方式?
keyboardWillShow(e) {
const { scrollView } = this;
const { scrollPostiion } = this.state;
const { height } = e.endCoordinates;
this.setState({
keyboardHeight: height,
scrollPostiion: scrollPostiion + height,
});
scrollView.scrollTo({ x: 0, y: scrollPostiion + height, animated: false });
}
keyboardWillHide() {
const { scrollView } = this;
const { scrollPostiion, keyboardHeight } = this.state;
this.setState({
keyboardHeight: 0,
scrollPostiion: scrollPostiion - keyboardHeight,
});
scrollView.scrollTo({ x: 0, y: scrollPostiion - keyboardHeight, animated: false });
}
changeNowScrollPosition = (event) => {
this.setState({
scrollPostiion: event.nativeEvent.contentOffset.y,
});
}
<ScrollView
ref={(c) => { this.scrollView = c; }}
keyboardShouldPersistTaps="handled"
pinchGestureEnabled={false}
keyboardDismissMode="interactive"
onScroll={(event) => {
changeNowScrollPosition(event);
}}
onScrollEndDrag={(event) => {
changeNowScrollPosition(event);
}}
scrollEventThrottle={16}
>
【问题讨论】:
-
我通过将 scrollTo 从 KeyboardWillShow 移动到 KeyboardDidShow 解决了这个问题。就我而言,当我使用 KeyboardAvoidingView 时,我无法使用它,因为滚动视图下方有一个空白区域。
标签: react-native react-native-scrollview