-
Notifications
You must be signed in to change notification settings - Fork 1.9k
[Bug] CollectionView can not be scrolled to bottom #13547
Comments
Any chance to get the CollectionView fixed so that it can scroll to the end of a list (without first having to scroll through all the items to get there)? I am 100% dependent on getting this to work, and I think being able to scroll a list is an absolutely mandatory feature. |
@jsuarezruiz @hartez I'd really appreciate some correspondence on these four CollectionView bugs I have opened: I spent a ton of time isolating the issues and making repro projects for them, and I need some kind of resolution as I have a hard time finish up my work here before they are resolved. So I'd really appreciate some comments on time estimates, or any kind of correspondence regarding them. |
Attached a sample: I have modified #13231 sample adding a Button to scroll to the end. This sample use the latest Xamarin.Forms 5.0 Nuget Package and the scroll seems to work as expected: @Tommigun1980 I am missing something? or maybe have been fixed by latest @hartez PRs? |
Hi. The CollectionView should be scrolled to bottom after data fills up (think a chat app - you see the most recent messages at the bottom when it loads). This doesn’t work. The sample uses ItemsUpdatingScrollMode="KeepLastItemInView". So there should be no need for a scroll to bottom button. It should show the last messages after it loads the contents. |
Accidentally clicked close issue so reopening. @hartez So I want to clarify that most of the workarounds in this ticket are needed to scroll to the bottom only when initially filling up the data. The repro project does use ItemsUpdatingScrollMode="KeepLastItemInView" but it doesn’t scroll to the bottom, as evident by seeing the top of the list. So the hacks detailed in here forcefully scroll it to bottom, but for some reason these calls (like ScrollTo()) don’t work properly so even more workarounds are needed. |
@Tommigun1980 I have a work around for detecting the last visible item on demand rather than waiting for a scrolled event. Beware, it is pretty gross and can probably be cleaned up. First, create a class (for example AppCollectionView) that extends CollectionView.
Introduce a custom renderer on iOS that does this:
Start using Let me know if this helps you get to a slightly less hacky solution. I am working on a very similar problem for a chat app. |
Just made an Android custom renderer to do the same thing:
Now the following code works on both platforms:
Last thing: I noticed |
Thank you @AnthonyIacono. @hartez @jsuarezruiz Do you guys think that scrolling a CollectionView to bottom could be fixed so it would work out of the box? I mean it's one of the most important features in any kind of list view and I'd really appreciated if it could be fixed. |
@hartez @jsuarezruiz Hi. I don't think I can finish my application unless the CollectionView can be fixed so that it can be loaded in a state where the last items are visible. The 'KeepLastItemInView' setting does not work when the data is populated, which makes it almost impossible to do a chat feature with the CollectionView. Scrolling is an important concept and I don't think one can make apps without it working. Any chance this could get fixed? Let me know if something is not clear in the repro project. Thank you. |
Thank you for those who provided a workaround. Although on Android it only scrolls to the third to last element. But I've got another problem too.
|
We have an app that does messaging that has this bug, the work around it not great because it requires slowing scrolling through all the messages to get to the end. Which is to slow if there are a large number of messages. We have implemented the work around from above but with a few changes:
|
@hartez @jsuarezruiz Pinging about this yet again. The CollectionView can't be scrolled to bottom when data is loaded so it's not really usable atm. Is there any chance you guys could fix this? This is an absolute blocker for me and probably quite a lot of other people. Scrolling a CollectionView is a core feature. Thank you! |
@hartez hi is the intention not to fix the collection view till maui is out? I can see that this release was just done to keep people quiet but to be honest the fixes that mattered were not there. Maui will suffer from the same problems no? Can someone who calls the shot reply? |
How do you handle the keyboard appear and load more when scrolling top on your side? On my side, i wrote some custom function like InsertRange like this:
But when using together with KeepLastItemInView, the list will always jump to the end when it called ... Also, when the keyboard is appeared, the scrolling position is not kept anymore. Which make me have to do a hack that it will wait for the keyboard appear (changing the bottom margin) and scroll to the bottom, which is not very nice. =_= And yes, there are a lot of scrolling issues in the CollectionView, i'm having this when making may chat app as well! |
Slightly off topic, but if anyone is desperate for a solution that simply works, try SfListView, it has a Loaded event that will execute ScrollToRowIndex perfectly. Commercial, but it does work and is a drop-in replacement. |
Description
What: Trying to programmatically scroll a CollectionView to the bottom (ie to the last item in its ItemsSource).
Use case: Chat page. Load say 100 last messages, scroll the CollectionView to the bottom to display the last messages.
Problem: There seems to be a design mistake in the CollectionView as this is almost impossible to achieve.
There seems to be some internal logic in the CollectionView that assumes its visualisation starts at the beginning of its contents list. It should be possible to have say 100 elements and scroll to the bottom (to display say the last 15 of it), without first having to reveal/draw all 85 items before it - the CollectionView is supposed to provide virtualisation, but even moreso basic functionality to scroll to an arbitrary element.
Also, while hacking around to achieve this I was also able to uncover several events that were not fired correctly.
The bugs can be summed up as such:
Workaround: Use ScrollTo(lastIndex).
Workaround: Add a delay, say 100 milliseconds, before invoking ScrollTo (this is very bad as it's device and content dependent!).
Workaround: Call ScrollTo in a loop, and stop when the 'Scrolled' event says it's at the bottom. CollectionView seems to require that the element is visible [or has already been drawn] before it can be scrolled to. Furthermore, even this won't work unless ScrollTo is called with
animate: true
in certain cases.So the problems are that:
a) ItemsUpdatingScrollMode="KeepLastItemInView" does not scroll to the last message when populating data.
b) ScrollTo for an arbitrary element does not fully scroll to an element unless it has drawn it at least once (thus defeating the purpose of virtualisation). This forces one to use black magic to sequentially scroll to elements in the background while keeping the CollectionView obscured by some element to not make it visible to the user.
c) ScrollTo does nothing if called inside an event (Scrolled, ChildAdded, SizeChanged) -- one has to wait an arbitrary amount of time after these actions before ScrollTo does anything.
d) Not all events are called (such as 'Scrolled') for all actions, further increasing the number of hacks one has to perform.
So all in all, all I want to do is to ask the CollectionView to scroll to the last element, but it's not possible without the following (timing-dependent) hacks in a page's code-behind file:
This is on iOS - I haven't tested it on Android.
Also, even if ScrollTo did work properly it should not be necessary to draw the first elements if I want to display the last elements. ItemsUpdatingScrollMode="KeepLastItemInView" when populating data as a batch (ie filling the collection and sending a RESET event) should display the last items without first drawing the ones at the start of the list.
Steps to Reproduce
The repro project at #13231 can be used as basis for revealing the issues. It uses a real asynchronously populated data source and not a hardcoded XAML data source, which often masks issues, and displays the ItemsUpdatingScrollMode="KeepLastItemInView" issue (contents are not scrolled to bottom).
For the ScrollTo issues etc, I think it'd make sense for you to try to try editing said repro project in a way where it scrolls to the last message. The road blocks should become evident. I can make a repro project for this if needed but I think it'd be better for you to discover the issues along the way.
It is very important to use an actual data source (like an asynchronously populated ObservableCollection) instead of hard-coding an XAML ItemsSource, as the latter masks a lot of issues.
Expected Behavior
It should be possible to scroll to the last element in a CollectionView.
Actual Behavior
Scrolling to the last element in a CollectionView is not possible without a ton of timing-dependent hacks.
Basic Information
Environment
Show/Hide Visual Studio info
Workaround
See the post for a timing-dependent workaround, but it's not a good fix because:
So a real fix is pretty much required.
The text was updated successfully, but these errors were encountered: