SectionList scrollToLocation does not scroll to the correct position
还没有人认领这个 Issue。
- 主要语言
- C++
- 星标
- 127k
- 派生
- 25.3k
- 平均合并
- 1 天 23 小时
- 30 天内合并 PR
- 4
描述
Description
When a SectionList contains a large number of items, calling scrollToLocation
scrolls to a slightly incorrect position, even though getItemLayout is properly provided.
Instead of scrolling exactly to the specified item, the list stops a bit offset from the expected location.
If this behavior is expected or if my usage of SectionList / getItemLayout is incorrect,
I would appreciate any guidance on the correct approach.
import React, { useRef, useMemo } from 'react';
import { SectionList, Text, View, Button } from 'react-native';
const HEADER_HEIGHT = 40;
const ITEM_HEIGHT = 30;
const TARGET_SECTION_INDEX = 20;
const generateSections = () => {
const createItems = (prefix) => {
const count = 20 + Math.floor(Math.random() * 100);
return [...Array(count)].map((_, i) => ({
label: `${prefix} Item ${i + 1}`,
}));
};
return [...Array(24)].map((_, sectionIndex) => ({
title: `Section ${sectionIndex + 1}`,
data: createItems(`Section ${sectionIndex + 1}`),
}));
};
export default function App() {
const sectionListRef = useRef(null);
const sections = useMemo(() => generateSections(), []);
const scrollToSection = () => {
if (sectionListRef.current) {
sectionListRef.current.scrollToLocation({
sectionIndex: TARGET_SECTION_INDEX,
itemIndex: 0,
viewPosition: 0,
});
}
};
return (
<View style={{ flex: 1, paddingTop: 40 }}>
<Button
title={`Scroll to Section ${TARGET_SECTION_INDEX + 1}`}
onPress={scrollToSection}
/>
<SectionList
ref={sectionListRef}
sections={sections}
keyExtractor={(item, index) => item.label + index}
renderItem={({ item }) => (
<View
style={{
height: ITEM_HEIGHT,
justifyContent: 'center',
borderBottomWidth: 1,
borderColor: '#ccc',
}}>
<Text>{item.label}</Text>
</View>
)}
renderSectionHeader={({ section: { title } }) => (
<View
style={{
justifyContent: 'center',
height: HEADER_HEIGHT,
backgroundColor: '#eee',
}}>
<Text
style={{
fontWeight: 'bold',
}}>
{title}
</Text>
</View>
)}
getItemLayout={(_, index) => ({
index,
length: ITEM_HEIGHT,
offset: ITEM_HEIGHT * index,
})}
/>
</View>
);
}
Steps to reproduce
- Render a SectionList
- Call scrollToLocation with a valid sectionIndex and itemIndex
- Observe the scroll position
React Native Version
0.81.4
Affected Platforms
Other (please specify), Runtime - Android
Output of npx @react-native-community/cli info
skip
Stacktrace or Logs
nothing
MANDATORY Reproducer
https://snack.expo.dev/@kuroppi/sectionlist-scrolltolocation
Screenshots and Videos
expected:
actual:
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
在 Android 上运行链接的 Snack 复现器,并将 SectionList.scrollToLocation 与提供的 getItemLayout 行为进行比较。首先跟踪 SectionList scrollToLocation 入口点以及对 sectionIndex 和 itemIndex 的处理。完成的标准是,请求的 section 和 item 在大型列表中到达预期位置。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- react-native, typescript
- 领域
- mobile
- Issue 类型
- 缺陷
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 活跃度
- 冷清
- 描述清晰度
- 基本清楚
- 新手友好度
- 45/100