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