Skip to content

Commit

Permalink
Expander CollectionView GridLayout 1557 (#1567)
Browse files Browse the repository at this point in the history
* Expander CollectionView GridLayout 1557

* Update expander macos

* Fix sample, remove expander macios

* Add more contributors

* Fix expander ios list view

* update sample

---------

Co-authored-by: Brandon Minnick <13558917+brminnick@users.noreply.github.com>
Co-authored-by: Victor Hugo Garcia Hernandez <vhugogarcia@users.noreply.github.com>
Co-authored-by: James Crutchley <ne0rmatrix@gmail.com>
Co-authored-by: Brandon Minnick <13558917+TheCodeTraveler@users.noreply.github.com>
  • Loading branch information
5 people authored Feb 5, 2025
1 parent 2c3b6a0 commit a6c11a2
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
</ListView.ItemTemplate>
</ListView>

<Label Text="Expander in CollectionView" FontSize="24" FontAttributes="Bold"/>
<Label Text="Expander in CollectionView with LinearItemsLayout" FontSize="24" FontAttributes="Bold"/>

<CollectionView ItemsSource="{Binding ContentCreators}">
<CollectionView.ItemTemplate>
Expand All @@ -96,6 +96,35 @@
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>

<Label Text="Expander in CollectionView with GridItemsLayout" FontSize="24" FontAttributes="Bold"/>

<CollectionView ItemsSource="{Binding ContentCreators}">
<CollectionView.ItemsLayout>
<GridItemsLayout Orientation="Vertical"
Span="4"
HorizontalItemSpacing="5"
VerticalItemSpacing="5" />
</CollectionView.ItemsLayout>
<CollectionView.ItemTemplate>
<DataTemplate>
<mct:Expander x:DataType="sample:ContentCreator"
ExpandedChanged="Expander_ExpandedChanged">
<mct:Expander.Header>
<Label Text="{Binding Name}"/>
</mct:Expander.Header>
<mct:Expander.Content>
<VerticalStackLayout>
<Label Text="{Binding Resource}" HorizontalOptions="Center"/>
<Image Source="{Binding Image}"
WidthRequest="100"
HeightRequest="100"/>
</VerticalStackLayout>
</mct:Expander.Content>
</mct:Expander>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</VerticalStackLayout>
</ScrollView>
</pages:BasePage>
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ public static IEnumerable<ContentCreator> GetContentCreators() =>
new("Kym Phillpotts", "https://kymphillpotts.com", "https://avatars.githubusercontent.com/u/1327346"),
new("Pedro Jesus", "https://github.com/pictos", "https://avatars.githubusercontent.com/u/20712372"),
new("Shaun Lawrence", "https://github.com/bijington", "https://avatars.githubusercontent.com/u/17139988"),
new("Vladislav Antonyuk", "https://vladislavantonyuk.azurewebsites.net", "https://avatars.githubusercontent.com/u/33021114"),
new("Vladislav Antonyuk", "https://vladislavantonyuk.github.io", "https://avatars.githubusercontent.com/u/33021114"),
new("Víctor Hugo García Hernández", "", "https://avatars.githubusercontent.com/u/1047398"),
new("James Crutchley", "", "https://avatars.githubusercontent.com/u/4167863"),
new("Clifford Agius", "https://cliffordagius.co.uk/", "https://avatars.githubusercontent.com/u/5613809"),
];
}
}
54 changes: 0 additions & 54 deletions src/CommunityToolkit.Maui/Views/Expander/Expander.macios.cs

This file was deleted.

24 changes: 11 additions & 13 deletions src/CommunityToolkit.Maui/Views/Expander/Expander.shared.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,33 +169,31 @@ void ResizeExpanderInItemsView(TappedEventArgs tappedEventArgs)
}

Element element = this;
#if WINDOWS
var size = IsExpanded
? Measure(double.PositiveInfinity, double.PositiveInfinity)
: Header.Measure(double.PositiveInfinity, double.PositiveInfinity);

#endif
while (element is not null)
{
if (element.Parent is ListView && element is Cell cell)
{
#if IOS || MACCATALYST
throw new NotSupportedException($"{nameof(Expander)} is not yet supported in {nameof(ListView)}");
#else
cell.ForceUpdateSize();
if (element is ListView listView)
{
(listView.Handler?.PlatformView as UIKit.UITableView)?.ReloadData();
}
#endif

#if WINDOWS
if (element.Parent is ListView listView && element is Cell cell)
{
cell.ForceUpdateSize();
}
#if IOS || MACCATALYST || WINDOWS
else if (element is CollectionView collectionView)
{
var tapLocation = tappedEventArgs.GetPosition(collectionView);
ForceUpdateCellSize(collectionView, size, tapLocation);
}
#endif
#if IOS || MACCATALYST
else if (element is ScrollView scrollView)
{
((IView)scrollView).InvalidateMeasure();
}
#endif

element = element.Parent;
}
Expand Down
44 changes: 43 additions & 1 deletion src/CommunityToolkit.Maui/Views/Expander/Expander.windows.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using CommunityToolkit.Maui.Core.Extensions;
using Microsoft.Maui.Controls.Platform;
using Microsoft.UI.Xaml.Controls;

Expand Down Expand Up @@ -30,5 +29,48 @@ static void ForceUpdateCellSize(CollectionView collectionView, Size size, Point?
}
}
}
else if (collectionView.Handler?.PlatformView is FormsGridView gridView)
{
var numberOfColumns = gridView.Span;
if (numberOfColumns == 0)
{
return;
}

for (var i = 0; i < gridView.Items.Count; i++)
{
if (gridView.ContainerFromIndex(i) is GridViewItem gridViewItem)
{
var itemTransform = gridViewItem.TransformToVisual(gridView);
var itemPosition = itemTransform.TransformPoint(new Windows.Foundation.Point(0, 0));
var itemBounds = new Rect(itemPosition.X, itemPosition.Y, gridViewItem.ActualWidth, gridViewItem.ActualHeight);

if (itemBounds.Contains(tapLocation.Value))
{
IterateItemsInRow(gridView, i, numberOfColumns, size.Height);
break;
}
}
}
}
}

static void IterateItemsInRow(ItemsControl gridView, int itemIndex, int totalColumns, double height)
{
var rowToIterate = itemIndex / totalColumns;
var startIndex = rowToIterate * totalColumns;

for (var i = startIndex; i < startIndex + totalColumns; i++)
{
if (i >= gridView.Items.Count)
{
break;
}

if (gridView.ContainerFromIndex(i) is GridViewItem cell)
{
cell.Height = height + Random.Shared.NextDouble();
}
}
}
}

0 comments on commit a6c11a2

Please sign in to comment.