react-native - 如何显示分页 ScrollView 的当前页码?

标签 react-native

ScrollView :

<View style={{flex:1}}>
    <ScrollView
        ref={(component) => {this._scrollView = component}}
        scrollEventThrottle={15}
        removeClippedSubviews={true}
        pagingEnabled={true}
        horizontal={true}
        onScroll={this._onScroll.bind(this)}>
        {items}
   </ScrollView>
   <Text style={styles.legend}>{this.state.currentPage}/{9}</Text
</View>
_onScroll :这个方法计算当前页数是多少

_onScroll(e) {
    let newPageNum = parseInt(e.nativeEvent.contentOffset.x/WINDOW_WIDTH+1);

    newPageNum != this.state.currentPage && this.setState({
        currentPage: newPageNum
    });
}

我认为代码行还可以,但是当我平移 scrollView 时到下一页,它将向后滚动。一旦我删除 _onScroll 中的代码行,问题可以解决,但我无法计算当前页数。

最佳答案

Math.round(parseFloat(event.nativeEvent.contentOffset.x/Dimensions.get('window').width));

会解决你的问题。

关于react-native - 如何显示分页 ScrollView 的当前页码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38799702/

相关文章:

ios - React Native 父子通信

javascript - 如何在 native react 中设置下拉菜单中的默认值

reactjs - DragItemTypes' 未从 'react-data-grid' 导出

react-native - 获取应用程序二进制文件未找到错误排毒测试

android - 导航回屏幕时,隐藏的 React Native 状态栏不起作用 (Android)

javascript - 返回动态对象结构

javascript - 在 Android 上运行时,React native 打包 js 失败

reactjs - 如何将 React 中的 localStorage 代码转换为 React Native 中的 AsyncStorage?

javascript - React-Native:Formik ref 无法获取值(value)

react-native - 使用 ReactNative + Redux-persist V 5.10 时出错