Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

How to add a missing Event to CollectionView

hjavaher1hjavaher1 USMember ✭✭

Hi,
I really need to use an event that notifies me when the user stops scrolling (lifting finger from the display). Since Xamarin.Forms's CollectionView does not currently has that event built-in (that I can see anyways) I decided to create a custom renderer that extends the stock CollectionView. I'm quite new to all of this so I'm not sure if this is the best way.

Long story short, this functionality now works but I have unfortunately removed all the other events from my custom CollectionView. The code below is how I achieved this and I believe it's the fact that I overnight the control's delegates that it's not working. How do I do this without overriding the rest of the functionality?

using System.Diagnostics; 
using Foundation;
using test.Controls;
using test.iOS;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;

[assembly: ExportRenderer(typeof(MyCollectionView), typeof(MyCollectionViewRenderer))]
namespace test.iOS
{    
    public class MyCollectionViewRenderer : CollectionViewRenderer, IUICollectionViewDelegate, IUIScrollViewDelegate
    {
        protected override void OnElementChanged(ElementChangedEventArgs<GroupableItemsView> e)
        {
            base.OnElementChanged(e);

            if (Control != null)
            {
                NSArray views = Control.ValueForKey(new NSString("_subviewCache")) as NSMutableArray;
                UICollectionView collectionView = views.GetItem<UICollectionView>(0);

                var oldDelegates = collectionView.WeakDelegate;
                collectionView.Delegate = new MyCollectionViewDelegate((MyCollectionView)Element);

            }
        }  
    }

    public class MyCollectionViewDelegate : UICollectionViewDelegate
    {
        MyCollectionView customView;

        public MyCollectionViewDelegate(MyCollectionView collectionView)
        {
            this.customView = collectionView;
        }

        [Foundation.Export("scrollViewDidEndDragging:willDecelerate:")]
        public void ScrollEnded(UIKit.UIScrollView scrollView, bool willDecelerate)
        {
            Debug.WriteLine("Finished Scrolling");

            customView.RaiseScrollEnded(customView, willDecelerate);
        }
    }
}

Answers

  • LandLuLandLu Member, Xamarin Team Xamurai

    No, we can't.
    The new delegate will override all the existed events. So we need to implement them all in the new delegate.

  • JariKuipersJariKuipers Member

    I'm having the exact same problem.

    Honestly I really REALLY cannot believe that Xamarin is this incompetent. We need to know when the user stops scrolling, which is very much possible on Android AND iOS.

    The functions are right there. I can extend them with an [Export] attribute. The only thing is that we cannot lose existing functionality.

    Has anyone figured this out yet? It would really be a life saver.

Sign In or Register to comment.