React Native FlatList有时仅响应触摸

时间:2017-11-25 16:17:24

标签: reactjs react-native scroll touch react-native-flatlist

我以前在我的应用中的多个位置使用过FlatList没有任何问题,但现在当我创建一个新的时,它似乎没有正确注册触摸/滑动。只有1/6触摸似乎注册。

点击此处视频:https://photos.app.goo.gl/NZCtVYX6GLVCQN392

这就是我使用FlatList

的方式
render() {
        return (
            <Container>
                ...
                <FlatList
                    data={this.state.exercises}
                    renderItem={({item}) =>
                        <SetsRepsAndWeightItem exercise={item}/>
                    }
                    keyExtractor={item => item.name}
                    style={style.list}
                />
            </Container>
        );
}

SetsRepsAndWeightItem

render() {
        return (
            <View style={style.container}>
                <View style={style.header}>
                    <Text style={style.headerText}>{this.props.exercise.name}</Text>
                </View>
                <View style={style.about}>
                    <TouchableWithoutFeedback onPress={this.handleSetsPressed}>
                        <StatisticNumber metric="Sets" value={7}/>
                    </TouchableWithoutFeedback>
                    <TouchableWithoutFeedback onPress={this.handleRepsPressed}>
                        <StatisticNumber metric="Reps" value={5}/>
                    </TouchableWithoutFeedback>
                    <TouchableWithoutFeedback onPress={this.handleWeightPressed}>
                        <StatisticNumber metric="kg" value={35}/>
                    </TouchableWithoutFeedback>
                </View>
            </View>
        );
}

handleSetsPressed = () => {
    console.log("sets pressed");
}

handleRepsPressed = () => {
    console.log("reps pressed");
}

handleWeightPressed = () => {
    console.log("weight pressed");
}

此外:TouchableWithoutFeedback元素在触摸时未调用onPress个函数。

Container就像这样简单:

export default class Container extends Component {
  static propTypes = {
    children: Proptypes.any,
    backgroundColor: Proptypes.string
  };

  render() {
    const containerStyles = StyleSheet.flatten([
      style.container,
      this.props.backgroundColor ? { backgroundColor: this.props.backgroundColor } : null,
    ]);

    return (
        <TouchableWithoutFeedback onPress={() => Keyboard.dismiss()}>
            <View style={containerStyles}>
              {this.props.children}
            </View>
        </TouchableWithoutFeedback>
    );
  }
}

1 个答案:

答案 0 :(得分:1)

以下两个修复程序解决了我的问题:

  1. onPress={() => Keyboard.dismiss()}组件

  2. 中删除Container
  3. TouchableWithoutFeedback移至StatisticNumber组件,并将onPress作为道具从SetsRepsAndWeightItem

  4. 传递