Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
React native 在列表中滑动时不显示绝对右侧元素_React Native_Position_Expo_Swipe - Fatal编程技术网

React native 在列表中滑动时不显示绝对右侧元素

React native 在列表中滑动时不显示绝对右侧元素,react-native,position,expo,swipe,React Native,Position,Expo,Swipe,编辑:好的,看来问题来自我的自定义项目列表,所以我在下面添加了该项目的代码(我的零食工作正常) 自定义项: // components/MyItem.js import React from 'react' import { StyleSheet, Image, View, Text, Platform, Animated, TouchableOpacity } from 'react-native' import Moment from 'react-moment' import { Ion

编辑:好的,看来问题来自我的自定义项目列表,所以我在下面添加了该项目的代码(我的零食工作正常)

自定义项:

// components/MyItem.js

import React from 'react'
import { StyleSheet, Image, View, Text, Platform, Animated, TouchableOpacity } from 'react-native'
import Moment from 'react-moment'
import { Ionicons } from '@expo/vector-icons'
import Swipeable from 'react-native-gesture-handler/Swipeable'

class MyItem extends React.Component{

    state= {
        deleteItem: false,
    }

    render(){
        const { myItem, displayDetailsForMyItem, itemType } = this.props
        let showType = false
        let type = ""
        if (itemType === "Stuff") {
            showType = true
            type = myItem.type
        }

        let ptfPrefix
        (Platform.OS === 'android') ? ptfPrefix = 'md-' : ptfPrefix = 'ios-'
        const calIconName = ptfPrefix + 'calendar'
        const titleIconName = ptfPrefix + 'create'
        const pplIconName = ptfPrefix + 'contact'
        const trashIconName = ptfPrefix + 'trash'

        const RightActions = (progress, dragX) => {
            const scale = dragX.interpolate({
              inputRange: [-100, 0],
              outputRange: [0.7, 0]
            })

            return (
                <>
                    <TouchableOpacity onPress={() => this.props.deleteItem(myItem.key, this.props.itemType)}
                    style= {{}}>
                        <View
                            style={{ flex:1, backgroundColor: 'red', justifyContent: 'center', marginTop: 5, marginBottom: 5}}>
                            <Animated.Text
                            style={{
                                color: 'white',
                                paddingHorizontal: 25,
                                marginTop: 5,
                                fontWeight: '600',
                                transform: [{ scale }]
                            }}>
                            <Ionicons name={trashIconName} size={40} color='white' />
                            </Animated.Text>
                        </View>
                    </TouchableOpacity>
                    <TouchableOpacity onPress={() => this.props.deleteItem(myItem.key, 'toto')}>
                        <View
                            style={{ flex: 1, backgroundColor: 'orange', justifyContent: 'center', marginTop: 5, marginBottom: 5 }}>
                            <Animated.Text
                            style={{
                                color: 'white',
                                paddingHorizontal: 20,
                                fontWeight: '600',
                                transform: [{ scale }]
                            }}>
                            <Ionicons name='md-create' size={40} color='white' />
                            </Animated.Text>
                        </View>
                    </TouchableOpacity>
                </>
            )
        }

        return(
            /*<TouchableOpacity 
                style={styles.main_container}
                //onPress={() => displayDetailsForMyItem(myItem.key)}
            >*/
                <View style={styles.main_container}>
                    <View style={styles.first_line}>
                        <View style={styles.left_part_container}>
                            <Image style={styles.date_bg} source={require('../assets/icons/list_bg.png')} />
                            <View style={styles.date_container}>
                                <Ionicons name={calIconName} style={styles.top_left_elmnts} />
                                <Moment style={styles.top_left_elmnts} element={Text} format="DD/MM/YYYY" date={myItem.date} />
                            </View>
                        </View>
                        <View style={styles.right_part_container}>
                            <Text style={styles.top_right_elmnts}>{myItem.title}</Text>
                            <Ionicons name={titleIconName} style={styles.top_right_elmnts} />
                        </View>
                    </View>

                    <View style={styles.main_data}>
                        <Text style={styles.main_text}>
                            {(this.props.itemType === 'Money') ? myItem.amount + " €" : myItem.quantity + " Objets"}
                        </Text>
                    </View>

                    <View style={styles.last_row}>
                        <View style={styles.left_part_container}>
                            <Ionicons name={pplIconName} style={styles.btm_left_elmnts} />
                            <Text style={styles.btm_left_elmnts}>{myItem.people}</Text>
                        </View>
                        { showType &&
                        <View style={styles.right_part_container}>
                            <Image style={styles.date_bg} source={require('../assets/icons/list_bg_type.png')} />
                            <View style={styles.date_container}>
                                <Text style={styles.btm_right_elmnts}>{type}</Text>
                                <Ionicons name={calIconName} style={styles.btm_right_elmnts} />
                            </View>
                        </View>
                        }
                    </View>
                </View>
            //</TouchableOpacity>
        )
    }
}

const styles=StyleSheet.create({
    main_container: {
        height: 100,
        flex: 1,
        marginTop: 2,
        marginBottom: 2,
        backgroundColor: '#465881'
    },
    top_left_elmnts: {
        marginLeft: 10,
        fontSize: 15,
        color: 'white'
    },
    top_right_elmnts: {
        marginRight: 10,
        fontSize: 15,
        color: 'white'
    },
    btm_left_elmnts:{
        marginLeft: 10,
        fontSize: 15,
        color: 'white'
    },
    btm_right_elmnts: {
        marginRight: 10,
        fontSize: 15,
        color: 'white'
    },
    first_line: {
        flexDirection: 'row',
        width: '100%',
        flex: 2,
        marginTop: 4
    },
    date_bg: {
        position: 'absolute'
    },
    date_container: {
        position: 'absolute',
        flexDirection: 'row',
        alignItems: 'center'
    },
    left_part_container: {
        width: '50%',
        flexDirection: 'row',
        justifyContent: 'flex-start',
        alignItems: 'center'
    },
    right_part_container: {
        width: '50%',
        justifyContent: 'flex-end',
        flexDirection: 'row',
        alignItems: 'center'
    },
    main_data: {
        justifyContent: 'center',
        alignItems: 'center',
        flex: 6
    },
    main_text: {
        fontSize: 25,
        margin: 10,
        color: '#FB5B5A'
    },
    last_row: {
        flexDirection: 'row',
        flex: 2,
        width: '100%',
        marginBottom: 4
    }
})

export default MyItem

MyItem对象已正确呈现。

好的,我想我找到了出现此问题的原因(如果有人能够解释差异,我会接受)

hiddenRender函数不会显示右按钮,因为它们位于
TouchableOpacity

问题是我从
react native手势处理程序
导入了
touchablepacity

但是当我从
react native
导入
touchablepacity
时,它工作正常


试试我更新的零食。

你能用你的代码添加一个snack.expo吗?那真是太好了好吧,我没想到要这么做,我很快会加上去的。我不明白。零食上的代码似乎有效,我有两个代码完全相同的类,但其中一个在从右侧滑动时显示两个值,而另一个则不显示。我不知道怎么给你看。
// components/Test.js

import React from 'react'
import { StyleSheet, View, Image, Platform, ActivityIndicator, Text } from 'react-native'
import { TouchableOpacity } from 'react-native-gesture-handler'
import { Ionicons } from '@expo/vector-icons'
import StuffData from '../dbaccess/StuffData'
import MoneyData from '../dbaccess/MoneyData'
import Moment from 'react-moment'
import MyItem from './MyItem'
import SwipeValueBasedUi from '../functions/SwipeValueBasedUI'

import { SwipeListView } from 'react-native-swipe-list-view';

class Test extends React.Component {

    constructor(props) {
        super(props)

        this.state = { 
            dataList: [],
            isLoading: true
        }
    }

    _initData() {
        const type = this.props.route.params?.type ?? 'Money'
        let myObject

        if (type === 'Money'){
            myObject = new MoneyData()
        } else {
            myObject = new StuffData()
        }

        this.setState({
            mydataObject: myObject
        })

        myObject.getData().then(val => {
            this.setState({
                dataList: val,
                isLoading: false,
            })
        })

        myObject.total().then(val => {
            this.setState({
                total: val
            })
        })
    }

    _updateNavigationParams() {
        const navigation = this.props.navigation
        const type = 'Money'

        let addIconName
        addIconName = ((Platform.OS == 'android') ? 'md-add' : 'ios-add')


        if (Platform.OS === "ios" && type !== 'People'){
            navigation.setOptions({
                        headerRight: () => <TouchableOpacity style={styles.add_touchable_headerrightbutton}
                                        onPress={() => this._addItem()}>
                                        <Ionicons name={addIconName} style={styles.add_image} />
                        </TouchableOpacity>
                })
            }
    }

    componentDidMount(){
        this._updateNavigationParams()
        this._initData()
    }

    _addItem = () => {
        const type = 'Money'

        if (type === 'Money'){
            this.props.navigation.navigate('AddMoney')
        } else {
            this.props.navigation.navigate('AddStuff')
        }
    }

    //Android dedicated
    _displayFloatingActionButton() {
        const type = 'Money'

        if (Platform.OS === 'android' && type !== 'People'){
            return(
            <TouchableOpacity style={styles.add_touchable_floatingactionbutton}
                onPress={() => this._addItem()}>
                <Image style={styles.add_image} source={require('../assets/icons/ic_add.png')}/>
            </TouchableOpacity>
            )
        }
    }

    _checkData(){
        this._displayDataList()
    }

    _displayDetailsForMyItem = (idItem, type) => {
        this.props.navigation.navigate('ItemDetails', { idMyItem: idItem, type: type })
    }

    _deleteItem = (idItem, type) => {
        this.state.mydataObject.delete(idItem)
        this.setState({
            dataList: this.state.moneyList.filter(item => item.key != idItem)
        })

        alert(type + ' deleted')        
    }

    _closeRow = (rowMap, rowKey) => {
        if (rowMap[rowKey]) {
            rowMap[rowKey]._closeRow();
        }
    }

    _deleteRow = (rowMap, rowKey) => {
        closeRow(rowMap, rowKey);
        this._deleteItem()
    }

    _onRowDidOpen = rowKey => {
        console.log('This row opened', rowKey);
    }

    _renderHiddenItem = (data, rowMap) => {
        return (
            <View style={styles.rowBack}>
                <Text style={styles.toto}>Left</Text>
                <TouchableOpacity style={[styles.backRightBtn, styles.backRightBtnLeft]}
                    //onPress={() => this._closeRow(rowMap, data.item.index)} 
                    >
                    <Text style={styles.backTextWhite}>Close</Text>
                </TouchableOpacity>
                <TouchableOpacity
                    style={[styles.backRightBtn, styles.backRightBtnRight]}
                    //onPress={() => this._deleteRow(rowMap, data.item.index)} 
                    >
                    <Text style={styles.backTextWhite}>Delete</Text>
                </TouchableOpacity>
            </View>
        )
    }

    render(){
        const type = 'Money'

        if(this.state.isLoading){
            return(
              <View style={styles.activity}>
                <ActivityIndicator size="large" color="#FB5B5A"/>
              </View>
            )
        } else {
            return(
                <View style={styles.main_container}>
                    <View style={styles.title_container}>
                        <Image source={require('../assets/icons/cadre.png')} style={styles.cadre} />
                        <Text style={styles.header_text}>
                            {(type === 'Money') ? this.state.total + ' €' : this.state.total}
                        </Text>
                    </View>
                    <View style={styles.main_container}>
                        <SwipeListView
                            data={this.state.dataList}
                            renderItem={({item}) => <MyItem 
                                myItem={item}
                                itemType={type}
                                displayDetailsForMyItem={this._displayDetailsForMyItem}
                                deleteItem={this._deleteItem}/>}
                            renderHiddenItem={this._renderHiddenItem}
                            leftOpenValue={75}
                            rightOpenValue={-150}
                            previewRowKey={'0'}
                            previewOpenValue={-40}
                            previewOpenDelay={3000}
                            onRowDidOpen={this._onRowDidOpen}
                        />
                    </View>
                </View>
            )
        }
    }
}

const styles=StyleSheet.create({
    toto: {
        color: 'white'
    },
    main_container: {
        flex: 1,
        backgroundColor: '#003F5C'
    },
    title_container: {
        justifyContent: 'center',
        alignItems: 'center',
        marginTop: 40,
        marginBottom: 40
    },
    header_text: {
        textAlign: 'center',
        fontSize: 35,
        color: 'white',
    },
    cadre: {
        position: 'absolute',
        marginRight: 10,
    },
    add_touchable_headerrightbutton: {
        marginRight: 8
    },
    add_image: {
        marginRight: 10,
        fontSize: 30,
        color: 'white'
    },
    add_touchable_floatingactionbutton: {
        position: 'absolute',
        width: 60,
        height: 60,
        right: 30,
        bottom: 30,
        borderRadius: 30,
        backgroundColor: '#e91e63',
        justifyContent: 'center',
        alignItems: 'center'
    },
    activity: {
        position: 'absolute',
        left: 0,
        right: 0,
        top: 0,
        bottom: 0,
        alignItems: 'center',
        justifyContent: 'center',
        backgroundColor: '#003F5C'
    },
    container: {
        backgroundColor: 'white',
        flex: 1,
    },
    backTextWhite: {
        color: '#FFF',
    },
    rowFront: {
        alignItems: 'center',
        backgroundColor: '#CCC',
        borderBottomColor: 'black',
        borderBottomWidth: 1,
        justifyContent: 'center',
        height: 50,
    },
    rowBack: {
        alignItems: 'center',
        backgroundColor: '#DDD',
        flex: 1,
        flexDirection: 'row',
        justifyContent: 'space-between',
        paddingLeft: 15,
    },
    backRightBtn: {
        alignItems: 'center',
        bottom: 0,
        justifyContent: 'center',
        position: 'absolute',
        top: 0,
        width: 75,
    },
    backRightBtnLeft: {
        alignSelf: 'flex-end',
        backgroundColor: 'blue',
        right: 75,
    },
    backRightBtnRight: {
        backgroundColor: 'red',
        right: 0,
    },
    trash: {
        height: 25,
        width: 25,
    },
})

export default Test