【问题标题】:React Native - How to detect view screen is scrollable?React Native - 如何检测视图屏幕是否可滚动?
【发布时间】:2019-03-17 07:42:14
【问题描述】:

主要问题:如何保持ScrollView的滚动条可见?

this question 没有回答我的问题

类似问题:如何检测视图高度的内容是否高于设备高度?所以如果屏幕是可滚动的,我可以setState

我已尝试将onLayout 与此代码一起使用:

<View onLayout={(event)=>{var {x, y, width, height}=event.nativeEvent.layout}}>
  //Long Content higher than device height
</View>

但我在设备屏幕中得到了viewheight,而不是内容本身的height

有没有办法回答我的问题?

【问题讨论】:

  • 也许这个解决方案可以帮助你stackoverflow.com/a/41398953/2083099
  • @Nima 我使用了您给出的链接的最高投票,但就像我说的,onLayout 本身只显示设备屏幕的高度,而不是内容

标签: react-native scrollview dimension


【解决方案1】:

我不知道如何在View 中获取高度,但在ScrollView 中你可以使用onContentSizeChange

import React, { Component } from "react";
import { View, Text, StyleSheet, Dimensions, ScrollView, Image } from "react-native";

const images =
    [
        { image: 'https://www.lens-rumors.com/wp-content/uploads/2014/10/Nikon-AF-S-DX-Nikkor-18-140mm-f3.5-5.6G-ED-VR-sample-images1.jpg', text: 'hello' },
        { image: 'https://www.lens-rumors.com/wp-content/uploads/2014/10/Nikon-AF-S-DX-Nikkor-18-140mm-f3.5-5.6G-ED-VR-sample-images1.jpg', text: 'hello' },
        { image: 'https://www.lens-rumors.com/wp-content/uploads/2014/10/Nikon-AF-S-DX-Nikkor-18-140mm-f3.5-5.6G-ED-VR-sample-images1.jpg', text: 'hello' },
        { image: 'https://www.lens-rumors.com/wp-content/uploads/2014/10/Nikon-AF-S-DX-Nikkor-18-140mm-f3.5-5.6G-ED-VR-sample-images1.jpg', text: 'hello' }];

class Test extends Component {
    find_dimesions(width,height) {
        const deviceHeight = Dimensions.get("window").height;
        const deviceWidth = Dimensions.get("window").width;
        console.log(" view width:" + width + "  " + "view height:" + height);
        console.log(
            "device width:" + deviceWidth + "  " + " device height:" + deviceHeight
        );
    }

    render() {
        return (
            <ScrollView
            onContentSizeChange={(width, height) => {
                this.find_dimesions(width,height)
              }}
                style={styles.container}
            >
                {images.map((data, index) => {
                   return <View key={index} style={{ flex: 1 }}>
                        <Image style={{height:200,width:200}} source={{ uri: data.image }} />
                    </View>
                })}
            </ScrollView>
        );
    }
}

const styles = StyleSheet.create({
    container: {
        flex:1,
        // justifyContent: "center",
        // alignItems: "center",
        backgroundColor: "#F5FCFF",
        
    }
});

export default Test;

【讨论】:

  • 你知道我的问题吗? >如何检测视图高度的内容是否高于设备高度?将viewheight 更改为低于height 的设备没有意义
  • 正如我所说的更新代码,您可以基于此 stackoverflow.com/a/36736197/2083099 为滚动视图使用 onContentSizeChange()
  • 它比上次更新的要好,我可以将你的代码与我的代码混合以使其工作
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-12
相关资源
最近更新 更多