css - 在 FlatList 中未正确显示页脚和搜索栏
问题描述
我有这组组件:
render() {
return (
<InstantSearch
appId="29FLVJV5X9"
apiKey="f2534ea79a28d8469f4e81d546297d39"
indexName="prod_conferences"
style={{ flexGrow: 1 }}>
<Configure filters={`startDateUnix>${TODAY}`} hitsPerPage={10} />
<SearchBox />
<LoadingIndicator />
<Conferences/>
</InstantSearch>)
}
哪里<InstantSearch>
有简单的<View>
我的会议:
export default connectInfiniteHits(({ hits, hasMore, refine }) => {
const LoadMoreFooter = connectStateResults(
({ searching }) =>
!searching ? <View style={s.loadMoreButtonContainer}>
<Divider style={s.loadMoreButtonDivider} />
<Button transparent color={'#53acfe'} title={'Load more confs...'} onPress={() => refine()} />
</View> : null
);
return (
<FlatList
data={hits}
keyExtractor={(item, index) => item.objectID}
ListFooterComponent={<LoadMoreFooter />}
renderItem={({ item }) =>
<ConferenceItem {...item} />
}
/>
);
});
并且<SearchBox>
是简单<SearchBar>
的 RN 元素。问题是,在我添加了 SearchBox 之后,我的页脚从未显示,滚动在显示页脚之前“停止”。
我该如何解决?
谢谢!
解决方案
方法一
添加flex: 1
到父视图:
<InstantSearch style={{ flex: 1 }}>
在您的InstantSearch
中,确保将样式传递给最外层的视图:
const InstantSearch = props => <View style={props.style}>...
方法二
将你SearchBox
的ListHeaderComponent
道具移动到FlatList
:
<FlatList ListHeaderComponent={() => <SearchBox />} ...
方法三
给你SearchBox
以下风格:
{ position: 'absolute', zIndex: 1 }
请参阅方法 1,了解如何将其传递给被包裹的View
. 如果SearchBox
来自第三方模块,则只需将其包装为View
:
<View style={{ position: 'absolute', zIndex: 1 }}>
<SearchBox />
</View>
方法四
使用SectionList
而不是FlatList
避免SearchBox
向下滚动(与方法 2 一样)。
<SectionList
renderSectionHeader={SearchBox}
sections={[{ data: hits }]}
它有一个稍微不同的 API,所以我更改了数据形状以传递给sections
prop。可能需要进一步更改。
编辑
您有一个未包含在 OP 中的额外View
内容,并且您没有SearchBox
将renderSectionHeader
. 顶部占用空间的所有内容都应从RootContainer
移至ConferenceList
:
<SectionList
sections={groupAndSortConferences(hits)}
renderItem={({ item }) => <ConferenceItem {...item} />}
keyExtractor={item => item.uuid}
ListFooterComponent={<LoadMoreFooter />}
renderSectionHeader={() => (
<View>
{Platform.OS === 'ios' && <View style={{ height: 24, backgroundColor: '#FFCA04' }} />}
<SearchBox />
</View>
)}
/>
您为 ios 添加的用于替换状态栏的硬编码View
虽然看起来很笨拙,但这是一个单独的问题。
推荐阅读
- javascript - 返回嵌套承诺但未找到响应
- r - Issues running a GLHT following a GLM in R due to 1 missing interaction
- php - 生产中的 Codeigniter 4 会话问题
- python - 熊猫保存并打开然后值更改为字符串问题
- amazon-web-services - 无法通过 CFT 在 CodeBuild 项目上成功设置 SNS,但手动工作
- rust - Does Rust have an elegant way to manipulate values further when assigning a value from a block statement?
- apache-kafka-connect - MirrorSourceConnector 不消耗
- javascript - 更新服务器内存中的对象与从数据库中提取更新的对象
- javascript - 提高大表数据集的渲染性能
- python-3.x - 'NoneType' 对象不适用于 beautifulsoup