在SectionList

时间:2019-01-10 13:26:55

标签: reactjs react-native jsx 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);
};

部分列表呈现:

        <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列表。

1 个答案:

答案 0 :(得分:3)

您收到此错误,因为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);
};
相关问题