I am using React Native's ScrollView
and FlatList
.
When the keyboard is opened,
I would like to see the same screen as before opening the keyboard.
I thought I could use the scrollTo
method depending on the keyboard state
It does not work properly.
Is there a typical implementation of a similar case?
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}
>