调用SectionList的scrollToLocation方法后,scroll正在跳转

Ham*_*mid 5 javascript react-native react-native-sectionlist

我想在带有部分列表的网络上实现类似 scrollspy 的功能。我正在使用 scrollToLocation 方法。问题是滚动完成后滚动正在跳跃。我认为它是由加载前几行引起的。即使提供 getItemLayout 道具,我也不知道为什么会发生此问题。这是一个基本的演示。您可以在此处找到完整的代码示例。

在此处输入图片说明

这是我的部分列表:

 <SectionList
    style={{ backgroundColor: 'blue' }}
    renderItem={this.renderItem}
    renderSectionHeader={this.renderSectionHeader}
    sections={sections}
    keyExtractor={(item, index) => item.id}
    getItemLayout={this.getItemLayout}
    ref={me => this.sectionList = me}
    maxToRenderPerBatch={20}
  />
Run Code Online (Sandbox Code Playgroud)

这是我的getItemLayout功能:

 getItemLayout = (
  data,
  index,
 ) => {
   const layoutTable = layoutTableGenerator(data, 100, 40)

   return {length: layoutTable[index].length, offset: layoutTable[index].offset, index}
  }
Run Code Online (Sandbox Code Playgroud)

这是我生成数据的辅助函数getItemLayout

 module.exports = (sections, itemHeight, sectionHeaderHeight, sectionFooterHeight = 0) => {
   return sections.reduce((layoutTable, section, sectionIndex) => {
     const layoutTableLastItem = layoutTable[layoutTable.length - 1]
     const currentSectionLayoutTable = []

     currentSectionLayoutTable.push({
       length: sectionHeaderHeight,
       offset: layoutTableLastItem ? (layoutTableLastItem.offset + layoutTableLastItem.length) : 0
     })

     for(let i = 0; i < section.data.length; i++) {
       const currentSectionLayoutTableLastItem = currentSectionLayoutTable[currentSectionLayoutTable.length - 1]

       currentSectionLayoutTable.push({
         length: itemHeight,
         offset: currentSectionLayoutTableLastItem.offset + currentSectionLayoutTableLastItem.length
       })
      }

      const currentSectionLayoutTableLastItem = currentSectionLayoutTable[currentSectionLayoutTable.length - 1]
      currentSectionLayoutTable.push({
        length: sectionFooterHeight,
        offset: currentSectionLayoutTableLastItem.offset + currentSectionLayoutTableLastItem.length
      })

     return [
       ...layoutTable,
       ...currentSectionLayoutTable
     ]
  }, [])
 }
Run Code Online (Sandbox Code Playgroud)

Hun*_*san -1

这是因为SectionList使用时需要考虑标头getItemlayout链接详细描述了它,还链接了一个npm包作为解决方案