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)
归档时间: |
|
查看次数: |
1447 次 |
最近记录: |