css - 我有一个带有 positoin:absolute 的 Flatlist ,onpress 在渲染项内没有响应
问题描述
这里的问题是当我用样式位置“绝对”onpress 包装平面列表时没有响应。
return ( <View style={styles.userNameSearchContainer}>{searchUserName()}</View>
<View style={styles.sendInviteContainer}>{sendInvite()}</View>)
const searchUserName = () => {
let searchUser = (
<KeyboardAvoidingView>
<View>
<Text style={styles.searhUserTitle}>Search by username</Text>
</View>
<View style={styles.searchUserInputWrapper}>
<Image
style={styles.searchIcon}
source={require("../../../assets/Images/search.png")}
/>
<TextInput
style={{ flex: 1 }}
placeholder={"@user114"}
value={searchText}
onChangeText={(text) => onSearchTextChanged(text)}
/>
</View>
<View>
<FlatList
style={{ position:'absolute', width: "100%" }}
data={filteredUsers}
renderItem={renderItem}
keyExtractor={(item) => item.id}
/>
</View>
</KeyboardAvoidingView>
);
return searchUser;
};
const renderItem = ({ item }) => (
<TouchableOpacity onPress={(item) => console.log(item)}>
<View style={styles.item}>
<Text style={styles.title}>{item.title}</Text>
</View>
</TouchableOpacity> );
我尝试从手势处理程序和 touchablewithoutfeedback 导入 touchableOpacity。
解决方案
这是一个例子
零食:https ://snack.expo.io/@ashwith00/ranting-bubblegum
import * as React from 'react';
import {
Text,
View,
StyleSheet,
FlatList,
TextInput,
TouchableOpacity,
} from 'react-native';
import Constants from 'expo-constants';
// You can import from local files
import AssetExample from './components/AssetExample';
// or any pure javascript modules available in npm
import { Card } from 'react-native-paper';
const searchResult = ['data1', 'data2', 'data3', 'data4', 'data5'];
export default function App() {
const [str, setStr] = React.useState('');
return (
<View style={styles.container}>
<TextInput style={styles.input} onChangeText={(st) => setStr(st)} />
<View style={[styles.list, ]}>
{str.length > 0 && <FlatList
data={searchResult}
renderItem={({ item }) => (
<TouchableOpacity
style={styles.item}
onPress={() => console.log(item)}>
<Text style={styles}>{item}</Text>
</TouchableOpacity>
)}
/>}
</View>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
paddingTop: Constants.statusBarHeight || 10,
backgroundColor: '#ecf0f1',
padding: 8,
},
paragraph: {
margin: 24,
fontSize: 18,
fontWeight: 'bold',
textAlign: 'center',
},
input: {
alignSelf: 'stretch',
height: 50,
backgroundColor: 'white',
},
item: {
alignSelf: 'stretch',
justifyContent: 'center',
alignItems: 'center',
height: 50,
backgroundColor: 'white',
},
list: {
position: 'absolute',
top: 60,
left: 0,
right: 0,
paddingHorizontal: 10,
height: 200,
},
});
推荐阅读
- jquery - 让 kendo Treeview 在拖放时复制节点
- java - 如何正确设置 Spring-boot2 的 activiti 框架
- javascript - 如何读取通过cordova文件插件编写的对象?
- elasticsearch - 为什么 ElasticSearch 只返回 5 个桶用于 dat_histogram 聚合?
- excel - 使用 VBA 获取文件的所有属性
- java - Android google api + 数据库
- c - 使用 fclose() 的错误
- sql - Group By 以获取唯一组合的出现次数及其事务的总和。
- html - HTML表格行上不同颜色的双单边边框
- java - 适用于桌面应用程序的带有 Java 开发工具包的 AWS Cognito 登录