-
Notifications
You must be signed in to change notification settings - Fork 24.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix maintainVisibleContentPosition on Android during momentum scroll (#…
…43425) Summary: When using maintainVisibleContentPosition (mvcp) on Android it doesn't work properly when items are added during a momentum scroll. This happens because the android scrollview momentum scroll animation overrides the scroll position that the mvcp implementation sets [here](https://github.com/facebook/react-native/blob/2d547a3252b328251e49dabfeec85f8d46c85411/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/scroll/MaintainVisibleScrollPositionHelper.java#L132). To fix this we need to cancel the momentum scroll animation, update the scroll position then restart the scroll animation with the previous animation remaining momentum. ## Changelog: [ANDROID] [FIXED] - Fix maintainVisibleContentPosition during momentum scroll Pull Request resolved: #43425 Test Plan: Tested in RNTester on Android with both vertical and horizontal scrollviews using the following code: ```ts // packages/rn-tester/js/RNTesterAppShared.js import { Button, SafeAreaView, ScrollView, Text, View, } from 'react-native'; import React, {useLayoutEffect, useRef, useState} from 'react'; const generateUniqueKey = () => `_${Math.random().toString(36).substr(2, 9)}` const initialData = Array.from(Array(100).keys()).map(n => ({ id: generateUniqueKey(), value: n, })) function ListItem({item}) { const color = `hsl(${item.value * 10}, 75%, 85%)`; return ( <View style={{ backgroundColor: color, height: 100, }}> <Text>List item: {item.value}</Text> </View> ); } export default function FlatListRepro() { const numToAdd = 10; const [numbers, setNumbers] = useState(initialData); const ref = useRef(); const addAbove = () => { setNumbers(prev => { const additionalNumbers = Array.from(Array(numToAdd).keys()) .map(n => ({ id: generateUniqueKey(), value: prev[0].value - n - 1, })) .reverse(); return additionalNumbers.concat(prev); }); }; useLayoutEffect(() => { ref.current.scrollTo({y: numbers.length * 100, animated: false}); // eslint-disable-next-line react-hooks/exhaustive-deps }, []); return ( <SafeAreaView style={{flex: 1}}> <View style={{flexDirection: 'row', alignItems: 'center'}}> <Button title="Add Above" onPress={addAbove} /> </View> <View> <ScrollView ref={ref} maintainVisibleContentPosition={{ minIndexForVisible: 0, }} > {numbers.map((item) => ( <ListItem key={item.id} item={item} /> ))} </ScrollView> </View> </SafeAreaView> ) } ``` Before: https://github.com/facebook/react-native/assets/2677334/a7102665-991e-449e-9d0a-ef06c370dc71 After: https://github.com/facebook/react-native/assets/2677334/5430ecb1-26a9-4c92-9f16-c762d75685db Reviewed By: javache Differential Revision: D54883984 Pulled By: NickGerleman fbshipit-source-id: 95fd673a87cf5ada3bf9a7c166bba77ce557c89b
- Loading branch information
1 parent
5c34ce0
commit a9e6759
Showing
5 changed files
with
88 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters