React-native scrollTo超出contentsize

时间:2019-07-17 11:48:27

标签: react-native scroll keyboard scrollview contentoffset

我想实现自动滚动scrollView来避免TextInput的键盘遮挡。我正在听键盘的显示和隐藏,以获取键盘的高度并更改contentOffset中的scrollView

问题:

到目前为止,一切都很好。 我在监听器中获得的最大偏移量是358,因此,当我单击最后一个TextInput时,我想执行keyboardheight + didscrolloffsetY(它已超过358,可能是618 ),他不工作,我想知道该怎么办?用手拖动,即使他已经超出范围,也可以从监听回调中获得更大的偏移量。

代码:

componentDidMount() {
    //..
    this.keyboardDidShowListener = Keyboard.addListener('keyboardDidShow', this._keyboardDidShow.bind(this));
    this.keyboardDidHideListener = Keyboard.addListener('keyboardDidHide', this._keyboardDidHide.bind(this));
  }
<ScrollView
    ref={"ScrollViewref"}
    style={styles.container}
    scrollEnabled={this.state.scrollEnabled}
    showsVerticalScrollIndicator={false}
    keyboardShouldPersistTaps="never"
    onScroll={this._handleScroll.bind(this)}
    scrollEventThrottle={100}
>
    //...
</ScrollView>
_keyboardDidShow(event) {
    this.refs["ScrollViewref"].scrollTo({x: 0, y: event.endCoordinates.height + this.state.didscrolloffsetY, animated: true});})
}
_handleScroll(event) {
    console.log(event.nativeEvent.contentOffset.y)
    this.setState({
        didscrolloffsetY: event.nativeEvent.contentOffset.y,
    })
  }

0 个答案:

没有答案