我正在开发一个用于学习目的的简单 React Native 应用程序。我只是迈出了进入 React Native 世界的第一步。但是在这个非常早期的阶段,我遇到了问题。我无法让简单的触摸事件工作。我正在使用 TouchableWithoutFeedback 实现触摸事件。这是我的代码。
class AlbumList extends React.Component {
constructor(props)
{
super(props)
this.state = {
displayList : true
}
}
componentWillMount() {
this.props.fetchAlbums();
}
albumPressed(album)
{
console.log("Touch event triggered")
}
renderAlbumItem = ({item: album}) => {
return (
<TouchableWithoutFeedback onPress={this.albumPressed.bind(this)}>
<Card>
<CardSection>
<Text>{album.artist}</Text>
</CardSection>
<CardSection>
<Text>{album.title}</Text>
</CardSection>
</Card>
</TouchableWithoutFeedback>
)
}
render() {
let list;
if (this.state.displayList) {
list = <FlatList
data={this.props.albums}
renderItem={this.renderAlbumItem}
keyExtractor={(album) => album.title}
/>
}
return (
list
)
}
}
const …Run Code Online (Sandbox Code Playgroud) 我有一段简单的代码,它只是一个带有 onLongPress 道具的 TouchableOpacity,但它似乎不起作用。
<TouchableOpacity delayLongPress={10} onLongPress={()=>{console.log("pressed")}} activeOpacity={0.6}>
<Text>BUTTON</Text>
</TouchableOpacity>
Run Code Online (Sandbox Code Playgroud)
我试过删除延迟道具,但这仍然不起作用。然而,将 onLongPress 更改为 onPress 似乎确实有效,但我想要长按功能。我正在 Android 模拟器上对此进行测试。
react-native react-native-android touchableopacity touchablehighlight touchablewithoutfeedback
我创建了一个组合组件来将 TouchableNativeFeedback 组合到 wrapperComponent。
export default function withFeedback2(
WrappedComponent
) {
return class extends BaseComponent {
constructor(props) {
super(props);
}
render() {
return (
<View>
<TouchableNativeFeedback
onPress={() => this.props.onContainerViewPress()}
>
<WrappedComponent {...this.props} />
</TouchableNativeFeedback>
{/* <TouchableOpacity
onPress={this.props.onContainerViewPress ? () => this.props.onContainerViewPress() : null}
>
<WrappedComponent {...this.props} />
</TouchableOpacity> */}
</View>
);
}
};
}
Run Code Online (Sandbox Code Playgroud)
但是TochableNativeFeedback 的 OnPress事件没有触发。而 OnPress 事件被正确触发,并且如果 wrappercomponent 包装在 TouchableOpacity 下,则调用 wrappercomponent 的onContainerViewPress道具。
我正在 Android 平台上对此进行测试。
react-native react-native-android touchableopacity react-navigation touchablewithoutfeedback
react-native ×3