简体   繁体   English

用可点击项反应本机FlatList

[英]React Native FlatList with clickable items

I'm building a custom line style for a react-native FlatList . 我正在为本机FlatList构建自定义线条样式。

I want the user to navigate to item details when clicking on the line text, or navigate to another page (drill down next level) when clicked on the right caret, something like: 我希望用户在单击行文本时导航到项目详细信息,或者在单击右插入号时导航到另一个页面(向下钻取下一级),例如:

在此处输入图片说明

Here is my current list components code: 这是我当前的列表组件代码:

class MyList extends Component {
  handleShowDetails = index => {
    ...
  };


  handleDrillDown = index => {
    ...
  }

  render = () => {
    let data = // Whatever data here

    return (
      <View style={styles.container}>
        <FlatList
          data={data}
          keyExtractor={(item, index) => index.toString()}
          renderItem={({ item }) => (
            <MyListItem
              onTextPress={this.handleShowDetails}
              onCaretPress={this.handleDrillDown}
              item={item}
            />
          )}
        />
      </View>
    );
  };
}

export default MyList;

const styles = StyleSheet.create({
  container: {
    flex: 1
  },
  item: {
    padding: 10,
    fontSize: 18,
    height: 44,
    backgroundColor: colors.white,
    borderStyle: "solid",
    marginBottom: 1
  }
});

And my list item component: 而我的清单项目组件:

class MyListItem extends Component {
  handleTextPress = () => {
    if (this.props.onTextPress) this.props.onTextPress(this.props.item.id);
  };

  handleIconPress =() => {
    if (this.props.onCaretPress) this.props.onCaretPress(this.props.item.id)
  }

  render = () => {
    return (
      <View style={styles.container}>
        <View style={styles.textContainer}>
          <Text onPress={this.handleTextPress}>{this.props.item.title}</Text>
        </View>
        <View style={styles.iconContainer}>
          <Button onPress={this.handleIconPress}>
            <Icon name="ios-arrow-forward"/>
          </Button>
        </View>
      </View>
    );
  };
}

export default MyListItem;

const styles = StyleSheet.create({
  container: {
    flex: 1,
    flexDirection: "row",
    justifyContent: "space-between",
    alignItems: "center",
    backgroundColor: colors.white,
    marginBottom: 1,
    height: 30
  },
  textContainer: {
    backgroundColor: colors.light,
    paddingLeft: 5,
    fontSize: 26
  },
  iconContainer: {
    alignSelf: "flex-end",
    backgroundColor: colors.primary,
    paddingRight: 5, 
    fontSize: 26
  }
});

Problems I'm facing: 我面临的问题:

a. 一种。 Clicking on text is not working properly. 单击文本无法正常工作。 Sometimes it navigates but most of time I cannot navigate, specially if I click on the empty space between the text and the caret. 有时它可以导航,但是大多数时候我无法导航,尤其是当我单击文本和插入符号之间的空白时。

b. b。 I simply cannot style the fontSize of the text. 我根本无法设置文本的fontSize样式。

c. C。 I'm not being able to space then accordingly. 我不能相应地间隔。

d. d。 I need to vertical center both itens on row. 我需要垂直居中对齐两个行。

Here is what I'm getting for now: 这是我现在得到的:

在此处输入图片说明

a. 一种。 Try to make the text full the cell. 尝试使文本充满单元格。 I can see your text just in 50% of the cell. 我可以在单元格的50%处看到您的文字。 style <Text> with height: 44 height: 44样式<Text>

b. b。 fontsize has to be placed in the <Text> component. fontsize必须放置在<Text>组件中。 You are placing it in <View> component 您将其放置在<View>组件中

c. C。 "I'm not being able to space then accordingly". “那么我就不能相应地间隔”。 I can't get your point clearly. 我不清楚你的意思。

d. d。 Try justifyContent: 'center' in iconContainer 尝试justifyContent: 'center'iconContainer

For part a I would suggest an improvement over Anthu's answer: 对于a部分,我建议对Anthu的答案进行改进:

I you want the entire textContainer to be clickable I suggest using TouchableWithoutFeedback (or another Touchable which suits your needs) and wrap this around the textContainer View 我希望整个textContainer都是可单击的,我建议使用TouchableWithoutFeedback(或另一个适合您需要的Touchable),并将其包装在textContainer视图周围

For the clicking issue, you could set an TouchableHighlight or a TouchableOpacity for the View . 对于点击问题,您可以为View设置TouchableHighlightTouchableOpacity

For spacing and alignment issues, maybe you could set the text to flex: 9 and the icon to flex: 1 with FlexBox. 对于间距和对齐问题,也许可以使用FlexBox将文本设置为flex: 9 ,将图标设置为flex: 1 Here are the docs for that https://facebook.github.io/react-native/docs/flexbox.html . 这是该https://facebook.github.io/react-native/docs/flexbox.html的文档。

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

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