【问题标题】:react native Horizontal Scroll on Button click在按钮单击时反应本机水平滚动
【发布时间】:2023-03-24 11:34:01
【问题描述】:

我是 React Native 的初学者。从image 可以看出,我有一个滚动视图和两个按钮。我已经成功实现了滚动视图,它工作正常,但我也希望他们在按下按钮时自动滚动。我尝试了很多搜索,但没有得到任何有效的东西。所以任何帮助表示赞赏。请在下面找到我的代码。

import React, { Component } from 'react';
import { AppRegistry, StyleSheet, Text, View, Dimensions, ScrollView, Button } from 'react-native';

var screenWidth = Dimensions.get('window').width;

export default class App extends React.Component {
  render() {
    return (
      <View style={styles.MainContainer}>
        <View style={styles.ButtonViewContainer}>
          <View style={styles.ButtonContainer}>
            <Button title="Screen 1" />
          </View>
          <View style={styles.ButtonContainer}>
            <Button title="Screen 2" />
          </View>
        </View>
        <ScrollView
          horizontal={true}
          pagingEnabled={true}
          showsHorizontalScrollIndicator={false}
        >
          <View style={styles.ScrollContainer}>
            <Text style={styles.ScrollTextContainer}>
              Screen 1
              </Text>
          </View>
          <View style={styles.ScrollContainer}>
            <Text style={styles.ScrollTextContainer}>
              Screen 2
              </Text>
          </View>
        </ScrollView>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  MainContainer: {
    backgroundColor: '#abe3a8',
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center'

  },
  ScrollContainer: {
    backgroundColor: '#cdf1ec',
    flexGrow: 1,
    marginTop: 0,
    width: screenWidth,
    justifyContent: 'center',
    alignItems: 'center'
  },
  ScrollTextContainer: {
    fontSize: 20,
    padding: 15,
    color: '#000',
    textAlign: 'center'
  },
  ButtonViewContainer: {
    flexDirection: 'row',
    paddingTop: 35,
  },
  ButtonContainer: {
    padding: 30,
  },
});

【问题讨论】:

  • 找不到您指的按钮的点击功能?你想在哪里自动滚动?
  • 现在滚动工作正常.. 但我也有两个按钮,单击它们时应自动滚动到相应的屏幕。 (请查看图片i.stack.imgur.com/HFnuQ.png

标签: android reactjs react-native button scrollview


【解决方案1】:

我不能保证这是最好的方法,因为我没有经常使用 React Native。

this.scroll.scrollTo({ x: calculatedStartPositionOfTheScreen}) 添加到您的按钮按下处理程序 https://facebook.github.io/react-native/docs/scrollview#scrollto

例如

<View>
    ...
    <View style={styles.ButtonContainer}>
        <Button title="Screen 1" onPress={() => { this.scroll.scrollTo({ x: 0 }) }} />
    </View>
    <View style={styles.ButtonContainer}>
        <Button title="Screen 2" onPress={() => { this.scroll.scrollTo({ x: screenWidth }) }} />
    </View>
    <View style={styles.ButtonContainer}>
        <Button title="Screen 3" onPress={() => { this.scroll.scrollTo({ x: screenWidth * 2 }) }} />
    </View>
    ...
    <ScrollView
        horizontal={true}
        pagingEnabled={true}
        showsHorizontalScrollIndicator={false}
        ref={(node) => this.scroll = node}
    >
    ...
    </ScrollView>
</View >

对于大型项目中的这个用例,您也可以考虑 https://reactnavigation.org

【讨论】:

  • 这令人惊讶,也许有一些根本问题,我在这里运行它,它适用于 android 和 IOS,修改了你的代码并添加了 onPress 方法snack.expo.io/r1r_8MJHQ
  • 谢谢..建立了一个新项目并尝试并成功了..我猜我以前的项目有问题..
【解决方案2】:

试试这个。

<View>
    ...
    <View style={styles.ButtonContainer}>
        <Button title="Screen 1" onPress={() => { this.refs.scroll.scrollTo({ x: 0 }) }} />
    </View>
    <View style={styles.ButtonContainer}>
        <Button title="Screen 2" onPress={() => { this.refs.scroll.scrollTo({ x: screenWidth }) }} />
    </View>
    <View style={styles.ButtonContainer}>
        <Button title="Screen 3" onPress={() => { this.refs.scroll.scrollTo({ x: screenWidth * 2 }) }} />
    </View>
    ...
    <ScrollView
        horizontal={true}
        pagingEnabled={true}
        showsHorizontalScrollIndicator={false}
        ref={'scroll'}
    >
    ...
    </ScrollView>
</View >

【讨论】:

    【解决方案3】:

    你可以试试:

    onPress = (index) => {
       this.scroll.scrollTo({x: index * screenWidth, y: 0, animated: true})
    }
    
     <Button title="Screen 1" 
        onPress = {() => this.onPress(0)}
     />
     ...
      <Button title="Screen 2" 
        onPress = {() => this.onPress(1)}
     />
     ...
     <ScrollView
          horizontal={true}
          pagingEnabled={true}
          showsHorizontalScrollIndicator={false}
          ref = {re => this.scroll = re}
     >
     </ScrollView>
    

    【讨论】:

    • 我已按照您的指南进行操作,但单击按钮时出现错误 - 未定义不是函数(正在评估 '_this2.scroll.ScrollToIndex({index:1,animated:true}) ')
    • 仍然错误.. undefined 不是函数.. 检查图像imgur.com/a/06jcRNV
    • 改用:this.scroll.scrollTo({x: index * screenWidth, y: 0, animated: true})
    猜你喜欢
    • 1970-01-01
    • 2016-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-14
    • 1970-01-01
    相关资源
    最近更新 更多