Skip to content

Commit

Permalink
Make sure transition from empty CollectionView measures cell size; fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
hartez authored and felipebaltazar committed Oct 16, 2019
1 parent 3aeee7a commit ed57fe3
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,29 @@ public class CollectionViewGallery : ContentPage
{
public CollectionViewGallery()
{
Content = new StackLayout
Content = new ScrollView
{
Children =
{
new Button { Text ="Enable CollectionView", AutomationId = "EnableCollectionView", Command = new Command(() => Device.SetFlags(new[] { ExperimentalFlags.CollectionViewExperimental })) },
GalleryBuilder.NavButton("Default Text Galleries", () => new DefaultTextGallery(), Navigation),
GalleryBuilder.NavButton("DataTemplate Galleries", () => new DataTemplateGallery(), Navigation),
GalleryBuilder.NavButton("Observable Collection Galleries", () => new ObservableCollectionGallery(), Navigation),
GalleryBuilder.NavButton("Snap Points Galleries", () => new SnapPointsGallery(), Navigation),
GalleryBuilder.NavButton("ScrollTo Galleries", () => new ScrollToGallery(), Navigation),
GalleryBuilder.NavButton("CarouselView Galleries", () => new CarouselViewGallery(), Navigation),
GalleryBuilder.NavButton("EmptyView Galleries", () => new EmptyViewGallery(), Navigation),
GalleryBuilder.NavButton("Selection Galleries", () => new SelectionGallery(), Navigation),
GalleryBuilder.NavButton("Propagation Galleries", () => new PropagationGallery(), Navigation),
GalleryBuilder.NavButton("Grouping Galleries", () => new GroupingGallery(), Navigation),
GalleryBuilder.NavButton("Item Size Galleries", () => new ItemsSizeGallery(), Navigation),
GalleryBuilder.NavButton("Scroll Mode Galleries", () => new ScrollModeGallery(), Navigation),
GalleryBuilder.NavButton("Alternate Layout Galleries", () => new AlternateLayoutGallery(), Navigation),
GalleryBuilder.NavButton("Header/Footer Galleries", () => new HeaderFooterGallery(), Navigation),
GalleryBuilder.NavButton("Nested CollectionViews", () => new NestedGalleries.NestedCollectionViewGallery(), Navigation),
Content = new StackLayout
{
Children =
{
new Button { Text ="Enable CollectionView", AutomationId = "EnableCollectionView", Command = new Command(() => Device.SetFlags(new[] { ExperimentalFlags.CollectionViewExperimental })) },
GalleryBuilder.NavButton("Default Text Galleries", () => new DefaultTextGallery(), Navigation),
GalleryBuilder.NavButton("DataTemplate Galleries", () => new DataTemplateGallery(), Navigation),
GalleryBuilder.NavButton("Observable Collection Galleries", () => new ObservableCollectionGallery(), Navigation),
GalleryBuilder.NavButton("Snap Points Galleries", () => new SnapPointsGallery(), Navigation),
GalleryBuilder.NavButton("ScrollTo Galleries", () => new ScrollToGallery(), Navigation),
GalleryBuilder.NavButton("CarouselView Galleries", () => new CarouselViewGallery(), Navigation),
GalleryBuilder.NavButton("EmptyView Galleries", () => new EmptyViewGallery(), Navigation),
GalleryBuilder.NavButton("Selection Galleries", () => new SelectionGallery(), Navigation),
GalleryBuilder.NavButton("Propagation Galleries", () => new PropagationGallery(), Navigation),
GalleryBuilder.NavButton("Grouping Galleries", () => new GroupingGallery(), Navigation),
GalleryBuilder.NavButton("Item Size Galleries", () => new ItemsSizeGallery(), Navigation),
GalleryBuilder.NavButton("Scroll Mode Galleries", () => new ScrollModeGallery(), Navigation),
GalleryBuilder.NavButton("Alternate Layout Galleries", () => new AlternateLayoutGallery(), Navigation),
GalleryBuilder.NavButton("Header/Footer Galleries", () => new HeaderFooterGallery(), Navigation),
GalleryBuilder.NavButton("Nested CollectionViews", () => new NestedGalleries.NestedCollectionViewGallery(), Navigation),
}
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,14 @@ void CheckForEmptySource()
{
UpdateEmptyViewVisibility(_isEmpty);
}

if (wasEmpty && !_isEmpty)
{
// If we're going from empty to having stuff, it's possible that we've never actually measured
// a prototype cell and our itemSize or estimatedItemSize are wrong/unset
// So trigger a constraint update; if we need a measurement, that will make it happen
ItemsViewLayout.ConstrainTo(CollectionView.Bounds.Size);
}
}

public override void ViewDidLoad()
Expand Down
22 changes: 13 additions & 9 deletions Xamarin.Forms.Platform.iOS/CollectionView/ObservableItemsSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,17 +135,21 @@ void Add(NotifyCollectionChangedEventArgs args)
var startIndex = args.NewStartingIndex > -1 ? args.NewStartingIndex : _itemsSource.IndexOf(args.NewItems[0]);
var count = args.NewItems.Count;

_collectionView.PerformBatchUpdates(() =>
if (!_grouped && _collectionView.NumberOfSections() != GroupCount && count > 0)
{
if (!_grouped && _collectionView.NumberOfSections() != GroupCount)
// Okay, we're going from completely empty to more than 0 items; this means we don't even
// have a section 0 yet. Inserting a section 0 manually results in an unexplained crash, so instead
// we'll just reload the data so the UICollectionView can get its internal state sorted out.
_collectionView.ReloadData();
}
else
{
_collectionView.PerformBatchUpdates(() =>
{
// We had an empty non-grouped list, and now we're trying to add an item;
// we need to give it a section as well
_collectionView.InsertSections(new NSIndexSet(0));
}

_collectionView.InsertItems(CreateIndexesFrom(startIndex, count));
}, null);
var indexes = CreateIndexesFrom(startIndex, count);
_collectionView.InsertItems(indexes);
}, null);
}
}

void Remove(NotifyCollectionChangedEventArgs args)
Expand Down

0 comments on commit ed57fe3

Please sign in to comment.