【问题标题】:React Native - How to add react-native-navigation into this layoutReact Native - 如何将 react-native-navigation 添加到此布局中
【发布时间】:2019-07-15 00:25:04
【问题描述】:

我在网上找到了一些代码,而且我对 react-native 还很陌生。我想知道是否可以添加代码以使这些渲染的正方形像按钮一样可点击? (请参阅下面的代码或链接,因为它已经为它创建了一个小吃版本)我试图摆弄按钮或在其中添加 onPress ,遗憾的是我的尝试都没有成功。我希望能够使用像 onPress={() => this.props.navigation.navigate('Details')} 这样的东西,但是当它添加到标签中以及当我尝试在附近添加标签时,点击什么也没做它在屏幕上完全覆盖它的标签。

// https://snack.expo.io/@spencercarli/react-native-flatlist-grid
import React from 'react';
import { Button, StyleSheet, Text, View, FlatList, Dimensions } from 'react-native';
import { createStackNavigator, createAppContainer } from 'react-navigation';
//import { Container, Header, Content} from "native-base";
//import { SectionList, FlatList, GridView, FlatGrid } from 'react-native-super-grid';<

const data = [
  { key: 'A' }, { key: 'B' }, { key: 'C' }, { key: 'D' }, { key: 'E' }, { key: 'F' }, { key: 'G' }, { key: 'H' }, { key: 'I' }, { key: 'J' },
  // { key: 'K' },
  // { key: 'L' },
];

const formatData = (data, numColumns) => {
  const numberOfFullRows = Math.floor(data.length / numColumns);

  let numberOfElementsLastRow = data.length - (numberOfFullRows * numColumns);
  while (numberOfElementsLastRow !== numColumns && numberOfElementsLastRow !== 0) {
    data.push({ key: `blank-${numberOfElementsLastRow}`, empty: true });
    numberOfElementsLastRow++;
  }

  return data;
};

const numColumns = 3;
export default class App extends React.Component {
  renderItem = ({ item, index }) => {
    if (item.empty === true) {
      return <View style={[styles.item, styles.itemInvisible]} />;
    }
    return (
      <View
        style={styles.item}
      >
        <Text style={styles.itemText}>{item.key}</Text>
      </View>
    );
  };

  render() {
    return (
      <FlatList
        data={formatData(data, numColumns)}
        style={styles.container}
        renderItem={this.renderItem}
        numColumns={numColumns}
      />
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    marginVertical: 20,
  },
  item: {
    backgroundColor: '#4D243D',
    alignItems: 'center',
    justifyContent: 'center',
    flex: 1,
    margin: 1,
    height: Dimensions.get('window').width / numColumns, // approximate a square
  },
  itemInvisible: {
    backgroundColor: 'transparent',
  },
  itemText: {
    color: '#fff',
  },
});

【问题讨论】:

    标签: react-native react-navigation react-native-flatlist


    【解决方案1】:

    是的,有可能,您只需将 View 更改为 TouchableOpacity 并添加您的 onPress 功能,如下所示:

    here is the snack modified

    【讨论】:

    • 哇,我想它就是这么简单......我只需要弄清楚在哪里放置东西。感谢您的帮助
    猜你喜欢
    • 2019-04-07
    • 2017-10-15
    • 2017-01-16
    • 2022-10-25
    • 2017-10-11
    • 2019-06-25
    • 2019-11-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多