Skip to content

Commit a575ab7

Browse files
Fix CarouselView ItemsLayout Runtime Updates (#29447)
* Fixed the issue for dynamic changes of ItemsLayout property * Simplified the fix * Removed unwanted lines * Modified the fix * Added null check * Added a test case and snapshots * Modified a test case * Modified the public api to internal
1 parent c60db11 commit a575ab7

File tree

7 files changed

+115
-2
lines changed

7 files changed

+115
-2
lines changed

src/Controls/src/Core/Handlers/Items/CarouselViewHandler.Android.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,15 @@ public static void MapCurrentItem(CarouselViewHandler handler, CarouselView caro
5353
(handler.PlatformView as IMauiCarouselRecyclerView).UpdateFromCurrentItem();
5454
}
5555

56+
// TODO: Change the modifier to public in .NET 10.
57+
internal static void MapItemsLayout(CarouselViewHandler handler, CarouselView carouselView)
58+
{
59+
if (handler.PlatformView is IMauiRecyclerView<CarouselView> recyclerView)
60+
{
61+
recyclerView.UpdateLayoutManager();
62+
}
63+
}
64+
5665
public override Size GetDesiredSize(double widthConstraint, double heightConstraint)
5766
{
5867
_widthConstraint = widthConstraint;

src/Controls/src/Core/Handlers/Items/CarouselViewHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public CarouselViewHandler(PropertyMapper mapper = null) : base(mapper ?? Mapper
1414

1515
public static PropertyMapper<CarouselView, CarouselViewHandler> Mapper = new(ItemsViewMapper)
1616
{
17-
#if TIZEN
17+
#if TIZEN || ANDROID
1818
[Controls.CarouselView.ItemsLayoutProperty.PropertyName] = MapItemsLayout,
1919
#endif
2020
[Controls.CarouselView.IsSwipeEnabledProperty.PropertyName] = MapIsSwipeEnabled,

src/Controls/src/Core/Handlers/Items2/CarouselViewHandler2.iOS.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ public CarouselViewHandler2(PropertyMapper mapper = null) : base(mapper ?? Mappe
2727
[Controls.CarouselView.PeekAreaInsetsProperty.PropertyName] = MapPeekAreaInsets,
2828
[Controls.CarouselView.IsBounceEnabledProperty.PropertyName] = MapIsBounceEnabled,
2929
[Controls.CarouselView.PositionProperty.PropertyName] = MapPosition,
30-
[Controls.CarouselView.CurrentItemProperty.PropertyName] = MapCurrentItem
30+
[Controls.CarouselView.CurrentItemProperty.PropertyName] = MapCurrentItem,
31+
[Controls.CarouselView.ItemsLayoutProperty.PropertyName] = MapItemsLayout,
3132
};
3233
}
3334

@@ -190,6 +191,12 @@ public static void MapIsBounceEnabled(CarouselViewHandler2 handler, CarouselView
190191
handler.Controller.CollectionView.Bounces = carouselView.IsBounceEnabled;
191192
}
192193

194+
// TODO: Change the modifier to public in .NET 10.
195+
internal static void MapItemsLayout(CarouselViewHandler2 handler, CarouselView carouselView)
196+
{
197+
handler?.UpdateLayout();
198+
}
199+
193200
public static void MapPeekAreaInsets(CarouselViewHandler2 handler, CarouselView carouselView)
194201
{
195202
handler.UpdateLayout();
35.3 KB
Loading
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
using System.Collections.ObjectModel;
2+
3+
namespace Maui.Controls.Sample.Issues;
4+
[Issue(IssueTracker.Github, 29372, "CarouselView ItemsLayout Not Updating at Runtime", PlatformAffected.All)]
5+
public partial class Issue29372 : ContentPage
6+
{
7+
public Issue29372()
8+
{
9+
var verticalStackLayout = new VerticalStackLayout();
10+
var carouselItems = new ObservableCollection<string>
11+
{
12+
"Item 0",
13+
"Item 1",
14+
"Item 2",
15+
"Item 3",
16+
"Item 4",
17+
};
18+
19+
CarouselView2 carouselView = new CarouselView2
20+
{
21+
ItemsSource = carouselItems,
22+
AutomationId = "carouselview",
23+
ItemsLayout = new LinearItemsLayout(ItemsLayoutOrientation.Horizontal),
24+
PeekAreaInsets = new Thickness(0, 100),
25+
HeightRequest = 300,
26+
Loop = false,
27+
ItemTemplate = new DataTemplate(() =>
28+
{
29+
var grid = new Grid
30+
{
31+
Padding = 10
32+
};
33+
34+
var label = new Label
35+
{
36+
VerticalOptions = LayoutOptions.Center,
37+
HorizontalOptions = LayoutOptions.Center,
38+
FontSize = 18,
39+
};
40+
label.SetBinding(Label.TextProperty, ".");
41+
label.SetBinding(Label.AutomationIdProperty, ".");
42+
43+
grid.Children.Add(label);
44+
return grid;
45+
}),
46+
HorizontalOptions = LayoutOptions.Fill,
47+
};
48+
49+
var button = new Button
50+
{
51+
Text = "Change Items Layout",
52+
AutomationId = "ChangeItemsLayoutButton",
53+
Margin = new Thickness(20),
54+
};
55+
56+
var label = new Label
57+
{
58+
Text = "The test is passed if the items are displayed in a vertical orientation.",
59+
HorizontalOptions = LayoutOptions.Center,
60+
Padding = new Thickness(20),
61+
};
62+
63+
button.Clicked += (sender, e) =>
64+
{
65+
carouselView.ItemsLayout = new LinearItemsLayout(ItemsLayoutOrientation.Vertical);
66+
};
67+
68+
verticalStackLayout.Children.Add(label);
69+
verticalStackLayout.Children.Add(carouselView);
70+
verticalStackLayout.Children.Add(button);
71+
Content = verticalStackLayout;
72+
}
73+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#if TEST_FAILS_ON_WINDOWS // Related issue for windows: https://github.com/dotnet/maui/issues/29445
2+
using NUnit.Framework;
3+
using UITest.Appium;
4+
using UITest.Core;
5+
6+
namespace Microsoft.Maui.TestCases.Tests.Issues;
7+
public class Issue29372 : _IssuesUITest
8+
{
9+
public override string Issue => "CarouselView ItemsLayout Not Updating at Runtime";
10+
11+
public Issue29372(TestDevice device)
12+
: base(device)
13+
{ }
14+
15+
[Test]
16+
[Category(UITestCategories.CarouselView)]
17+
public void VerifyCarouselLayoutOrientationChange()
18+
{
19+
App.WaitForElement("carouselview");
20+
App.Tap("ChangeItemsLayoutButton");
21+
VerifyScreenshot();
22+
}
23+
}
24+
#endif
46.4 KB
Loading

0 commit comments

Comments
 (0)