We have an UICollectionView using an custom MvxCollectionViewSource as source. When selecting an item, we want to scroll to th item, do a short animation and then do a action. In the source the ItemSelected is overriden as follows where an event is set for ScrollAnimationEnded.
public override void ItemSelected(UICollectionView collectionView, NSIndexPath indexPath)
{
var item = GetItemAt(indexPath) as SlideMenuViewItemModel;
var cell = collectionView.VisibleCells[0];
var index = collectionView.IndexPathForCell(cell);
if (!Equals(index, indexPath))
{
collectionView.ScrollAnimationEnded += HandleAnimationDone;
collectionView.ScrollToItem(indexPath, UICollectionViewScrollPosition.CenteredHorizontally, true);
}
else
{
var layout = (SlideMenuLayout)collectionView.CollectionViewLayout;
layout.SelectedIndexPath = indexPath;
collectionView.PerformBatchUpdates(delegate { }, delegate
{
item.OnClick.Execute(null);
layout.SelectedIndexPath = null;
});
}
}
private void HandleAnimationDone(object sender, EventArgs args)
{
CollectionView.ScrollAnimationEnded -= HandleAnimationDone;
var layout = (SlideMenuLayout)CollectionView.CollectionViewLayout;
CollectionView.PerformBatchUpdates(delegate { }, delegate
{
// item.OnClick.Execute(null);
layout.SelectedIndexPath = null;
});
}
The problem is that it only works the first time. The item is selected and the scrolling is performed and the event is fired. However when trying to do the next selection the ItemSelected is never called. If I remove the line collectionView.ScrollAnimationEnded += HandleAnimationDone it keeps working. So it seems hooking up the event breaks something or I need to call something for resetting.
I hope you have a suggestion :)
Regards
I have not found a solution, as it seems that setting the event cut something else off. However I did a work-around, instead of using ScrollToItem and setting the animation-end event, I did a custom scrolling animation. The following shows how I did it.
public override void ItemSelected(UICollectionView collectionView, NSIndexPath indexPath)
{
var item = GetItemAt(indexPath) as SlideMenuViewItemModel;
var cell = GetCell (collectionView, indexPath);
CurrentIndex = indexPath;
UIView.Animate (0.5, 0, UIViewAnimationOptions.CurveEaseIn,
() => {
// do any animation
},
() => {
//do anything after the animation
}
);
}