-
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
[FlatList] Scrolling issues with FlatList when rows are variable height #13727
Comments
@MrChe Arrow functions are bad for performances: https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/jsx-no-bind.md |
@giladno I'm facing the same issue. Any fixes? |
@giladno Very interesting information! Thk) |
@ugiacoman no fix for me yet. I want to try the new 0.44 version to compare with. |
@giladno v0.44 Partially fixed it for me :) |
@ugiacoman please explain "partially" :) |
@giladno So the FlatList correctly calculates the viewport so I can scroll all the way down. However if I add another component within the same Flexbox, it simply pushes it down and I end up not being able to scroll all the way. Now that I think about it, that may not be a bug. I need to do more testing to figure out the precise requirements so that I can ensure it renders correctly. |
Hi @giladno any update for this? I am kinda facing the same problem, the current solution is to init the |
@ariona Sadly, no solution yet for me :( |
I have this problem on a project of mine. My solution has been to dynamically calculate the height of each cell as a function of the data it contains, and to include the height information in the data blob I pass to the list so that I can implement |
@Ashoat Could you please share the code where you render the component and calculate its height? Thanks. |
@Gildano: np, here you go - https://gist.github.com/Ashoat/c283a40b052ae1e4571e944abcef0fd1 It's a React component called TextHeightMeasurer. You hand it:
Besides text, it should be possible to identify the height of everything else programmatically. |
@Ashoat much appreciated buddy!! |
@Ashoat Could you please contact me in private (check my profile page)? I have a consulting job for you. |
@giladno v0.44 doesn't fix it, I just updated and still having the same issue. |
@parkerproject Yeah, that's pretty bad. I'm willing to pay for a bug fix :( |
Didn't have the exact same issue as you did, but I did have the same kind of data (images + random lengths of text). When I switched to FlatList i ran into various perf-problems I couldn't get out of. Switching back to ListView solved it for me. Too bad, since the FlatList api is so much nicer. |
@ippa Thanks, I was originally using ListView and it was ok, the problem is that it doesn't support jump to item, which is crucial in my case. |
@Ashoat TextHeightMeasurer looks like it'll be handy thanks! How do you use it within an app though? Is it meant to replace a component or do you iterate through records and render the TextHeightMeasurer component off screen then display the list once the measurements are ready? |
It's sorta like StatusBar, it doesn't render anything so you can place it anywhere. I place it in the same View my FlatList is in. The version in the gist I linked above probably has a bug or two, here's the latest version: https://github.com/Ashoat/squadcal/blob/master/native/text-height-measurer.react.js |
Nice, thanks. I ended up using onLayout then storing the dimensions of each cell. I then used onViewableItemsChanged to determine whether a cell should be visible or not and stored this information too. Finally, the render method checks whether the cell should be visible or not. If not it returns an empty view with the previously stored version (otherwise cells would jump around). Additionally onViewableItemsChanged works out the direction that the scroll view is moving based on which cells are being removed. It then adds make sure there's always X cells visible before of the first cell and X cells visible after the last cell. Doing this means I can now scroll through a lot more cells and the user only sees the blank cells in rare cases (a really low res image could be used to help with design). I'll release it as a component next week some time. |
@ippa what perf issues did you have migrating off ListView? |
@giladno: does FlatList have any more problems than ListView for you, or is it mostly just the scrollToIndex with variable height problem? |
This issue is more to do with the styling. My approach was to add footer component to the Flatlist, though it might not be the optimal solution, it gets the job done for now You can adjust the height to meet you needs |
I'm guessing there is an issue with the layout of your screen that's not FlatList specific. Try mucking with the style prop. A complete code snippet for the whole page might help others identify the bug. |
This may be a dumb question, but is there a reason you're not using "scrollToEnd()"? I had a pretty big issue with this until I attached that. I was adding dynamically sized components, and it wasn't going all the way to the bottom. Note, that it doesn't fire properly under "componentDidUpdate()" so I created my own method which fires it:
I'm not sure if this is NEEDED or I'm just missing something, but it's far easier than calculating each individual component size (unless you need to go to a specific component, not the bottom). |
@lprhodes would love too see your I'm currently experiencing jumps due to objects reverting to their collapsed state once they are off screen (via |
nevermind fixed the jumping by slowing down the collapsing item animation and setting |
same issue, and this seems to be the standard behaviour. in the example page https://facebook.github.io/react-native/docs/using-a-listview.html if you add a lot of items to the FlatList example you will see that scrolling doesn't work properly, you scroll and then it gets back up to the beginning. |
Sorry, looking at the original issue again I see there might be an additional issue - you might be hitting the first caveat described in the docs. Quoting from https://facebook.github.io/react-native/docs/flatlist.html:
So when your items scroll out of the render window, they lose their state which included the height from the async There are many ways to get around this. One would be a simple synchronous cache of src -> height that you would read from when initializing your |
I think @eballeste is probably having that render window issue as well. BUT, as for the bottom content not being reachable, I still think it probably has to do with the layout of the rest of the app. @victorbadila mentioned it's reproable in the demo app, but it also repros with a plain ScrollView and ListView, not just FlatList. I'm guessing that something is messed up a shared component like the navigation library, or something like that. The proper fix will depend on the rest of the app, but you can also just add some marginBottom. |
@sahrens, nope! I'm good, I found the |
Great to hear! |
I use onLayout callback and inside it scrollToEnd work as expected, without getItemLayout implementation |
I use that method too but when i want to navigate to 200th item it has long delay. For now i use react-native-recyclerview-list but it only available for android. |
I'm having the same issue: a flatlist with variable height items and scrollToIndex not scrolling to the right position. But I noticed something interesting, the FlatList scrolls to the right position to then adjust to the wrong position: Here is an example of the behaviour: If the FlatList is able to scroll (even if briefly) to the right index, couldn't we just find a way make it stay there? I tried adding:
My last option is to previously calculate the height of each item (what @Ashoat suggested), but it's hard since in my original project I'm rendering facebook posts, tweets, instagram posts, blogs posts all with variable text and images. |
@xavieramoros, that's what I'm doing, I save the heights of the rendered
rows in my redux state and use the viewOffset parameter of the
scrollToIndex method to compensate for that jump that you see.
The viewabilityConfig.viewAreaCoveragePercentThreshold prop I use to
figure out when should the row be considered to be out of screen.
|
Solved this by specifying both I had a list of ~300 elements with variable heights. I knew ahead of time that certain rows would be of certain heights, so i pre-computed each row height (for Was getting (1) blank screens, (2) jittery movement, (3) flashes of the correct UI suddenly disappearing etc... before this fix. const cumulativeRowHeights = [
0,
...rowHeights.reduce(function(soFar, current) {
const next = (soFar[soFar.length - 1] || 0) + current;
soFar.push(next);
return soFar;
}, [])
].slice(0, data.length);
const getItemLayout = (data, index) => {
const length = rowHeights[index];
const offset = cumulativeRowHeights[index];
return {length, offset, index};
}
const onLayout = () => this.listRef.scrollToOffset({offset: cumulativeRowHeights[desiredIndex]})
...
<FlatList
{...otherProps}
ref={(ref) => { this.listRef = ref; }}
getItemLayout={getItemLayout}
onLayout={onLayout}
>
...
</FlatList> I then use |
Sorry for the late reply, what you mention @eballeste is very similar to what @pcattori uses. It's easy to calculate the offset when you know the height of the elements. I've updated the snack with both approaches (scrollToIndex and scrollToOffset) working correctly : But in my (real) case, my problem is that calculating the height of the elements is quite difficult . I was trying to find another way but I'll have to stick to these two for now. EDIT: I've also added to the expo @Ashoat text height measurer component, works wonderfully as well. EDIT2: Source code is here: https://github.com/xavieramoros/flatlist-initialScrollIndexIssue |
Hi @xavieramoros Can I look at the https://exp.host/@xavieramoros/snack-SyN4FJikz source code? thx! |
I have the same issue. |
Any fixes on this yet? |
from what I have seen it sometimes happens and sometimes does not, even though in the examples where I use it the code is really the same. once you get it to "work" in a specific example it will continue to work forever, so the behaviour seems to be pretty much deterministic. not sure what triggers it to not work in other examples. |
Unfortunate if you can not get it running even once :D. |
@RoarRain the source code is here: https://github.com/xavieramoros/flatlist-initialScrollIndexIssue |
I have the same issue. |
This worked for me: https://stackoverflow.com/a/48249803/1500515
|
Someone pointed out on another issue (lost the link) that your I managed to fix my issue with the following steps:
Works way smoother and straightforward than |
Still haven't been able to find a way to solve this problem. |
this my be usefull if any one have problem with scrolling in flatlist
|
I read this in VirtualizedList.js
And
Attention that when |
I have the same issue. https://github.com/godness84/react-native-recyclerview-list can solve this issue only for android. |
I did not find any way to use But I have a solution that may be a bit slow: The only solution that comes to my mind is using
|
I'm using a
FlatList
where each row can be of different height (and may contain a mix of both text and zero or more images from a remote server).I cannot use
getItemLayout
because I don't know the height of each row (nor the previous ones) to be able to calculate.The problem I'm facing is that I cannot scroll to the end of the list (it jumps back few rows when I try) and I'm having issues when trying to use
scrollToIndex
(I'm guessing due to the fact I'm missing getItemLayout).I wrote a sample project to demonstrate the problem (simply try to scroll to the end with your finger - it won't allow you)
The text was updated successfully, but these errors were encountered: