【问题标题】:React Native Scrolling Screen with ScrollView from a Within a Component使用组件内的 ScrollView 反应本机滚动屏幕
【发布时间】:2020-11-27 18:09:08
【问题描述】:

在我的 React Native 项目中,我有一个带有 ScrollView 的屏幕,其中包含许多已定义的区域。在同一个屏幕中,我还有一个组件可以在 ScrollView 之上创建一个浮动视图。

在这个浮动框中,我有一个按钮。如果用户单击此按钮,主屏幕应向下滚动到 ScrollView 中的特定组件。有可能吗,我该怎么做?

App.js

import * as React from 'react';
import { Text, View, StyleSheet, ScrollView, Dimensions, Pressable } from 'react-native';
import Constants from 'expo-constants';

import Guide from './components/Guide';

import { Card } from 'react-native-paper';

export default function App() {
  return (
    <>
    <ScrollView style={styles.container}>
      <Card style={{margin:20,padding:20}}>
        <Text style={{textAlign:'center'}}>AREA 1</Text>
      </Card>
      <Card style={{margin:20,padding:20}}>
        <Text style={{textAlign:'center'}}>AREA 2</Text>
      </Card>
      <Card style={{margin:20,padding:20}}>
        <Text style={{textAlign:'center'}}>AREA 3</Text>
      </Card>
      <Card style={{margin:20,padding:20}}>
        <Text style={{textAlign:'center'}}>AREA 4</Text>
      </Card>
      <Card style={{margin:20,padding:20}}>
        <Text style={{textAlign:'center'}}>AREA 5</Text>
      </Card>
      <Card style={{margin:20,padding:20}}>
        <Text style={{textAlign:'center'}}>AREA 6</Text>
      </Card>
      <Card style={{margin:20,padding:20}}>
        <Text style={{textAlign:'center'}}>AREA 7</Text>
      </Card>
      <Card style={{margin:20,padding:20}}>
        <Text style={{textAlign:'center'}}>AREA 8</Text>
      </Card>
      <Card style={{margin:20,padding:20}}>
        <Text style={{textAlign:'center'}}>AREA 9</Text>
      </Card>
      <Card style={{margin:20,padding:20}}>
        <Text style={{textAlign:'center'}}>AREA 10</Text>
      </Card>
    </ScrollView>
    <Guide />
    </>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,

    paddingTop: Constants.statusBarHeight,
    backgroundColor: '#FFC300',
    padding: 8,
  },

  paragraph: {
    margin: 24,
    fontSize: 18,
    fontWeight: 'bold',
    textAlign: 'center',
  },
});

下面是指南组件。

Guide.js

import * as React from 'react';
import { Text, View, StyleSheet, Image, Dimensions, Pressable } from 'react-native';

    export default function Guide() {
      return (
        <View style={styles.context}>
        <Pressable onPress={() => {
              alert('This should scroll down to Area 8');
            }}>
          <View style={styles.blockWrapper}>
            <Text style={styles.textStyle}>Scroll to Area 8</Text>
          </View>
          </Pressable>
        </View>
      );
    }
    
    const styles = StyleSheet.create({
        context: {
        position: 'absolute',
        width: Dimensions.width,
        height: Dimensions.height,
        top: 0,
        left: 0,
        right: 0,
        bottom: 10,
        zIndex: 1000,
        justifyContent: 'flex-end',
      },
      blockWrapper: {
        width: '100%',
        padding: 0,
        backgroundColor: '#5C93D8',
        paddingHorizontal: 20,
        paddingVertical: 15,
        // marginBottom: 20,
        borderRadius: 20,
        justifyContent: 'center',
        minHeight:100
      },
    });

Guide 生成的按钮漂浮在 ScrollView 上。当我点击它时,主屏幕应该向下滚动到区域 8。我不知道如何实现这一点。我已经看到一些关于在滚动上使用 ref 的线程,但找不到很好的解释。你能帮忙吗?

你可以在这里看到同样的小吃:https://snack.expo.io/lRl8ffSWo。请注意,在我的实际项目中,我没有使用 Expo。

【问题讨论】:

    标签: javascript react-native


    【解决方案1】:

    您可以使用 ref 来做到这一点。 &lt;ScrollView ref={yourRef} ... /&gt; 和 onClick 你可以滚动到某个位置。 ref.current.scrollTo({ y: &lt;offset&gt; }) 但有更好的方法,您可以使用 FlatList 而不是 ScrollView,您可以只传递索引而不是偏移量。 &lt;FlatList ref={yourRef} ... /&gt;,您可以使用 ref.current.scrollToIndex({ index: &lt;your_index&gt; }) 滚动到某个索引。

    【讨论】:

    • 在 ScrollView 本身中,是否可以获取渲染元素的偏移量(在本例中为区域 8)然后滚动到该位置?此外,我需要完全从另一个组件滚动。我明白你在说什么,但我不确定如何以正确的方式实现它。
    • snack.expo.io/UoumJa9eE我更新了你的零食,有一个如何滚动到第四张卡片的例子,我超过了300(从顶部偏移)因为每张卡片的高度大约为50,它们之间的间距为40 .. 所以 3x50 + 3x40 和 + 20(第一张卡片上方的空间)。
    • @asanas 好的,如果您想从另一个组件滚动,您需要将所需的索引作为道具传递,在您的情况下为 8 并计算偏移量。假设每张卡片的高度为 50,您需要隐藏 7,因为第 8 个必须在顶部,您可以使用公式:const offset = (desiredIndex - 1) * 50 + (desiredIndex - 1) * 40;
    • 谢谢。我可以通过索引。但是如何将 ref 传递给另一个组件,以及如何从该其他组件调用 ref.current.scrollTo()。你能帮我吗?我有指南组件。如果你能告诉我如何让它与那个组件一起工作......
    • @asanas 很高兴为您提供帮助。当你想将 ref 作为 prop 传递给其他组件时,你需要使用 forwardRef。检查doc
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-28
    • 1970-01-01
    • 2020-12-24
    • 1970-01-01
    相关资源
    最近更新 更多