Skip to content

Commit a6c11a2

Browse files
VladislavAntonyukTheCodeTravelervhugogarciane0rrmatrix
authored
Expander CollectionView GridLayout 1557 (#1567)
* 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>
1 parent 2c3b6a0 commit a6c11a2

File tree

5 files changed

+88
-70
lines changed

5 files changed

+88
-70
lines changed

samples/CommunityToolkit.Maui.Sample/Pages/Views/Expander/ExpanderPage.xaml

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
</ListView.ItemTemplate>
7575
</ListView>
7676

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

7979
<CollectionView ItemsSource="{Binding ContentCreators}">
8080
<CollectionView.ItemTemplate>
@@ -96,6 +96,35 @@
9696
</DataTemplate>
9797
</CollectionView.ItemTemplate>
9898
</CollectionView>
99+
100+
<Label Text="Expander in CollectionView with GridItemsLayout" FontSize="24" FontAttributes="Bold"/>
101+
102+
<CollectionView ItemsSource="{Binding ContentCreators}">
103+
<CollectionView.ItemsLayout>
104+
<GridItemsLayout Orientation="Vertical"
105+
Span="4"
106+
HorizontalItemSpacing="5"
107+
VerticalItemSpacing="5" />
108+
</CollectionView.ItemsLayout>
109+
<CollectionView.ItemTemplate>
110+
<DataTemplate>
111+
<mct:Expander x:DataType="sample:ContentCreator"
112+
ExpandedChanged="Expander_ExpandedChanged">
113+
<mct:Expander.Header>
114+
<Label Text="{Binding Name}"/>
115+
</mct:Expander.Header>
116+
<mct:Expander.Content>
117+
<VerticalStackLayout>
118+
<Label Text="{Binding Resource}" HorizontalOptions="Center"/>
119+
<Image Source="{Binding Image}"
120+
WidthRequest="100"
121+
HeightRequest="100"/>
122+
</VerticalStackLayout>
123+
</mct:Expander.Content>
124+
</mct:Expander>
125+
</DataTemplate>
126+
</CollectionView.ItemTemplate>
127+
</CollectionView>
99128
</VerticalStackLayout>
100129
</ScrollView>
101130
</pages:BasePage>

samples/CommunityToolkit.Maui.Sample/ViewModels/Views/ExpanderViewModel.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ public static IEnumerable<ContentCreator> GetContentCreators() =>
2020
new("Kym Phillpotts", "https://kymphillpotts.com", "https://avatars.githubusercontent.com/u/1327346"),
2121
new("Pedro Jesus", "https://github.com/pictos", "https://avatars.githubusercontent.com/u/20712372"),
2222
new("Shaun Lawrence", "https://github.com/bijington", "https://avatars.githubusercontent.com/u/17139988"),
23-
new("Vladislav Antonyuk", "https://vladislavantonyuk.azurewebsites.net", "https://avatars.githubusercontent.com/u/33021114"),
23+
new("Vladislav Antonyuk", "https://vladislavantonyuk.github.io", "https://avatars.githubusercontent.com/u/33021114"),
24+
new("Víctor Hugo García Hernández", "", "https://avatars.githubusercontent.com/u/1047398"),
25+
new("James Crutchley", "", "https://avatars.githubusercontent.com/u/4167863"),
26+
new("Clifford Agius", "https://cliffordagius.co.uk/", "https://avatars.githubusercontent.com/u/5613809"),
2427
];
2528
}
2629
}

src/CommunityToolkit.Maui/Views/Expander/Expander.macios.cs

Lines changed: 0 additions & 54 deletions
This file was deleted.

src/CommunityToolkit.Maui/Views/Expander/Expander.shared.cs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -169,33 +169,31 @@ void ResizeExpanderInItemsView(TappedEventArgs tappedEventArgs)
169169
}
170170

171171
Element element = this;
172+
#if WINDOWS
172173
var size = IsExpanded
173174
? Measure(double.PositiveInfinity, double.PositiveInfinity)
174175
: Header.Measure(double.PositiveInfinity, double.PositiveInfinity);
175-
176+
#endif
176177
while (element is not null)
177178
{
178-
if (element.Parent is ListView && element is Cell cell)
179-
{
180179
#if IOS || MACCATALYST
181-
throw new NotSupportedException($"{nameof(Expander)} is not yet supported in {nameof(ListView)}");
182-
#else
183-
cell.ForceUpdateSize();
180+
if (element is ListView listView)
181+
{
182+
(listView.Handler?.PlatformView as UIKit.UITableView)?.ReloadData();
183+
}
184184
#endif
185+
186+
#if WINDOWS
187+
if (element.Parent is ListView listView && element is Cell cell)
188+
{
189+
cell.ForceUpdateSize();
185190
}
186-
#if IOS || MACCATALYST || WINDOWS
187191
else if (element is CollectionView collectionView)
188192
{
189193
var tapLocation = tappedEventArgs.GetPosition(collectionView);
190194
ForceUpdateCellSize(collectionView, size, tapLocation);
191195
}
192196
#endif
193-
#if IOS || MACCATALYST
194-
else if (element is ScrollView scrollView)
195-
{
196-
((IView)scrollView).InvalidateMeasure();
197-
}
198-
#endif
199197

200198
element = element.Parent;
201199
}

src/CommunityToolkit.Maui/Views/Expander/Expander.windows.cs

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using CommunityToolkit.Maui.Core.Extensions;
21
using Microsoft.Maui.Controls.Platform;
32
using Microsoft.UI.Xaml.Controls;
43

@@ -30,5 +29,48 @@ static void ForceUpdateCellSize(CollectionView collectionView, Size size, Point?
3029
}
3130
}
3231
}
32+
else if (collectionView.Handler?.PlatformView is FormsGridView gridView)
33+
{
34+
var numberOfColumns = gridView.Span;
35+
if (numberOfColumns == 0)
36+
{
37+
return;
38+
}
39+
40+
for (var i = 0; i < gridView.Items.Count; i++)
41+
{
42+
if (gridView.ContainerFromIndex(i) is GridViewItem gridViewItem)
43+
{
44+
var itemTransform = gridViewItem.TransformToVisual(gridView);
45+
var itemPosition = itemTransform.TransformPoint(new Windows.Foundation.Point(0, 0));
46+
var itemBounds = new Rect(itemPosition.X, itemPosition.Y, gridViewItem.ActualWidth, gridViewItem.ActualHeight);
47+
48+
if (itemBounds.Contains(tapLocation.Value))
49+
{
50+
IterateItemsInRow(gridView, i, numberOfColumns, size.Height);
51+
break;
52+
}
53+
}
54+
}
55+
}
56+
}
57+
58+
static void IterateItemsInRow(ItemsControl gridView, int itemIndex, int totalColumns, double height)
59+
{
60+
var rowToIterate = itemIndex / totalColumns;
61+
var startIndex = rowToIterate * totalColumns;
62+
63+
for (var i = startIndex; i < startIndex + totalColumns; i++)
64+
{
65+
if (i >= gridView.Items.Count)
66+
{
67+
break;
68+
}
69+
70+
if (gridView.ContainerFromIndex(i) is GridViewItem cell)
71+
{
72+
cell.Height = height + Random.Shared.NextDouble();
73+
}
74+
}
3375
}
3476
}

0 commit comments

Comments
 (0)