我们的应用程序中有一个边缘案例。在呈现UI并且用户尝试滚动到某个部分之后,它会抛出scrolltoindex should be used in conjunction with getitemlayout or on scrolltoindex failed
。现在只有在UI渲染后立即执行此操作时才会发生这种情况。
_scrollToSection = index => {
setTimeout(() => {
this.list.scrollToLocation({
animated: true,
itemIndex: -1,
sectionIndex: index,
viewPosition: 0
});
}, 150);
};
部分列表呈现:
<SectionList
sections={this.props.sections}
extraData={this.props.subscriber}
ref={ref => {
if (ref) {
this.list = ref;
}
}}
automaticallyAdjustContentInsets={true}
contentInsetAdjustmentBehavior={'automatic'}
windowSize={100}
ListHeaderComponent={this.props.header || null}
ItemSeparatorComponent={() => (
<Separator
style={[mediumStyle.separatorEnd, { backgroundColor: IOS_GREY_02_03 }]}
/>
)}
renderSectionFooter={() => <View style={{ height: 17 }} />}
keyExtractor={(item, index) => index}
removeClippedSubviews={false}
stickySectionHeadersEnabled={true}
renderSectionHeader={({ section }) => (
<SectionTitle title={section.title} theme={this.props.theme} />
)}
renderItem={this._renderItem}
onEndReachedThreshold={0}
onEndReached={() => HapticFeedback.trigger()}
scrollEventThrottle={16}
/>
我试图谷歌的原因,但没有成功找到只有过时和关闭的问题没有解决方案。这是否发生在其他人身上?你是怎么修理的?
更新:我们已经提出了一个恒定项目大小的解决方案,它也可以计算可访问性比例因子。所以我们有一个项目和标题大小我们可以在getItemLayout
中使用。一切都按原样运作,但SectionList
是毛病。当我们滚动到较低的部分时,列表本身是跳跃的,没有任何交互。到目前为止,我们最好的解决方案是使用本机代码自己构建节列表,并使用它而不是RN列表。
您收到此错误是因为scrollToIndex失败并且您尚未实现getItemLayout
或onScrollToIndexFailed
getItemLayout
在一个部分列表中设置并不是很有趣但是这个媒体帖子讨论了如何做https://medium.com/@jsoendermann/sectionlist-and-getitemlayout-2293b0b916fb
他们建议react-native-section-list-get-item-layout
计算布局https://github.com/jsoendermann/rn-section-list-get-item-layout的大小
onScrollToIndexFailed
更容易设置你可以添加道具onScrollToIndexFailed={(info) => { /* handle error here /*/ }}
你可以捕捉到错误,然后决定你将如何处理它。
我还要添加一个检查,以确保在调用this.list
函数之前存在对scrollToLocation
的引用。像这样的东西。
_scrollToSection = index => {
setTimeout(() => {
if (this.list) {
this.list.scrollToLocation({
animated: true,
itemIndex: -1,
sectionIndex: index,
viewPosition: 0
});
}
}, 150);
};