CollectionView: Scrolling to Exact Position

Nathan Sokalski 4,116 Reputation points
2020-12-04T20:24:21.183+00:00

I have several CollectionView(s) for which I want to scroll to an exact position. However, the only method offered by CollectionView that I could find was ScrollTo which scrolls to an index or item, not a specific position. I also could not find any properties indicating the current scroll position of the CollectionView (such as VerticalOffset). When the user scrolls, it does not snap to the items (I don't want it to, which is why I did not use Snap Points), so I need to know the exact scroll position so that I can re-scroll to that same position later on if needed. Where can I get this info, and how can I use it to scroll to the desired position?

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,294 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. JessieZhang-MSFT 7,706 Reputation points Microsoft Vendor
    2020-12-07T07:41:48.717+00:00

    Hello,

    Welcome to our Microsoft Q&A platform!

    Yes, it is just the case as you said. We can scroll an item at an index into view or Scroll an item into view.

    And we can detect scrolling and get the HorizontalOffset and VerticalOffset by using the following event:

    <CollectionView Scrolled="OnCollectionViewScrolled">  
        ...  
    </CollectionView>  
    void OnCollectionViewScrolled(object sender, ItemsViewScrolledEventArgs e)  
    {  
        Debug.WriteLine("HorizontalDelta: " + e.HorizontalDelta);  
        Debug.WriteLine("VerticalDelta: " + e.VerticalDelta);  
        Debug.WriteLine("HorizontalOffset: " + e.HorizontalOffset);  
        Debug.WriteLine("VerticalOffset: " + e.VerticalOffset);  
        Debug.WriteLine("FirstVisibleItemIndex: " + e.FirstVisibleItemIndex);  
        Debug.WriteLine("CenterItemIndex: " + e.CenterItemIndex);  
        Debug.WriteLine("LastVisibleItemIndex: " + e.LastVisibleItemIndex);  
    }  
    

    If you want to scroll to exact position , you can create an issue here: https://github.com/xamarin/xamarin-forms-samples/issues

    Best Regards,

    Jessie Zhang

    ---
    <font color="grey">Xamarin forums are migrating to a new home on Microsoft Q&A!
    We invite you to post new questions in the Xamarin forums’ new home on Microsoft Q&A!
    For more information, please refer to this sticky post.</font>

    0 comments No comments