reactjs - React Native iOS - FlatList:单击列表中的任何项目并显示警报
问题描述
我想知道如何进行操作:在 React Native - iOS 中触摸列表中的任何项目(我正在使用 FlatList)并显示点击位置的警报。按照下面,代码必须对我的 DetailProduct 类很重要:
详细产品
export default class DetailProduct extends React.Component {
state = {
stores: []
};
componentDidMount() {
this.setState({stores});
}
render() {
return (
<View style={styles.container}>
<FlatList
data={this.state.stores}
showsVerticalScrollIndicator={false}
initialNumToRender={2}
renderItem={({item, index}) =>
<View style={styleItem.container}>
<View style={styleItem.holderImage}>
<Image source={{uri : item.logo_company}} style={{width: '100%', height: '100%'}} resizeMode='contain'/>
</View>
<Text style={item.amount > 0 ? styleItem.textAvailable : styleItem.textUnavailable}>{item.is_available == true ? 'Disponivel' : 'Indisponivel'}</Text>
<TouchableOpacity style={item.amount > 0 ? styleItem.qtdHolder : styleItem.qtdHolderUnavailable} activeOpacity={0.8}>
<Text style={item.amount > 0 ? styleItem.textQtdNum : styleItem.textQtdNumUnavailable}>{item.amount}</Text>
<Image source={require('../imgs/assets/ic_arrow_detail_store.png')} style={item.amount > 0 ? styleItem.icShowDetail : {position: 'absolute', top: -1000}}/>
</TouchableOpacity>
</View>
}
keyExtractor={item => item.id_store}
/>
</View>
);
}
}
解决方案
TouchableWithoutFeedback
您需要用or元素包装您的元素(renderItem 函数中的那个)TouchableOpacity
并将该onPress
函数添加到 Touchable 元素。在onPress
函数中,您可以传递 item 或 index 参数,这样您就知道单击了哪个位置并显示警报。
推荐阅读
- r - 当我将图从 R 导出到 .eps 时,所有顶点名称都消失了
- sql - 选择重复的 SKU 并将相应的价格放入新的相邻列中
- python - 如何从现有 SQLite 数据库中获取特定行的列以在 Flask 应用程序中使用?
- css - 幻灯片计数器并在每张幻灯片上显示
- xml - arrays.xml:16:错误:解析 XML 时出错:标签不匹配
- rust - Calamine crate (Rust) 只读取 xlsx 文件的第一行,如何解决?
- python - 如何在python中为多个discord服务器设置频道ID
- node.js - 带有nodejs的wordpress身份验证cookie
- svg - 添加恒定水平线
- javascript - 我有一个总是正确的内部函数,但是当分配给外部函数中的变量时,它的输出是未定义的