Forum Xamarin Xamarin.Forms

How to make ScrollTo on Android show the group header?

When using a ListView with grouping on, I'd like to be able to scroll the ListView so that the header of a particular group ends up on the top of the screen. This is easy to do on iOS using the built in ScrollTo method simply by calling it with the scroll target set to the first element in the target group (presumably because iOS always keeps a group header at the top at all times) but on Android the same call results in the first element being at the very top of the view with the header just slightly above it (off the screen/out of view). Please see attached screenshots and attached sample app.

How can I make the ListView scroll on Android like it does on iOS? I started looking into copying/forking the vanilla Xamarin.Forms.Android ListViewRenderer and tweaking the index handling of the OnScrollToRequested method but that made me feel very queasy.

For illustration, a couple of workarounds I had considered:

  • Call ScrollTo(target , ScrollToPosition.Center, true) rather than ScrollTo(target , ScrollToPosition.Start, true) - the group header will always be visible after the scroll, albeit with some space above it
  • Call ScrollTo but target the last element of the previous group - this will leave a little bit of space at the top still


Sign In or Register to comment.