CollectionView: Scrolling Past the Current ItemsSource

Nathan Sokalski 4,106 Reputation points
2020-12-30T03:02:27.137+00:00

I have several CollectionView(s) for which I want to synchronize the scroll positions (I haven't figured out how to do this yet, but that is a different post). Because the CollectionViews will sometimes have different numbers of items, some of them may end up needing to be scrolled past the last item. Because the CollectionView currently only allows scrolling using the ScrollTo method, which uses an item index (to the best of my knowledge there is no way to scroll to an exact offset), entering an index larger than the number of items simply scrolls to the last item. Is there any way to scroll to an exact position or an index greater than the number of items?

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

1 answer

Sort by: Most helpful
  1. Cole Xia (Shanghai Wicresoft Co,.Ltd.) 6,751 Reputation points
    2020-12-30T09:21:24.857+00:00

    Hello,

    Welcome to Microsoft Q&A!

    Xamarin.Forms does not provide the Api that make collectionView scroll to specific position .

    We need to do this in target platform project with custom renderer .

    • Create a class derives from CollectionView public class MyView : CollectionView
      {
      public delegate void MyEventHandler(float offset);
             // Declare the event.  
             public event MyEventHandler MyEvent;  
      
      
             public void ScrollToPosition(float offset)  
             {  
                 MyEvent(offset);  
             }   
         }  
      
    • Call native api in Custom Renderer ,check the following method in iOS platform [assembly:ExportRenderer(typeof(MyView),typeof(MyRenderer))]
      namespace CollectionViewDemos.iOS
      {
      class MyRenderer : CollectionViewRenderer
      {
      protected override void OnElementChanged(ElementChangedEventArgs<GroupableItemsView> e)
      {
      base.OnElementChanged(e);
                 if (Control != null)  
                 {  
                     NSArray s = Control.ValueForKey(new NSString("_subviewCache")) as NSMutableArray;  
                     UICollectionView c = s.GetItem<UICollectionView>(0);  
      
                     MyView view = Element as MyView;  
                     view.MyEvent += (offset) =>  
                     {  
                         c.SetContentOffset(new CoreGraphics.CGPoint(0, offset), true);  
                     };             
                 }  
             }  
      
         }  
      
      }
    • Call the method when you want to scroll . MyView my = collectionView as MyView;
      my.ScrollToPosition(300); //the offset in vertical direction

    Thank you.


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.