在SectionList上执行scrollToLocation时崩溃

par*_*ohy 2 jsx reactjs react-native react-native-sectionlist

我们的应用程序中有一个保护套。呈现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);
};
Run Code Online (Sandbox Code Playgroud)

节列表渲染:

        <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}
        />
Run Code Online (Sandbox Code Playgroud)

我尝试通过Google搜索原因,但仅找到过时和已解决的问题而没有解决方案,但未成功。这件事发生在别人身上吗?您是如何解决的?

更新: 我们提出了一个固定项目大小的解决方案,该解决方案还考虑了可访问性比例因子。所以我们有一个项目和标题大小,可以在中使用getItemLayout。所有人都应有的工作,但SectionList故障。当我们滚动到下半部分时,列表本身是跳跃的,没有任何交互。到目前为止,我们最好的解决方案是用本机代码自己构建节列表,并使用它代替RN列表。

And*_*rew 5

您收到此错误是因为scrollToIndex失败并且您尚未实现getItemLayoutonScrollToIndexFailed


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);
};
Run Code Online (Sandbox Code Playgroud)