javascript - 有没有办法在 Modal 中制作大小合适且功能强大的 FlatList?
问题描述
晚上好!
我一直在尝试创建一个内部带有可滚动 FlatList 的模式,但是这样的 FlatList 应该减小它的高度大小。该模式将搜索广泛的机场列表,因此基本上可能会显示很多数据,因此为什么 FlatList 是这种场合的最佳工具。基本上,模态屏幕如下图所示:
但是,无论如何,我都无法使 FlatList 中的滚动工作。这是代码,FlatList 有点功能,但它有一个警告:
const styles = StyleSheet.create({
modalContainer: {
backgroundColor: 'rgba(50,50,50,0.6)',
flex: 1,
justifyContent: 'center',
alignItems: 'center',
},
modalActivityContainer: {
backgroundColor: colors.colorWhite,
borderColor: colors.colorLightBlack,
borderWidth: 2,
borderRadius: 20,
padding: props.padding ? Number(props.padding) : 10,
maxWidth: '85%',
},
modalHeader: {
flexDirection: 'row',
justifyContent: 'center',
},
headerAirportModal: {
backgroundColor: colors.colorGrey,
borderTopRightRadius: 20,
borderTopLeftRadius: 20,
paddingHorizontal: 5,
paddingVertical: 10,
width: '100%',
justifyContent: 'center',
},
searchAirportBarContainer: {
backgroundColor: colors.colorWhite,
height: 40,
},
...
<Modal
onRequestClose={() => props.onRequestClose()}
animationType="fade"
transparent
visible={props.showModal}>
<TouchableWithoutFeedback
onPress={() => {
props.onRequestClose();
}}>
<View style={styles.modalContainer}>
<TouchableWithoutFeedback>
<View style={styles.modalActivityContainer}>
<View style={styles.modalHeader}>
<View style={styles.headerAirportModal}>
<Item style={styles.searchAirportBarContainer} rounded>
<Icon
style={styles.iconSearchAirport}
type="FontAwesome5"
name="search"
/>
<Input
style={styles.searchAirportBarText}
placeholderTextColor={colors.colorLightBlack}
placeholder="Search by Airport, Country or City"
/>
</Item>
</View>
</View>
<View>
<View style={styles.modalHeader}>
<Grid style={styles.headerAirportModalBackground}>
<Text style={styles.headerAirportModalText}>Latest Searches</Text>
<TouchableOpacity style={styles.clearButtonLatestContainer}>
<View>
<Text style={styles.clearButtonAirport}>Clear</Text>
</View>
</TouchableOpacity>
</Grid>
</View>
</View>
<View style={{maxHeight: 250}}>
<ScrollView>
<View onStartShouldSetResponder={() => true}>
<FlatList
data={airportElements}
renderItem={renderAirportItem}
keyExtractor={(item) => item.id}
/>
</View>
</ScrollView>
</View>
<Button
backgroundColor={colors.colorRed}
alignSelf="stretch"
buttonStyle={styles.finishButton}
onPress={() => {
setModalSelectAirport(false);
}}>
Cancel
</Button>
</View>
</TouchableWithoutFeedback>
</View>
</TouchableWithoutFeedback>
</Modal>
如果我在 ScrollView 中使用 FlatList,如上所示,代码将按预期工作。但是,我收到了这个警告:
VirtualizedLists 永远不应该嵌套在具有相同方向的普通 ScrollViews 中 - 改用另一个 VirtualizedList 支持的容器。
因此,我无法正确使用 FlatList。另外,我已经设法使它与 . 一起使用.map
,但建议不要使用它,因为我要加载一个包含很多元素的列表,这可能会导致延迟。
我设法以另一种方式实现 FlatList:
<View
style={styles.airportLatestSearchListContainer}>
<FlatList
contentContainerStyle={styles.airportLatestSearchlist}
data={airportElements}
renderItem={renderAirportItem}
keyExtractor={(item) => item.id}
/>
</View>
但是,这会阻止用户滚动。该列表保持静态,用户根本无法滚动。
我曾尝试{flex: 1, flexGrow: 1}
在父视图和 FlatList 本身中使用,即使在 FlatList 容器或样式中,两者都不起作用。
有人可以帮我解决这个问题吗?我真的很感激时间和精力!谢谢!
解决方案
推荐阅读
- node.js - 我无法部署角度项目
- angular - 在 Angular 10 中使用选择器模拟 NgRx 存储
- r - 如何在不退出 R 的情况下从终端执行 R 单行程序?
- java - Cobertura Maven插件报告中类声明级别的红色复选标记是什么意思?
- java - 双击 jar 会出现 jni 错误,但通过控制台运行它会正常运行
- python - 使用正则表达式包含字典中的一组键并将它们匹配到字符串列表中
- javascript - 范围滑块不连贯,点击时不更新
- sql - REDSHIFT ODBC 连接上的 SSRS 数据集参数
- angular - angular-cli build:发生未处理的异常:没有可用于依赖类型的模块工厂:CssDependency
- php - Phalcon 4 如何获取当前页面的 URL?