简体   繁体   English

如何使列表项可点击并导航到另一个页面?

[英]How to make list items clickable and navigate to another page?

How can I make items clickable on my list and then navigate to another page?如何使我的列表中的项目可点击,然后导航到另一个页面? I want to have some arrow icon included and visible signs to the eyes when I clicked on some items list.当我单击某些项目列表时,我希望包含一些箭头图标和可见的标志。 I would love to help with this because I didn't really understand how to do it in my case.我很乐意帮助解决这个问题,因为我真的不明白在我的情况下该怎么做。 In my code, I show a list that comes from axios.在我的代码中,我显示了一个来自 axios 的列表。

This is link to my example list这是我的示例列表的链接

import React, { Component } from 'react';
import { View, Text, StyleSheet, ActivityIndicator, Platform, FlatList, Dimensions, Image } from 'react-native';
import { HeaderButtons, Item } from 'react-navigation-header-buttons'
import HeaderButton from '../components/HeaderButton';
import axios from 'axios';
import { DrawerNavigator } from 'react-navigation';



const { width, height } = Dimensions.get('window');

export default class MainScreen extends Component {
    constructor(props) {
        super(props);
        this.state = { isLoading: true, data: [] };
    }

    componentDidMount() {
        axios.get('https://rallycoding.herokuapp.com/api/music_albums')
            .then(res => {
                this.setState({
                    isLoading: false,
                    data: res.data,
                })
                console.log(res.data);
            })
    }

    renderItem(item) {
        const { title, artist, url } = item.item;
        return (
            <View style={styles.itemView}>


                <View style={styles.itemInfo}>
                    <Text style={styles.name}>
                        {title + ' ' + artist }
                    </Text>
                    <Text style={styles.vertical} numberOfLines={2}></Text>
                </View>
            </View>
        );
    }

    render() {
        if (this.state.isLoading) {
            return (
                <View style={{ flex: 1, padding: 20 }}>
                    <Text style={{ alignSelf: 'center', fontWeight: 'bold', fontSize: 20 }}>טוען נתונים...</Text>
                    <ActivityIndicator />
                </View>
            )
        }

        return (
            <View style={styles.container}>
                <FlatList
                    data={this.state.data}
                    renderItem={this.renderItem.bind(this)}
                    keyExtractor={item => item.id}
                />
            </View>
        );
    }
}

const styles = StyleSheet.create({
    container: {
        flex: 1,
        justifyContent: 'center',
        alignItems: 'center'

    },
    itemView: {
        flex: 1,
        width,
        borderBottomWidth: 0.5,
        borderColor: 'black',
        borderStyle: 'solid',
        paddingHorizontal: 12,
        flexDirection: 'row',
    },
    imgContainer: {
        flex: 0,
        borderColor: 'black',
        borderWidth: 1.5,
        height: 60,
        width: 60,
        alignItems: 'center',
        justifyContent: 'center',
    },
    itemInfo: {
        flex: 1,
        marginHorizontal: 10,
    },
    name: {
        //fontFamily: 'Verdana',
        fontSize: 18,
        color: '#ff0000',
        textAlign: 'left',
    },
    imageStyle: {
        height: 50,
        width: 50,
    },
    vertical: {
        fontSize: 18,
    }

});

You can use TouchableOpacity to make your list clickable, but to navigate to another page you need have your React Navigation working correctly.您可以使用TouchableOpacity使您的列表可点击,但要导航到另一个页面,您需要让 React Navigation 正常工作。

First import TouchableOpacity from the react-native library.首先从react-native库中导入TouchableOpacity Then you can edit your renderItem element to use it:然后你可以编辑你的renderItem元素来使用它:

renderItem(item) {
    const { title, artist, url } = item.item;
    return (
        <TouchableOpacity onPress={()=>this.props.navigation.navigate("OTHER_PAGE_NAME")} style={styles.itemView}>


            <View style={styles.itemInfo}>
                <Text style={styles.name}>
                    {title + ' ' + artist }
                </Text>
                <Text style={styles.vertical} numberOfLines={2}></Text>
            </View>
        </TouchableOpacity>
    );
}

TouchableOpacity reference: https://facebook.github.io/react-native/docs/touchableopacity TouchableOpacity 参考: https : //facebook.github.io/react-native/docs/touchableopacity

React Navigation reference: https://reactnavigation.org/docs/en/navigating.html反应导航参考: https : //reactnavigation.org/docs/en/navigating.html

I had the same issue too and gave my items module-id's.我也有同样的问题,并给了我的项目模块 ID。 I had 3 different main screens to avigate to so I solved it with a switch;我有 3 个不同的主屏幕要导航,所以我用一个开关解决了它; first:第一的:

const onRandomthingPressed = () => {
   itemSelected(navigate, {
      moduleid: 1,
   })
}

then use id's:然后使用id:

const something = (navigate, item) => {
   switch (item.moduleid) {
      case 1:
         navigation.navigate('screen1')
         break
      case 2:
         navigation.navigate('screen2')
         break
      case 3:
         navigation.navigate('screen3')
         break
      default:
         break
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM