您可以反转您的平面列表,然后也反转您的数据透视图。我的意思是,当您反转平面列表的方向时,您会在其末尾添加数据,然后不会发生跳转。我修改了您的共享代码并将其粘贴在下面以清楚我所说的希望对您有所帮助:
import * as React from 'react';
import { Text, View, Button, FlatList } from 'react-native';
let lastContentHeight = 0;
let lastY = 0;
export default function App() {
const [data, setdata] = React.useState([]);
const flatlist = React.useRef();
const addData = React.useCallback(() => {
const first = data.length ? data[data.length-1].key : 1;
const newArray = [];
for (let i = first ; i <first+ 15; i++) {
newArray.push({ key: i });
}
setdata([ ...data,...newArray]);
}, [data]);
React.useEffect(() => {
addData();
}, []);
// const onContentSizeChange = (contentWidth, contentHeight) => {
// const delta = contentHeight - lastContentHeight;
// const newY = lastY + delta;
// flatlist.current.scrollToOffset({ animated: false, offset: newY });
// lastY = newY;
// lastContentHeight = contentHeight;
// };
const onScroll = ({
nativeEvent: {
contentOffset: { y },
},
}) => {
lastY = y;
};
return (
<View style={{ height: 600 }}>
<Button onPress={addData} title="Prepend data" />
<FlatList
inverted
ref={flatlist}
data={data}
renderItem={({ item }) => <Text style={{ padding: 80 }}>{item.key}</Text>}
// onContentSizeChange={onContentSizeChange}
onScroll={onScroll}
/>
</View>
);
}