9

我们的应用程序中有一个边缘案例。在呈现 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 列表。

4

1 回答 1

12

您收到此错误是因为 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);
};
于 2019-01-10T13:34:04.647 回答