我正在尝试在我的 React Native 部分列表中的滚动动量完成后运行一个函数。
参考文档后,我发现了
onMomentumScrollEnd
。但问题是 onMomentumScrollEnd
仅在我们手动滚动部分列表时才起作用。但我在函数中使用 scrollToLocation
滚动到特定部分。使用时,onMomentumScrollEnd
似乎不会触发。使用scrollToLocation
时如何在滚动完成时触发此功能
<SectionList
sections={groupActivities}
keyExtractor={(item) => item?.id?.toString()}
ref={sectionList}
getItemLayout={getItemLayout}
onMomentumScrollEnd={() => console.log('ends')}
renderItem={({ section: { title }, item, index }) => {
const sectionTitle = String(title);
return (
<View style={activityContainer}>
<MCard
startTime={getFormattedStartTime(item.duration?.start)}
/>
</View>
);
}}
renderSectionHeader={({ section: { title } }) => (
<MText>
{setSectionTitle}
</MText>
)}
refreshing={groupActivityLoading}
onRefresh={handleRefresh}
/>
你如何触发
scrollToLocation
?
请使用参考文献检查我的示例:
const Example = () => {
const sectionListRef = useRef(null);
return (
<SafeAreaView style={styles.container}>
<Button
title="Scroll"
onPress={() => {
sectionListRef.current.scrollToLocation({
itemIndex: 6,
});
}}
/>
<SectionList
ref={sectionListRef}
onMomentumScrollEnd={() => alert('ends')}
sections={DATA}
keyExtractor={(item, index) => item + index}
renderItem={({item}) => <Item title={item} />}
renderSectionHeader={({section: {title}}) => (
<Text style={styles.header}>{title}</Text>
)}
/>
</SafeAreaView>
);
};
完整代码: 如果您想在您的设备上进行测试,这里是完整的代码:
https://gist.github.com/tiagotedsky/076887546530a509ee602318f92ca7ac