Commit a9e6759
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: 95fd673a87cf5ada3bf9a7c166bba77ce557c89b1 parent 5c34ce0 commit a9e6759
File tree
5 files changed
+88
-17
lines changed- packages/react-native/ReactAndroid
- api
- src/main/java/com/facebook/react/views/scroll
5 files changed
+88
-17
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6319 | 6319 | | |
6320 | 6320 | | |
6321 | 6321 | | |
| 6322 | + | |
6322 | 6323 | | |
6323 | 6324 | | |
6324 | 6325 | | |
| |||
6443 | 6444 | | |
6444 | 6445 | | |
6445 | 6446 | | |
| 6447 | + | |
6446 | 6448 | | |
6447 | 6449 | | |
6448 | 6450 | | |
| |||
6551 | 6553 | | |
6552 | 6554 | | |
6553 | 6555 | | |
| 6556 | + | |
6554 | 6557 | | |
6555 | 6558 | | |
6556 | 6559 | | |
| |||
Lines changed: 2 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
118 | 118 | | |
119 | 119 | | |
120 | 120 | | |
121 | | - | |
| 121 | + | |
122 | 122 | | |
123 | 123 | | |
124 | 124 | | |
| |||
129 | 129 | | |
130 | 130 | | |
131 | 131 | | |
132 | | - | |
| 132 | + | |
133 | 133 | | |
134 | 134 | | |
135 | 135 | | |
| |||
Lines changed: 34 additions & 15 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1324 | 1324 | | |
1325 | 1325 | | |
1326 | 1326 | | |
| 1327 | + | |
| 1328 | + | |
| 1329 | + | |
| 1330 | + | |
| 1331 | + | |
| 1332 | + | |
| 1333 | + | |
1327 | 1334 | | |
1328 | 1335 | | |
1329 | 1336 | | |
| |||
1377 | 1384 | | |
1378 | 1385 | | |
1379 | 1386 | | |
1380 | | - | |
1381 | | - | |
1382 | | - | |
| 1387 | + | |
| 1388 | + | |
| 1389 | + | |
| 1390 | + | |
| 1391 | + | |
| 1392 | + | |
| 1393 | + | |
| 1394 | + | |
1383 | 1395 | | |
1384 | 1396 | | |
1385 | | - | |
| 1397 | + | |
1386 | 1398 | | |
1387 | 1399 | | |
1388 | | - | |
1389 | | - | |
1390 | | - | |
1391 | | - | |
1392 | | - | |
1393 | | - | |
1394 | | - | |
1395 | | - | |
1396 | 1400 | | |
1397 | | - | |
| 1401 | + | |
1398 | 1402 | | |
1399 | 1403 | | |
1400 | 1404 | | |
| |||
1413 | 1417 | | |
1414 | 1418 | | |
1415 | 1419 | | |
1416 | | - | |
1417 | | - | |
| 1420 | + | |
1418 | 1421 | | |
1419 | 1422 | | |
1420 | 1423 | | |
1421 | 1424 | | |
1422 | 1425 | | |
1423 | 1426 | | |
| 1427 | + | |
| 1428 | + | |
| 1429 | + | |
| 1430 | + | |
| 1431 | + | |
| 1432 | + | |
| 1433 | + | |
| 1434 | + | |
| 1435 | + | |
| 1436 | + | |
| 1437 | + | |
| 1438 | + | |
| 1439 | + | |
| 1440 | + | |
| 1441 | + | |
| 1442 | + | |
1424 | 1443 | | |
1425 | 1444 | | |
1426 | 1445 | | |
| |||
Lines changed: 47 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1091 | 1091 | | |
1092 | 1092 | | |
1093 | 1093 | | |
| 1094 | + | |
| 1095 | + | |
| 1096 | + | |
| 1097 | + | |
| 1098 | + | |
| 1099 | + | |
| 1100 | + | |
| 1101 | + | |
| 1102 | + | |
| 1103 | + | |
| 1104 | + | |
| 1105 | + | |
| 1106 | + | |
| 1107 | + | |
| 1108 | + | |
| 1109 | + | |
| 1110 | + | |
| 1111 | + | |
| 1112 | + | |
| 1113 | + | |
| 1114 | + | |
| 1115 | + | |
| 1116 | + | |
| 1117 | + | |
| 1118 | + | |
| 1119 | + | |
| 1120 | + | |
| 1121 | + | |
| 1122 | + | |
| 1123 | + | |
| 1124 | + | |
| 1125 | + | |
| 1126 | + | |
| 1127 | + | |
| 1128 | + | |
| 1129 | + | |
| 1130 | + | |
| 1131 | + | |
| 1132 | + | |
| 1133 | + | |
| 1134 | + | |
| 1135 | + | |
| 1136 | + | |
| 1137 | + | |
| 1138 | + | |
| 1139 | + | |
| 1140 | + | |
1094 | 1141 | | |
1095 | 1142 | | |
1096 | 1143 | | |
| |||
Lines changed: 2 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
597 | 597 | | |
598 | 598 | | |
599 | 599 | | |
| 600 | + | |
| 601 | + | |
600 | 602 | | |
601 | 603 | | |
0 commit comments