簡體   English   中英

在本機中滾動到ScrollView組件底部時調用事件

[英]Call event when scroll to bottom of ScrollView component in react native

我有個問題。 如何在ScrollView底部向下滾動反應原生時調用函數? 請幫我。 非常感謝!

有效!

var windowHeight = Dimensions.get('window').height,
          height = e.nativeEvent.contentSize.height,
          offset = e.nativeEvent.contentOffset.y;
if( windowHeight + offset >= height ){
    console.log('End Scroll')
}

您應該將一個函數傳遞給ScrollView onScroll prop。

更多信息可以在這里找到。

constructor(props){
    super(props)

    this._onScroll = this._onScroll.bind(this)
    this.state = {
      contentOffsetY: 0
    }
  }

_onScroll(e){
    var contentOffset = e.nativeEvent.contentOffset.y;
    this.state.contentOffsetY < contentOffset ? console.log("Scroll Down") : console.log("Scroll Up");
    this.setState({contentOffsetY: contentOffset});
  }

  render(){
    var items = ["1","2","3","4","5"];
    return(
      <ScrollView style={{flex: 1}} onScroll={this._onScroll}>
        {items.map((item, i) => {
          return (
            <View key={i} style={{height: 200}}>
              <Text>{item}</Text>
            </View>
          )
        })}
      </ScrollView>
    )
  }

請使用ScrollView的這兩種方法完整和最終。 不要使用ónScroll',因為它會在一個滾動條上多次調用。

onStartScrollList = () => {
    this.setState({ isListScrolled: true });
    console.log('onScrollList');
}

onStopScrollList = () => {
    console.log('onStopScrollList');
    this.setState({ isListScrolled: false });

}

現在在'loadMore'方法中檢查'isListScrolled'。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM