代码之家  ›  专栏  ›  技术社区  ›  flix

React Native-如何检测视图屏幕是否可滚动?

  •  1
  • flix  · 技术社区  · 6 年前

    ScrollView 可见?

    this question 没有回答我的问题

    setState 如果屏幕可滚动

    我试过使用 onLayout

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

    但我得到了 height 属于 view 高度 内容本身的

    1 回复  |  直到 6 年前
        1
  •  2
  •   Rafael Tavares    5 年前

    我不知道如何在一个房间里获得高度 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;
    
    推荐文章