javascript - React Native - 滑动以隐藏和显示新内容

标签 javascript android ios react-native

我正在 React Native 应用程序上创建项目列表。我想向左和向右滑动此列表中的每个项目。因此,当我向左滑动时,我想隐藏已呈现的 View 并显示新元素。此外,当我向右滑动时,我想显示其他元素,这些元素将与我向左滑动时呈现的元素不同。我找到了这个名为“react-native-swipe-gestures”的库,但我不知道如何用它显示和隐藏元素。我声明了一些项目,但是当我尝试显示它时,我得到了一个错误“找不到变量‘item’”,也许你会向我解释我应该如何使用它来实际左右滑动。

import React, {Component} from 'react';
import {
    ScrollView,
    Text,
    View,
    Image,
    Button
} from 'react-native';
import GestureRecognizer, {swipeDirections} from 'react-native-swipe-gestures';
import {List, ListItem} from "react-native-elements";

class Offers extends Component {

    constructor(props) {
        super(props);
        this.state = {
            myText: '',
            gestureName: 'none',
            icons: '',
            guardian: '',
            area: '',
            rooms: '',
            floor: '',
            market: '',
            year: '',
            pricePerMeter: '',
        };
    }


    onSwipeRight(gestureState) {
        this.setState({myText: 'You swiped right!'});
    }

    onSwipe(gestureName, gestureState) {
        const {SWIPE_LEFT, SWIPE_RIGHT} = swipeDirections;
        this.setState({gestureName: gestureName});
        switch (gestureName) {
            case SWIPE_LEFT:
                this.setState({backgroundColor: 'blue'});
                break;
            case SWIPE_RIGHT:
                this.setState({backgroundColor: 'yellow'});
                break;
        }
    }

    onSwipeLeft(gestureState) {
        this.setState({
            guardian: item.offerGuardian, //items from const offers
            area: item.offerArea,
            floor: item.offerFloor,
            rooms: item.offerRooms,
            market: item.offerMarket,
            year: item.offerYear,
            pricePerMeter: item.offerPricePerMeter,
        })
    }

    render() {

        const config = {
            velocityThreshold: 0.3,
            directionalOffsetThreshold: 80
        };


        const offers = [
            {
                offerNumber: 'TEST912697',
                offerLocation: 'Warszawa Białołęka',
                offerStreet: 'ul. Bruszewska',
                offerType: 'Mieszkanie',
                offerStatus: 'Akt. Wewnętrzna',
                offerStatusColor: '#0FBEB2',
                offerAddDate: '2017-09-20 12:08:06',
                offerPrice: '2 450 000',
                photo: 'https://static.pexels.com/photos/164516/pexels-photo-164516.jpeg',
                offerGuardian: 'Adam Borek',
                offerTransactionType: 'sprzedaż',
                offerArea: '50 m2',
                offerRooms: '2 pokoje',
                offerFloor: '1 z 2',
                offerYear: '2005 rok',
                offerMarket: 'rynek pierwotny',
                offerPricePerMeter: '5000 zł/m2'
            },
        ];

        return (
            <ScrollView>
                <View style={{
                    flexDirection: 'row',
                    justifyContent: 'flex-end',
                    alignItems: 'flex-end'
                }}>
                    <View style={{marginRight: 20, marginTop: 10}}>
                        <Button title="akcje"/>
                    </View>
                </View>
                {offers.map((item, i) => (
                    <View key={i}>
                        <List>
                            <GestureRecognizer
                                onSwipe={(direction, state) => this.onSwipe(direction, state)}
                                onSwipeLeft={(state) => this.onSwipeLeft(state)}
                                onSwipeRight={(state) => this.onSwipeRight(state)}
                                config={config}
                                style={{
                                    flex: 1,
                                    backgroundColor: this.state.backgroundColor
                                }}
                            >
                                <ListItem
                                    roundAvatar
                                    subtitle={
                                        <View style={{flexDirection: 'row'}}>
                                            <View>
                                                <Text>{this.state.myText}</Text>
                                                <Text>{this.state.guardian}</Text>
                                                <Text>{this.state.area}</Text>
                                                <Text>{this.state.floor}</Text>
                                                <Text>{this.state.market}</Text>
                                                <Text>{this.state.year}</Text>
                                                <Text>{this.state.pricePerMeter}</Text>

                                                <Image source={require('../../gfx/lel.jpg')}
                                                       style={{
                                                           height: 100,
                                                           width: 150
                                                       }}/>
                                            </View>
                                            <View style={{
                                                marginLeft: 5,
                                                flexDirection: 'row',
                                                flexWrap: 'wrap'
                                            }}>
                                                <View style={{width: 140}}>
                                                    <Text>
                                                        {item.offerLocation}
                                                        {"\n"}
                                                        {item.offerStreet}
                                                        {"\n"}
                                                        {item.offerType} na {item.offerTransactionType}
                                                        {"\n"}
                                                        {item.offerNumber}
                                                    </Text>
                                                </View>
                                                <View>
                                                    <View style={{
                                                        justifyContent: 'flex-end',
                                                        width: 95,
                                                        height: 30,
                                                        backgroundColor: item.offerStatusColor
                                                    }}>
                                                        <Text style={{color: '#fff', textAlign: 'center'}}>
                                                            {item.offerStatus}
                                                        </Text>
                                                    </View>
                                                    <View style={{
                                                        flexDirection: 'column',
                                                        alignItems: 'flex-end',
                                                        flexWrap: 'wrap'
                                                    }}>
                                                        <Text style={{fontSize: 20}}>
                                                            {"\n"}
                                                            {"\n"}
                                                            {item.offerPrice}
                                                        </Text>
                                                    </View>
                                                </View>
                                            </View>
                                        </View>
                                    }
                                    onPress={() => this.props.navigation.navigate('OffersDetails')}
                                />
                            </GestureRecognizer>
                        </List>
                    </View>
                ))}
            </ScrollView>
        )
    }

}

export default Offers;

最佳答案

我建议试用这个库 react-native-swipe-list-view。它有据可查,易于用于任何类型的可滑动行。 https://github.com/jemise111/react-native-swipe-list-view

基本概念是您在隐藏元素前面有一个元素。当您滑动时,它只会显示下面隐藏的元素。

关于javascript - React Native - 滑动以隐藏和显示新内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47074379/

相关文章:

安卓服务应用

ios - Segue 没有被转入(swift 3)

ios - 快速与容器 View 交互

iphone - 确定 iPad 上 UITableView 的大小

javascript - TypeError : $(. ..).daterangepicker 不是一个函数

javascript - 从 html 中剪切信息(在 javascript 上)

javascript - Apps 脚本 - 无法在 html 模板的 js 函数中使用 google.script.run?

javascript - 单元测试异步 Express 中间件

Android getTextBounds 尾随空格处理(也与 AndEngine Font.getStringWidth 相关)

android - 如何在开始 Espresso 测试之前准备数据库数据?