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.