Skip to content

Commit

Permalink
Update StaggeredItemsViewLayout
Browse files Browse the repository at this point in the history
  • Loading branch information
VladislavAntonyuk committed May 6, 2024
1 parent 105daa4 commit af4cdb7
Show file tree
Hide file tree
Showing 9 changed files with 190 additions and 370 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@

<PropertyGroup Condition="$([MSBuild]::IsOSPlatform('windows')) == 'false' and $([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'ios' and $(Configuration) == 'Debug'">
<RuntimeIdentifier>ios-arm64</RuntimeIdentifier>
<RuntimeIdentifier>iossimulator-x64</RuntimeIdentifier>
<RuntimeIdentifier>iossimulator-arm64</RuntimeIdentifier>
<CreatePackage>false</CreatePackage>
</PropertyGroup>

Expand Down
2 changes: 1 addition & 1 deletion MauiStaggeredCollectionView/AppShell.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
Title="MauiStaggeredCollectionView">

<ShellContent
Title=".NET MAUI Staggeled CollectionView"
Title=".NET MAUI Staggered CollectionView"
ContentTemplate="{DataTemplate local:MainPage}"
Route="MainPage" />

Expand Down
8 changes: 4 additions & 4 deletions MauiStaggeredCollectionView/MainPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ private static void MapItemsLayout(StructuredItemsViewHandler<CollectionView> ha
platformView?.UpdateAdapter();
platformView?.SetLayoutManager(
new AndroidX.RecyclerView.Widget.StaggeredGridLayoutManager(
staggeredItemsLayout.Span,
staggeredItemsLayout.Span,
staggeredItemsLayout.Orientation == ItemsLayoutOrientation.Horizontal ? AndroidX.RecyclerView.Widget.StaggeredGridLayoutManager.Horizontal : AndroidX.RecyclerView.Widget.StaggeredGridLayoutManager.Vertical));
break;
default:
Expand All @@ -68,7 +68,7 @@ protected override ItemsViewLayout SelectLayout()

if (itemsLayout is StaggeredItemsLayout staggeredItemsLayout)
{
return new StagLayout([(1d, 1d), (1d, 1d)], 0, staggeredItemsLayout, ItemSizingStrategy.MeasureAllItems);
return new StagerredItemsViewLayout(staggeredItemsLayout, ItemsView.ItemSizingStrategy);
}

return base.SelectLayout();
Expand All @@ -90,10 +90,10 @@ protected override Microsoft.UI.Xaml.Controls.ListViewBase SelectListViewBase()
public class StaggeredItemsLayout(ItemsLayoutOrientation orientation) : ItemsLayout(orientation)
{
public static readonly BindableProperty SpanProperty = BindableProperty.Create(nameof(Span), typeof(int), typeof(StaggeredItemsLayout), default(int));

public StaggeredItemsLayout() : this(ItemsLayoutOrientation.Vertical)
{

}

public int Span
Expand Down
54 changes: 0 additions & 54 deletions MauiStaggeredCollectionView/Platforms/MacCatalyst/StagLayout.cs

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
namespace MauiStaggeredCollectionView;

using CoreGraphics;
using Foundation;
using Microsoft.Maui.Controls.Handlers.Items;
using UIKit;

public class StagerredItemsViewLayout(StaggeredItemsLayout itemsLayout, ItemSizingStrategy sizingStrategy)
: ItemsViewLayout(itemsLayout, sizingStrategy)
{
private readonly List<UICollectionViewLayoutAttributes> cache = new();

private float contentHeight;

public int ColumnCount { get; set; } = itemsLayout.Span;

public double MinCellHeight { get; set; } = 150;

public double MaxCellHeight { get; set; } = 250;

public override CGSize CollectionViewContentSize => new(CollectionView.Frame.Width, contentHeight);

public override void ConstrainTo(CGSize size)
{
ConstrainedDimension = ScrollDirection == UICollectionViewScrollDirection.Vertical ? size.Width : size.Height;
DetermineCellSize();
}

protected void OnItemsSourcePropertyChanged()
{
cache.Clear();
}

public override void PrepareLayout()
{
if (cache.Count > 0)
{
return;
}

contentHeight = 0;
var columnWidth = (float)CollectionView.Frame.Width / ColumnCount;
var xOffsets = new List<float>();
var yOffsets = new List<float>();
for (var i = 0; i < ColumnCount; i++)
{
xOffsets.Add(i * columnWidth);
yOffsets.Add(0);
}

var column = 0;

var itemsCount = CollectionView.NumberOfItemsInSection(0).ToInt32();
for (var i = 0; i < itemsCount; i++)
{
var indexPath = NSIndexPath.FromRowSection(i, 0);

var cellHeight = new Random().Next((int)MinCellHeight, (int)MaxCellHeight);

var frame = new CGRect(xOffsets[column], yOffsets[column], columnWidth, cellHeight);

var attrs = UICollectionViewLayoutAttributes.CreateForCell(indexPath);
attrs.Frame = frame;
cache.Add(attrs);

contentHeight = Math.Max(contentHeight, (float)frame.GetMaxY());
yOffsets[column] += cellHeight;

column = column == ColumnCount - 1 ? 0 : column + 1;
}
}

public override UICollectionViewLayoutAttributes[] LayoutAttributesForElementsInRect(CGRect rect)
{
var visibleLayoutAttributes = new List<UICollectionViewLayoutAttributes>();

foreach (var attr in cache)
{
if (attr.Frame.IntersectsWith(rect))
{
visibleLayoutAttributes.Add(attr);
}
}

return visibleLayoutAttributes.ToArray();
}

public override UICollectionViewLayoutAttributes LayoutAttributesForItem(NSIndexPath indexPath)
{
return cache[indexPath.Row];
}
}
54 changes: 0 additions & 54 deletions MauiStaggeredCollectionView/Platforms/iOS/StagLayout.cs

This file was deleted.

Loading

0 comments on commit af4cdb7

Please sign in to comment.