react-native - React Native 中可调整大小的 Flex 布局

标签 react-native

如何在 React Native 中创建可调整大小的布局?像这样:
enter image description here

这是一个演示,但适用于 ReactJS:
https://leefsmp.github.io/Re-Flex/index.html

最佳答案

我在实现目标的路上也遇到了这个问题,我解决了这个问题,如下所示。

希望这可以帮助其他有类似情况的人节省时间。

https://github.com/brucelin0325/resizable_layout

MyComponent.js

import React, { Component } from 'react'; 
import { StyleSheet, View, Dimensions, PanResponder, Animated } from 'react-native';

export default class MyComponent extends Component {

    constructor(props) {
        super(props);

        this.state = {
            offset          : 0,
            topHeight       : 40, // min height for top pane header
            bottomHeight    : 40, // min height for bottom pane header,
            deviceHeight    : Dimensions.get('window').height,
            isDividerClicked: false,

            pan             : new Animated.ValueXY()
        }

    }

    componentWillMount() {
        this._panResponder = PanResponder.create({
            onMoveShouldSetResponderCapture: () => true,
            onMoveShouldSetPanResponderCapture: () => true,

            // Initially, set the Y position offset when touch start
            onPanResponderGrant: (e, gestureState) => {
                this.setState({
                    offset: e.nativeEvent.pageY,
                    isDividerClicked: true
                })
            },

            // When we drag the divider, set the bottomHeight (component state) again.
            onPanResponderMove: (e, gestureState) => {

                this.setState({
                    bottomHeight    : gestureState.moveY > (this.state.deviceHeight - 40) ? 40 : this.state.deviceHeight - gestureState.moveY,
                    offset: e.nativeEvent.pageY
                })
            },

            onPanResponderRelease: (e, gestureState) => {
                // Do something here for the touch end event
                this.setState({
                    offset: e.nativeEvent.pageY,
                    isDividerClicked: false
                })
            }
        });
    }


    render() {
        return ( 
            <View style={styles.content}>

                {/* Top View */}
                <Animated.View 
                    style       = {[{backgroundColor: 'pink', minHeight: 40, flex: 1}, {height: this.state.topHeight}]}

                >
                </Animated.View>

                {/* Divider */}
                <View 
                    style={[{height: 10}, this.state.isDividerClicked ? {backgroundColor: '#666'} : {backgroundColor: '#e2e2e2'}]} 
                    {...this._panResponder.panHandlers}
                >
                </View>

                {/* Bottom View */}
                <Animated.View 
                    style={[{backgroundColor: 'green', minHeight: 40}, {height: this.state.bottomHeight}]} 

                >
                </Animated.View>
            </View>
        )
    }
}

const styles = StyleSheet.create({
    content         : {
        flex        : 1,
        flexDirection: 'column'
    },
})

关于react-native - React Native 中可调整大小的 Flex 布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52527803/

相关文章:

javascript - 如何在 react-native 中使用 RCTEventEmitter 接收事件回调

javascript - promise – 找不到变量过程

javascript - 返回 React 组件中索引中的第一项

react-native - 我可以在 React Native 中使用什么数据库

reactjs - 在airbnb/react-native-maps中自定义我的位置按钮

javascript - 在 React Native 中迭代 JSON

reactjs - React-Native Image 提供给图像的 Prop 'source' 无效

react-native - 修改react-native-chart-kit中标签的宽度

react-native - 带有标题/后退按钮的 react 导航模式

javascript - React-Native 如何设置 View 后的整体背景色