我正在使用 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}
>