5

我想用部分列表在网络上实现类似 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}
  />

这是我的getItemLayout功能:

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

   return {length: layoutTable[index].length, offset: layoutTable[index].offset, index}
  }

这是我生成数据的辅助函数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
     ]
  }, [])
 }
4

1 回答 1

-1

那是因为SectionList在使用getItemlayout. 这个链接详细描述了它,还链接了一个npm包作为解决方案

于 2018-12-28T22:59:54.183 回答