gpt4 book ai didi

reactjs - React Native FlatList 有时只响应触摸

转载 作者:行者123 更新时间:2023-12-04 17:47:58 27 4
gpt4 key购买 nike

我用过 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>
);
}
}

最佳答案

以下两个修复为我解决了这些问题:

  • 删除 onPress={() => Keyboard.dismiss()}来自 Container组件
  • 移动 TouchableWithoutFeedbackStatisticNumber组件,并通过 onPress作为来自 SetsRepsAndWeightItem 的 Prop
  • 关于reactjs - React Native FlatList 有时只响应触摸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47488063/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com