【问题标题】:react-native-swiper method scrollByreact-native-swiper 方法 scrollBy
【发布时间】:2026-01-07 05:00:02
【问题描述】:

如果单击索引 0,该方法将不起作用。如果单击索引 0,则 1 和 2 将停止工作。如果我在 1 或 2 上单击 2 次,则滑块将转到所需的幻灯片。但是零索引根本不起作用。请告诉我,可能是什么问题?

   <Swiper
        onMomentumScrollEnd={(e, state, context) => this.setState({index: 
        state.index})}
        ref={(swiper) => {this.swiper = swiper;}}
        showsButtons={false}
        width={500}
        height={500}
        showsPagination={false}
        index={0}
        loop={true} >
        <View>
            <Text>One</Text>
        </View>
        <View>
            <Text>Two</Text>
        </View>
        <View>
            <Text>Three</Text>
        </View>
    </Swiper>
    <Text onPress={()=>{this.swiper.scrollBy(0, true)}}>One</Text>
    <Text onPress={()=>{this.swiper.scrollBy(1, true)}}>Two</Text>
    <Text onPress={()=>{this.swiper.scrollBy(2, true)}}>Three</Text>

【问题讨论】:

  • scrollBy 按给定索引滚动(相对于当前索引)。这就是scrollBy(0) 不起作用的原因。
  • 感谢您解释方法是如何安排的

标签: reactjs react-native swiper


【解决方案1】:

我做了这个方法:

onClickScroll(index){
    let currentIndex = this.state.index;

    if(currentIndex !== index) {
        let resultSlide = undefined;
        let countSlides = this.state.itineraryDaysListItem.length;

        if(index > currentIndex && index !== countSlides)
        {
            resultSlide = index - currentIndex;
            this.swiper.scrollBy(resultSlide, true);
        }
        else if(index>currentIndex && index === countSlides)
        {
            resultSlide = currentIndex+1;
            this.swiper.scrollBy(resultSlide, true);
        }
        else if(index < currentIndex && index !== 0){
            resultSlide = (currentIndex - index) * (-1);
            this.swiper.scrollBy(resultSlide, true);
        }
        else if(index < currentIndex && index === 0){
            resultSlide = currentIndex * (-1);
            this.swiper.scrollBy(resultSlide, true);
        }
    }
}               

【讨论】:

    【解决方案2】:

    以下实现对我有用

        'use strict'
    import React, { Component } from 'react';
    import {
      AppRegistry,
      StyleSheet,
      Text,
      View
    } from 'react-native';
    
    import Swiper from 'react-native-swiper';
    
    var styles = StyleSheet.create({
      wrapper: {
      },
      slide1: {
        flex: 1,
        justifyContent: 'center',
        alignItems: 'center',
        backgroundColor: '#9DD6EB',
      },
      slide2: {
        flex: 1,
        justifyContent: 'center',
        alignItems: 'center',
        backgroundColor: '#97CAE5',
      },
      slide3: {
        flex: 1,
        justifyContent: 'center',
        alignItems: 'center',
        backgroundColor: '#92BBD9',
      },
      text: {
        color: '#fff',
        fontSize: 30,
        fontWeight: 'bold',
      }
    })
    export default class swiper  extends Component{
      constructor (props) {
        super(props)
        this.swiperRef = swiper => this.swiper = swiper
        this.scrollHandler = page => {
          console.log ('Page ',page,this.swiper)
          this.swiper && this.swiper.scrollBy(page, true)
        }
      }
      render (){
        return (
          <Swiper
            ref={ this.swiperRef }
            showsButtons={false}
            width={500}
            height={500}
            showsPagination={false}
            index={0}
            loop={true} >
            <View style={ styles.slide1 }>
                <Text style={ styles.text } onPress={()=>{console.log('Page 0'); this.swiper.scrollBy(1, true)}}>One</Text>
            </View>
            <View style={ styles.slide2 }>
                 <Text style={ styles.text } onPress={()=>{console.log('Page 1'); this.swiper.scrollBy(1, true)}}>Two</Text>
            </View>
            <View style={ styles.slide3 } >
                <Text style={ styles.text } onPress={()=>{console.log('Page 2');this.swiper.scrollBy(2, true)}}>Three</Text>
            </View>
        </Swiper>
        )
      }
    }
    
    
    AppRegistry.registerComponent('myproject', () => swiper);
    

    【讨论】:

    • 这不是我所需要的。我想实现自定义导航。无限数量的元素。例如,我们在第五个元素上,点击零元素——点击零元素。但谢谢你的想法。据我了解,如果我们知道当前索引以及我们想要到达的索引,我需要编写计算所需元素的逻辑。
    【解决方案3】:

    适用于 2019 年阅读本文并使用功能组件/钩子编写的任何人。

    import React, { useRef } from 'react'
    import Swiper from 'react-native-swiper'
    
    const YourCoolComponent = () => {
      const swiper = useRef(null)
      return (
        <Swiper ref={swiper}>
          <View>
            <Text>Page 1</Text>
            <Button onPress={() => swiper.current.scrollBy(1)}>Forward</Button>
          </View>
          <View>
            <Text>Page 2</Text>
            <Button onPress={() => swiper.current.scrollBy(-1)}>Backward</Button>
          </View>
        </Swiper>
      )
    }
    

    【讨论】:

      【解决方案4】:

      这对我有用。

      export default class Navigator extends Component {
      constructor(props) {
          super(props);
          this.onPressActiveButton = this.onPressActiveButton.bind(this);
          this.state = {
              idxActive: 0
          }
      }
      getIdxActive = () => {
          return this.state.idxActive
      }
      onPressActiveButton = (index) => {
          this.refs._swiper.scrollBy(index - this.state.idxActive, true);
      }
      onIndexChanged(index){
          this.setState({
              idxActive: index
          });
      }
      render() {
          return (
              <View style={styles.container}>
                  <Toolbar
                      buttons={tabButtons}
                      color={getColor('#0A5F22')}
                      getIndex={this.getIdxActive}
                      onPressActiveButton={this.onPressActiveButton} />
                  <Swiper
                      ref="_swiper"
                      loop={true}
                      showsPagination={false}
                      onIndexChanged={this.onIndexChanged.bind(this)}>
                      <SmartAlarm />
                      <ClockView />
                      <TimerView />
                      <StopWatchView />
                  </Swiper>
              </View>
          )
      }
      

      }

      【讨论】:

        【解决方案5】:

        如果你的屏幕是分开的组件

        你可以使用 useRef 和 forwardRef

        父母刷卡

        import React, {useState , useRef , forwardRef} from 'react';
        import Swiper from 'react-native-swiper';
        import ScreenOne from "./screens/ScreenOne";
        import ScreenTow from "./screens/ScreenTow";
        
        const Parent = (props) => {
        const swiper = useRef(null);
        return (
           <Swiper ref={swiper}>
              <ScreenOne ref={swiper} />
              <ScreenTow ref={swiper}/>
           </Swiper>
        );}
        export default Parent;
        

        ScreenOne 组件

        import React, {forwardRef} from 'react';
        const ScreenOneComponent = (props , ref) => {
        return (
           <View>
              <Button title ='move to screen 1' onPress={() => ref.current.scrollBy(1)}/>
          </View>
        );}
        
        const ScreenOne = forwardRef(ScreenOneComponent);
        
        export default ScreenOne;
        

        【讨论】: