Skip to content

Commit 31e02ca

Browse files
jsuarezruizhartezGitHub Actions Autoformatter
authored andcommitted
[Android] Fix CollectionView EmptyView (#11763)
* Fix Android CollectionView EmptyView * Update src/Controls/src/Core/Handlers/Items/ItemsViewHandler.Android.cs Co-authored-by: E.Z. Hart <hartez@users.noreply.github.com> * Fixed build errors * Auto-format source code --------- Co-authored-by: E.Z. Hart <hartez@users.noreply.github.com> Co-authored-by: GitHub Actions Autoformatter <autoformat@example.com>
1 parent 69d03c6 commit 31e02ca

3 files changed

Lines changed: 60 additions & 7 deletions

File tree

src/Controls/src/Core/Handlers/Items/Android/Adapters/EmptyViewAdapter.cs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ public DataTemplate EmptyViewTemplate
9696
protected readonly ItemsView ItemsView;
9797
public override int ItemCount => 1 + ((Header != null || HeaderTemplate != null) ? 1 : 0) + ((Footer != null || FooterTemplate != null) ? 1 : 0);
9898

99+
internal double RecyclerViewHeight { get; set; }
100+
101+
internal double RecyclerViewWidth { get; set; }
102+
99103
public EmptyViewAdapter(ItemsView itemsView)
100104
{
101105
ItemsView = itemsView;
@@ -228,7 +232,7 @@ protected RecyclerView.ViewHolder CreateEmptyViewHolder(object content, DataTemp
228232

229233
if (template == null)
230234
{
231-
if (!(content is View formsView))
235+
if (content is not View formsView)
232236
{
233237
// No template, EmptyView is not a Forms View, so just display EmptyView.ToString
234238
return SimpleViewHolder.FromText(content?.ToString(), context);
@@ -273,15 +277,23 @@ bool IsEmpty(int position)
273277

274278
int GetHeight(ViewGroup parent)
275279
{
276-
//this was using parent.MeasuredHeight on XF but that reports 0 now.
280+
int height = (int)RecyclerViewHeight;
281+
282+
if (height <= 0)
283+
height = parent.MeasuredHeight;
284+
277285
var headerFooterHeight = parent.Context.ToPixels(_headerHeight + _footerHeight);
278-
return Math.Abs((int)(parent.Height - headerFooterHeight));
286+
return Math.Abs((int)(height - headerFooterHeight));
279287
}
280288

281289
int GetWidth(ViewGroup parent)
282290
{
283-
//this was using MeasuredWidth on XF but that reports 0 now.
284-
return parent.Width;
291+
int width = (int)RecyclerViewWidth;
292+
293+
if (width <= 0)
294+
width = parent.MeasuredWidth;
295+
296+
return width;
285297
}
286298

287299
void UpdateHeaderFooterHeight(object item, bool isHeader)

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

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
#nullable disable
1+
#nullable disable
22
using System;
33
using System.Collections.Generic;
44
using System.Text;
55
using AndroidX.RecyclerView.Widget;
6+
using Microsoft.Maui.Graphics;
67
using Microsoft.Maui.Handlers;
78

89
namespace Microsoft.Maui.Controls.Handlers.Items
@@ -12,6 +13,7 @@ public abstract partial class ItemsViewHandler<TItemsView> : ViewHandler<TItemsV
1213
protected ItemsViewHandler(PropertyMapper mapper, CommandMapper commandMapper = null) : base(mapper, commandMapper)
1314
{
1415
}
16+
1517
protected abstract IItemsLayout GetItemsLayout();
1618

1719
protected virtual ItemsViewAdapter<TItemsView, IItemsViewSource> CreateAdapter() => new(VirtualView);
@@ -31,6 +33,32 @@ protected override void DisconnectHandler(RecyclerView platformView)
3133
protected override RecyclerView CreatePlatformView() =>
3234
new MauiRecyclerView<TItemsView, ItemsViewAdapter<TItemsView, IItemsViewSource>, IItemsViewSource>(Context, GetItemsLayout, CreateAdapter);
3335

36+
public override Size GetDesiredSize(double widthConstraint, double heightConstraint)
37+
{
38+
var width = widthConstraint;
39+
var height = heightConstraint;
40+
41+
if (!double.IsInfinity(width))
42+
width = Context.ToPixels(width);
43+
44+
if (!double.IsInfinity(height))
45+
height = Context.ToPixels(height);
46+
47+
UpdateEmptyViewSize(width, height);
48+
49+
return base.GetDesiredSize(widthConstraint, heightConstraint);
50+
}
51+
52+
public override void PlatformArrange(Rect frame)
53+
{
54+
var width = Context.ToPixels(frame.Width);
55+
var height = Context.ToPixels(frame.Height);
56+
57+
UpdateEmptyViewSize(width, height);
58+
59+
base.PlatformArrange(frame);
60+
}
61+
3462
public static void MapItemsSource(ItemsViewHandler<TItemsView> handler, ItemsView itemsView)
3563
{
3664
(handler.PlatformView as IMauiRecyclerView<TItemsView>)?.UpdateItemsSource();
@@ -74,5 +102,16 @@ public static void MapItemsUpdatingScrollMode(ItemsViewHandler<TItemsView> handl
74102
{
75103
(handler.PlatformView as IMauiRecyclerView<TItemsView>)?.UpdateScrollingMode();
76104
}
105+
106+
void UpdateEmptyViewSize(double width, double height)
107+
{
108+
var adapter = PlatformView.GetAdapter();
109+
110+
if (adapter is EmptyViewAdapter emptyViewAdapter)
111+
{
112+
emptyViewAdapter.RecyclerViewWidth = width;
113+
emptyViewAdapter.RecyclerViewHeight = height;
114+
}
115+
}
77116
}
78-
}
117+
}

src/Controls/src/Core/PublicAPI/net-android/PublicAPI.Unshipped.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ Microsoft.Maui.Controls.Border.~Border() -> void
44
Microsoft.Maui.Controls.Handlers.Compatibility.FrameRenderer.FrameRenderer(Android.Content.Context! context, Microsoft.Maui.IPropertyMapper! mapper) -> void
55
Microsoft.Maui.Controls.Handlers.Compatibility.FrameRenderer.FrameRenderer(Android.Content.Context! context, Microsoft.Maui.IPropertyMapper! mapper, Microsoft.Maui.CommandMapper! commandMapper) -> void
66
Microsoft.Maui.Controls.Handlers.Items.MauiRecyclerView<TItemsView, TAdapter, TItemsViewSource>.~MauiRecyclerView() -> void
7+
override Microsoft.Maui.Controls.Handlers.Items.ItemsViewHandler<TItemsView>.GetDesiredSize(double widthConstraint, double heightConstraint) -> Microsoft.Maui.Graphics.Size
8+
override Microsoft.Maui.Controls.Handlers.Items.ItemsViewHandler<TItemsView>.PlatformArrange(Microsoft.Maui.Graphics.Rect frame) -> void
79
Microsoft.Maui.Controls.LayoutOptions.Equals(Microsoft.Maui.Controls.LayoutOptions other) -> bool
810
Microsoft.Maui.Controls.Platform.ShapesExtensions
911
Microsoft.Maui.Controls.Region.Equals(Microsoft.Maui.Controls.Region other) -> bool

0 commit comments

Comments
 (0)