-
Notifications
You must be signed in to change notification settings - Fork 24.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Android][Flatlist][Horizontal] Last item delete in horizontal flatlist does not adjust scroll position. #27504
Comments
This problem will most likely also be present on the latest version from the code it seems. |
The fix can be simple: add a hierarchy change + layout change listener, which does a |
Hey there, it looks like there has been no activity on this issue recently. Has the issue been fixed, or does it still require the community's attention? This issue may be closed if no further activity occurs. You may also label this issue as a "Discussion" or add it to the "Backlog" and I will leave it open. Thank you for your contributions. |
Thank you, kind little bot, but this still needs a fix :/ |
I would suggest to use scrollToEnd(), but anyway you need to know screen width and each item width. In case you have two or only one item in your list, you will need to use scrollToIndex({ index: 0 }) instead |
The PR (#27514) has the exact fix for Horizontal Scroll View which is already present in vertical scrollview in production/master since a long time. |
latest pr is #28977 |
I am facing this issue too. Any workaround while waiting for the fix? |
Up ! Same problem on my side... |
I have the same problem with react-native 0.64.2 😕 |
Same problem here. A workaround while waiting fix: use scrollToEnd when removing the last item. Example: import React, { useState, useRef, useCallback } from "react";
import { FlatList } from "react-native";
function ExampleComponent() {
const [items, setItems] = useState<[]>([]);
const flatListRef = useRef<FlatList>();
const handleRemoveFlatListItem = useCallback(
(index: number) => {
const canScrollToEnd = index === items.length - 1;
const newItems = [...items] as [];
newItems.splice(index, 1);
setItems([...newItems]);
if (flatListRef?.current && canScrollToEnd)
flatListRef.current.scrollToEnd();
},
[items, flatListRef]
);
const renderItem = ...
return (
<FlatList
horizontal
data={items}
renderItem={renderItem}
keyExtractor={(item) => `${item.id}`}
/>
);
} |
Same problem here, hope it get fixed soon. |
Hey there, it looks like there has been no activity on this issue recently. Has the issue been fixed, or does it still require the community's attention? This issue may be closed if no further activity occurs. You may also label this issue as a "Discussion" or add it to the "Backlog" and I will leave it open. Thank you for your contributions. |
Same problem on 0.63.4 |
Same problem on 0.67.4 |
+1 |
I have this issue on 0.67.4, but with a scrollview (horizontal) like:
|
Branch - Last item delete in horizontal flatlist does not adjust scroll position.
Test case documents PR #34141 sourcecode example
import React from "react";
import {
SafeAreaView,
View,
FlatList,
StyleSheet,
Text,
StatusBar,
Button,
} from "react-native";
const DATA = [
{
id: "bd7acbea-c1b1-46c2-aed5-3ad53abb28ba",
title: "First Item",
},
{
id: "3ac68afc-c605-48d3-a4f8-fbd91aa97f63",
title: "Second Item",
},
{
id: "58694a0f-3da1-471f-bd96-145571e29d72",
title: "Third Item",
},
{
id: "bd7acbea-c1b1-46c2-aed5-3ad53abb8bbb",
title: "Fourth Item",
},
{
id: "3ac68afc-c605-48d3-a4f8-fbd91aa97676",
title: "Fifth Item",
},
{
id: "58694a0f-3da1-471f-bd96-145571e27234",
title: "Sixth Item",
},
{
id: "58694a0f-3da1-471f-bd96-145571e29234",
title: "Seven Item",
},
{
id: "58694a0f-3da1-471f-bd96-145571429234",
title: "Eight Item",
},
{
id: "58694a0f-3da1-471f-bd96-115571429234",
title: "Nine Item",
},
{
id: "58694a0f-3da1-471f-bd96-1155h1429234",
title: "Ten Item",
},
];
const Item = ({ title }) => (
<Text style={[styles.item, styles.title]}>{title}</Text>
);
const renderItem = ({ item }) => <Item title={item.title} />;
const ITEM_HEIGHT = 50;
const renderFlatList = ({ item }) => (
<NestedFlatList item={item} />
);
function NestedFlatList(props) {
const [items, setItems] = React.useState(DATA);
return (
<View>
<Button
title="add an item"
onPress={() => setItems([...items, {title: 'new item'}])}
/>
<Button
title="remove an item"
onPress={() => {
const newItems = [...items];
newItems.splice(items.length - 1, 1)
setItems(newItems);
}}
/>
<Text>Flatlist</Text>
<FlatList
horizontal={true}
style={{height: 400}}
inverted={true}
renderItem={renderItem} data={items} />
</View>
)
}
const FlatList_nested = () => {
let flatlist = React.useRef(0);
return (
<FlatList
ref={(ref) => flatlist = ref}
data={[1,2,3]}
renderItem={renderFlatList}
keyExtractor={(item) => item.toString()}
/>
);
};
const styles = StyleSheet.create({
item: {
backgroundColor: "#f9c2ff",
padding: 20,
marginVertical: 8,
marginHorizontal: 16,
},
title: {
fontSize: 16,
},
});
export default ({
title: 'Nested',
name: 'nested',
description: 'nested FlatList',
render: () => <FlatList_nested />,
}: RNTesterModuleExample); Screen.Recording.2022-07-08.at.14.38.53.mov |
Main - Last item added/deleted in vertical flatlist does not adjust scroll position.
Screen.Recording.2022-07-11.at.22.06.54.mov |
same problem on Android on 0.71.3 |
Will re raise the fix after taking a look at source once again. |
So...I see this is closed, but it's still broken. Is there another thread where it's not closed? |
Given this is unfixed after 3 years I just went with a workaround. :-P In my case I'm not explicitly removing an item from a list, but rather getting updated data from an API call which could change the data in a horizontal list. If viewing the last item when this happens it will exhibit this behavior. And since I don't explicitly know that the last item was removed (or any item to make the list shorter I guess) I just went with a more brute force approach. I store the current content offset by observing Bit of a pain, but not too bad and works for me. |
still broken |
sigh! |
call Can do more optimisations to restrict calling
|
This is still an issue. |
React Native version: 0.59.10
Steps To Reproduce
Describe what you expected to happen:
After removing last item, scroll position should be adjusted.
Snack, code example, screenshot, or link to a repository:
The text was updated successfully, but these errors were encountered: