Bug 54241 - ListView.ScrollTo doesn't work with animated: false when called early in page lifetime
Summary: ListView.ScrollTo doesn't work with animated: false when called early in page...
Alias: None
Product: Forms
Classification: Xamarin
Component: Android ()
Version: 2.3.3
Hardware: PC Mac OS
: --- normal
Target Milestone: ---
Assignee: Jason Smith [MSFT]
Depends on:
Reported: 2017-03-29 19:23 UTC by Chase Long
Modified: 2017-06-19 21:18 UTC (History)
2 users (show)

Tags: android, lollipop, listview, scrollto, ac
Is this bug a regression?: ---
Last known good build:

Example solution for reproducing the bug (215.68 KB, application/zip)
2017-03-29 19:23 UTC, Chase Long

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report for Bug 54241 on Developer Community or GitHub if you have new information to add and do not yet see a matching new report.

If the latest results still closely match this report, you can use the original description:

  • Export the original title and description: Developer Community HTML or GitHub Markdown
  • Copy the title and description into the new report. Adjust them to be up-to-date if needed.
  • Add your new information.

In special cases on GitHub you might also want the comments: GitHub Markdown with public comments

Related Links:

Description Chase Long 2017-03-29 19:23:16 UTC
Created attachment 21030 [details]
Example solution for reproducing the bug

# Description
ListView.ScrollTo is broken on Android with animated: false in certain use cases. I'm not sure exactly what causes it, but it seems to happen if ListView.ScrollTo is called very soon after the view is instantiated, e.g. before its the view's constructor completes.
The call will appear to have no effect with animated: false. (bad)
The call will scroll the list correctly if ListView.ScrollTo is called with animated: true. (good)
This only affects Android.

Tested on Xamarin.Forms and

Also see https://forums.xamarin.com/discussion/comment/262351#Comment_262351.

# Example
Run the sample solution. You will need to swap between commenting out lines 23 and 24 of MainPage.xaml.cs to test each case.

## Expected behavior
The list scrolls the list item "30" into view when the page loads.

## Actual behavior
Android, animated: false - The list does not scroll at all. (bad)
Android, animated: true - The list scrolls list item "30" into view when the page loads. (good)
iOS, animated: false - The list scrolls list item "30" into view when the page loads. (good)
iOS, animated: true - The list scrolls list item "30" into view when the page loads. (good)

# Resolution
The documentation at https://developer.xamarin.com/api/member/Xamarin.Forms.ListView.ScrollTo/p/System.Object/Xamarin.Forms.ScrollToPosition/System.Boolean/ should specify what will happen when calls are made to ScrollTo before the ListView is rendered.
I am assuming it has not been rendered before the ScrollTo call in the example, but I am not 100% sure.
The source code of Xamarin.Forms.Platform.Android.ListViewRenderer includes a field "_pendingScrollTo". At a glance, it seems the ScrollTo request should be stored until the ListView is rendered, at which point the scroll should happen.
Comment 1 Jimmy [MSFT] 2017-04-03 16:17:18 UTC
Hi Chase, thank you for filing this report! Let me run the attached project to see if I can confirm this issue and I will update the report with my results.
Comment 2 Jimmy [MSFT] 2017-04-03 21:59:45 UTC
I was able to reproduce this issue so I will confirm the report.

However, I was only able to reproduce this on an Android 5.0 (API 21). The ListView scrolled as expected when I ran the project on an Android 4.2, 4.4 and 6.0 emulator, and 6.0 device. Are you testing with Android 5.0, or are you seeing this issue on other versions?

I also tested calling ScrollTo() inside OnAppearing() instead, but the issue still occurred unless I added a small (500ms) delay. So this could be used as another workaround if you do not want to enable the animation.

### Version Tests        BAD
2.3.4-pre6      BAD       BAD
Comment 3 Chase Long 2017-04-03 22:09:32 UTC
Jimmy: I used an emulator. Intel Atom x86, Android 6.0 API level 23.