React Native - Как обнаружить экран с возможностью прокрутки? - PullRequest
0 голосов
/ 12 октября 2018

Основной вопрос: как сохранить полосу прокрутки ScrollView видимой?

и на этот вопрос на мой вопрос нет ответа

аналогичный вопрос: Как я могу обнаружитьесли содержимое высоты просмотра больше, чем высота устройства?так что я могу setState, если экран можно прокручивать

Я пытался использовать onLayout с этим кодом:

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

, но я получил height из view в устройствеэкран, а не height самого контента

Можно ли ответить на мой вопрос?

1 Ответ

0 голосов
/ 12 октября 2018

Я не знаю, как получить высоту внутри 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;
...