From f3b9036a102c43a7f8ea3bba7b314e8c049052a1 Mon Sep 17 00:00:00 2001 From: Rui Marinho Date: Mon, 15 Jul 2019 19:32:00 +0100 Subject: [PATCH] [iOS] ItemsViewLayout needs strategy on the ctor (#6690) --- .../CollectionView/GridViewLayout.cs | 2 +- .../CollectionView/ItemsViewLayout.cs | 6 ++++-- .../CollectionView/ItemsViewRenderer.cs | 25 ++++++---------------- .../CollectionView/ListViewLayout.cs | 2 +- 4 files changed, 13 insertions(+), 22 deletions(-) diff --git a/Xamarin.Forms.Platform.iOS/CollectionView/GridViewLayout.cs b/Xamarin.Forms.Platform.iOS/CollectionView/GridViewLayout.cs index 4a892c7..f8e52e1 100644 --- a/Xamarin.Forms.Platform.iOS/CollectionView/GridViewLayout.cs +++ b/Xamarin.Forms.Platform.iOS/CollectionView/GridViewLayout.cs @@ -10,7 +10,7 @@ namespace Xamarin.Forms.Platform.iOS { readonly GridItemsLayout _itemsLayout; - public GridViewLayout(GridItemsLayout itemsLayout) : base(itemsLayout) + public GridViewLayout(GridItemsLayout itemsLayout, ItemSizingStrategy itemSizingStrategy) : base(itemsLayout, itemSizingStrategy) { _itemsLayout = itemsLayout; } diff --git a/Xamarin.Forms.Platform.iOS/CollectionView/ItemsViewLayout.cs b/Xamarin.Forms.Platform.iOS/CollectionView/ItemsViewLayout.cs index 57e8768..73a3f95 100644 --- a/Xamarin.Forms.Platform.iOS/CollectionView/ItemsViewLayout.cs +++ b/Xamarin.Forms.Platform.iOS/CollectionView/ItemsViewLayout.cs @@ -14,10 +14,12 @@ namespace Xamarin.Forms.Platform.iOS bool _determiningCellSize; bool _disposed; - protected ItemsViewLayout(ItemsLayout itemsLayout) + protected ItemsViewLayout(ItemsLayout itemsLayout, ItemSizingStrategy itemSizingStrategy) { Xamarin.Forms.CollectionView.VerifyCollectionViewFlagEnabled(nameof(ItemsViewLayout)); + ItemSizingStrategy = itemSizingStrategy; + _itemsLayout = itemsLayout; _itemsLayout.PropertyChanged += LayoutOnPropertyChanged; @@ -75,7 +77,7 @@ namespace Xamarin.Forms.Platform.iOS public Func GetPrototype { get; set; } - internal ItemSizingStrategy ItemSizingStrategy { get; set; } + internal ItemSizingStrategy ItemSizingStrategy { get; private set; } public abstract void ConstrainTo(CGSize size); diff --git a/Xamarin.Forms.Platform.iOS/CollectionView/ItemsViewRenderer.cs b/Xamarin.Forms.Platform.iOS/CollectionView/ItemsViewRenderer.cs index f010f87..299a3e7 100644 --- a/Xamarin.Forms.Platform.iOS/CollectionView/ItemsViewRenderer.cs +++ b/Xamarin.Forms.Platform.iOS/CollectionView/ItemsViewRenderer.cs @@ -59,23 +59,23 @@ namespace Xamarin.Forms.Platform.iOS } } - protected virtual ItemsViewLayout SelectLayout(IItemsLayout layoutSpecification) + protected virtual ItemsViewLayout SelectLayout(IItemsLayout layoutSpecification, ItemSizingStrategy itemSizingStrategy) { if (layoutSpecification is GridItemsLayout gridItemsLayout) { - return new GridViewLayout(gridItemsLayout); + return new GridViewLayout(gridItemsLayout, itemSizingStrategy); } if (layoutSpecification is ListItemsLayout listItemsLayout) { - return new ListViewLayout(listItemsLayout); + return new ListViewLayout(listItemsLayout, itemSizingStrategy); } // Fall back to vertical list - return new ListViewLayout(new ListItemsLayout(ItemsLayoutOrientation.Vertical)); + return new ListViewLayout(new ListItemsLayout(ItemsLayoutOrientation.Vertical), itemSizingStrategy); } - void TearDownOldElement(ItemsView oldElement) + protected virtual void TearDownOldElement(ItemsView oldElement) { if (oldElement == null) { @@ -118,8 +118,7 @@ namespace Xamarin.Forms.Platform.iOS protected virtual void UpdateLayout() { - _layout = SelectLayout(Element.ItemsLayout); - _layout.ItemSizingStrategy = Element.ItemSizingStrategy; + _layout = SelectLayout(Element.ItemsLayout, Element.ItemSizingStrategy); if (ItemsViewController != null) { @@ -129,17 +128,7 @@ namespace Xamarin.Forms.Platform.iOS protected virtual void UpdateItemSizingStrategy() { - if (ItemsViewController?.CollectionView?.VisibleCells.Length == 0) - { - // The CollectionView isn't really up and running yet, so we can just set the strategy and move on - _layout.ItemSizingStrategy = Element.ItemSizingStrategy; - } - else - { - // We're changing the strategy for a CollectionView mid-stream; - // we'll just have to swap out the whole UICollectionViewLayout - UpdateLayout(); - } + UpdateLayout(); } protected virtual ItemsViewController CreateController(ItemsView newElement, ItemsViewLayout layout) diff --git a/Xamarin.Forms.Platform.iOS/CollectionView/ListViewLayout.cs b/Xamarin.Forms.Platform.iOS/CollectionView/ListViewLayout.cs index 8c65248..9560cf1 100644 --- a/Xamarin.Forms.Platform.iOS/CollectionView/ListViewLayout.cs +++ b/Xamarin.Forms.Platform.iOS/CollectionView/ListViewLayout.cs @@ -5,7 +5,7 @@ namespace Xamarin.Forms.Platform.iOS { internal class ListViewLayout : ItemsViewLayout { - public ListViewLayout(ListItemsLayout itemsLayout): base(itemsLayout) + public ListViewLayout(ListItemsLayout itemsLayout, ItemSizingStrategy itemSizingStrategy) : base(itemsLayout, itemSizingStrategy) { } -- 2.7.4