Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

[Bug] CollectionView's ItemsUpdatingScrollMode="KeepLastItemInView" doesn't work when populating data #12601

Closed
Tommigun1980 opened this issue Oct 26, 2020 · 3 comments

Comments

@Tommigun1980
Copy link

Tommigun1980 commented Oct 26, 2020

Hi.

I'd like to use a CollectionView for displaying chat messages, where I want it to scroll to the end of the messages as an initial state.

ItemsUpdatingScrollMode="KeepLastItemInView" doesn't work when the list is initially loaded, and only seems to work when adding new items to it.

So repro would be to have a CollectionView with an ItemsSource (with enough items to have it scroll), and ItemsUpdatingScrollMode="KeepLastItemInView". I also have MeasureAllItems as item sizing strategy, don't know if it affects.


Workaround attempt:
I tried to ScrollTo the end manually, but it takes an arbitrary amount of time before ScrollTo actually does anything (I guess it's populating the elements asynchronously from the ItemsSource), so I listen to ChildAdded instead to know when it has started to actually populate data. But even there I need to wait arbitrarily for ScrollTo to actually do anything. I really dislike adding this much hacks and random delays to get a list to scroll to the bottom. I keep an overlay on top of the collection view until it's loaded fully. But the drawback with this approach is that I can't know how long to delay the operations, and they are surely device and list contents specific. I've had to increase the delays to hide the issues and it's impossible to get it right. My current hack is:

        // TEMP HACK! ItemsUpdatingScrollMode="KeepLastItemInView" doesn't work when collection view is populated,
        // so wait until one element has been added and manually scroll to bottom.
        // I also tried to manually scroll to bottom when collection gets data, but that is also too early.
        // TODO: remove this nonsense once Xamarin fixes KeepLastItemInView when creating collection, or fixes
        // ScrollTo in conjunction with a collection being populated.
        private async void ChatMessages_ChildAdded(object sender, ElementEventArgs e)
        {
            if (this.tempHackHasRequestedInitialScrollToBottom)
            {
                this.tempHackHasRequestedInitialScrollToBottom = false;

                await Task.Delay(17); // HACK! trying to scroll while items are being added seems to be ignored

                this.ScrollToLastMessage(false);

                await Task.Delay(60); // HACK: scroll doesn't seem to be immediate and it doesn't return a Task to await on...
                this.HasFullyLoadedChatMessages = true;
            }
        }

Xamarin.Forms 5-pre3, iOS (didn't test on Android).

@Tommigun1980 Tommigun1980 added s/unverified New report that has yet to be verified t/bug 🐛 labels Oct 26, 2020
@PureWeen PureWeen added s/needs-info ❓ A question has been asked that requires an answer before work can continue on this issue. s/needs-repro ❔ This reported issue doesn't include a sample project reproducing the issue. Please provide one. and removed s/needs-info ❓ A question has been asked that requires an answer before work can continue on this issue. s/needs-repro ❔ This reported issue doesn't include a sample project reproducing the issue. Please provide one. labels Oct 27, 2020
@PureWeen PureWeen added e/2 🕑 2 and removed s/unverified New report that has yet to be verified labels Oct 27, 2020
@Tommigun1980
Copy link
Author

Just in case -- in your tests: please make sure to set the ItemsSource to a real binding, preferably to an ObservableCollection, as I am starting to think some issues can not be reproduced with an "inline" items source.

@Tommigun1980
Copy link
Author

Repro project at #13231 (bug 6).

@Tommigun1980
Copy link
Author

Closing this in favour of #13547.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants