SectionList scrollToLocation does not scroll to the correct position
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- C++
- Star
- 127k
- Fork
- 25.3k
- Merge trung bình
- 1 ngày 23 giờ
- Pull request đã merge (30 ngày)
- 4
Mô tả
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:
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Chạy reproducer Snack được liên kết trên Android và so sánh SectionList.scrollToLocation với hành vi getItemLayout được cung cấp. Bắt đầu bằng cách lần theo điểm vào SectionList scrollToLocation và cách xử lý sectionIndex và itemIndex. Hoàn thành nghĩa là section và item được yêu cầu đạt đến vị trí mong đợi trong các danh sách lớn.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- react-native, typescript
- Lĩnh vực
- mobile
- Loại issue
- Lỗi
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức độ hoạt động
- Ít trao đổi
- Độ rõ ràng
- Khá rõ ràng
- Mức phù hợp với người mới
- 45/100