From b212691a92fb33f670bded739b931a6a2aa3a730 Mon Sep 17 00:00:00 2001 From: Vignesh-SF3580 Date: Thu, 19 Sep 2024 18:09:03 +0530 Subject: [PATCH 1/8] Fixed: 11896 - CollectionView Header/Footer/EmptyView issues when adding/removing items --- .../Items/Android/Adapters/AdapterNotifier.cs | 16 ---------- .../Items/Android/MauiRecyclerView.cs | 2 ++ .../Items/ItemsViewHandler.Android.cs | 32 ++++++++++++++++++- 3 files changed, 33 insertions(+), 17 deletions(-) diff --git a/src/Controls/src/Core/Handlers/Items/Android/Adapters/AdapterNotifier.cs b/src/Controls/src/Core/Handlers/Items/Android/Adapters/AdapterNotifier.cs index 6ed445bd5e3e..192d10afbb4e 100644 --- a/src/Controls/src/Core/Handlers/Items/Android/Adapters/AdapterNotifier.cs +++ b/src/Controls/src/Core/Handlers/Items/Android/Adapters/AdapterNotifier.cs @@ -29,9 +29,6 @@ public void NotifyItemInserted(IItemsViewSource source, int startIndex) if (IsValidAdapter()) { _adapter.NotifyItemInserted(startIndex); - - var changedCount = _adapter.ItemCount - startIndex; - _adapter.NotifyItemRangeChanged(startIndex, changedCount); } } @@ -40,10 +37,6 @@ public void NotifyItemMoved(IItemsViewSource source, int fromPosition, int toPos if (IsValidAdapter()) { _adapter.NotifyItemMoved(fromPosition, toPosition); - - var minPosition = System.Math.Min(fromPosition, toPosition); - var changedCount = _adapter.ItemCount - minPosition; - _adapter.NotifyItemRangeChanged(minPosition, changedCount); } } @@ -58,9 +51,6 @@ public void NotifyItemRangeInserted(IItemsViewSource source, int startIndex, int if (IsValidAdapter()) { _adapter.NotifyItemRangeInserted(startIndex, count); - - var changedCount = _adapter.ItemCount - startIndex; - _adapter.NotifyItemRangeChanged(startIndex, changedCount); } } @@ -69,9 +59,6 @@ public void NotifyItemRangeRemoved(IItemsViewSource source, int startIndex, int if (IsValidAdapter()) { _adapter.NotifyItemRangeRemoved(startIndex, count); - - var changedCount = _adapter.ItemCount - startIndex; - _adapter.NotifyItemRangeChanged(startIndex, changedCount); } } @@ -80,9 +67,6 @@ public void NotifyItemRemoved(IItemsViewSource source, int startIndex) if (IsValidAdapter()) { _adapter.NotifyItemRemoved(startIndex); - - var changedCount = _adapter.ItemCount - startIndex; - _adapter.NotifyItemRangeChanged(startIndex, changedCount); } } diff --git a/src/Controls/src/Core/Handlers/Items/Android/MauiRecyclerView.cs b/src/Controls/src/Core/Handlers/Items/Android/MauiRecyclerView.cs index c4afc301aa67..66219acdf3cc 100644 --- a/src/Controls/src/Core/Handlers/Items/Android/MauiRecyclerView.cs +++ b/src/Controls/src/Core/Handlers/Items/Android/MauiRecyclerView.cs @@ -572,6 +572,7 @@ internal void UpdateEmptyViewVisibility() var currentAdapter = GetAdapter(); if (showEmptyView && currentAdapter != _emptyViewAdapter) { + GetRecycledViewPool().Clear(); SwapAdapter(_emptyViewAdapter, true); // TODO hartez 2018/10/24 17:34:36 If this works, cache this layout manager as _emptyLayoutManager @@ -580,6 +581,7 @@ internal void UpdateEmptyViewVisibility() } else if (!showEmptyView && currentAdapter != ItemsViewAdapter) { + GetRecycledViewPool().Clear(); SwapAdapter(ItemsViewAdapter, true); UpdateLayoutManager(); } diff --git a/src/Controls/src/Core/Handlers/Items/ItemsViewHandler.Android.cs b/src/Controls/src/Core/Handlers/Items/ItemsViewHandler.Android.cs index ca839407bc84..cf6f3c76231a 100644 --- a/src/Controls/src/Core/Handlers/Items/ItemsViewHandler.Android.cs +++ b/src/Controls/src/Core/Handlers/Items/ItemsViewHandler.Android.cs @@ -109,8 +109,38 @@ void UpdateEmptyViewSize(double width, double height) if (adapter is EmptyViewAdapter emptyViewAdapter) { + var emptyView = emptyViewAdapter.EmptyView ?? emptyViewAdapter.EmptyViewTemplate; + + var size = Size.Zero; + + if (emptyView is IView view) + { + if (view.Handler == null) + { + TemplateHelpers.GetHandler(view as View, this.MauiContext); + } + + size = view.Measure(double.PositiveInfinity, double.PositiveInfinity); + } + else if (emptyView is DataTemplate dataTemplate) + { + var content = dataTemplate.CreateContent() as IView; + + if (content.Handler == null) + { + TemplateHelpers.GetHandler(content as View, this.MauiContext); + } + + size = content.Measure(double.PositiveInfinity, double.PositiveInfinity); + } + + var measuredHeight = size.Height; + + if (!double.IsInfinity(size.Height)) + measuredHeight = Context.ToPixels(size.Height); + emptyViewAdapter.RecyclerViewWidth = width; - emptyViewAdapter.RecyclerViewHeight = height; + emptyViewAdapter.RecyclerViewHeight = measuredHeight > 0 ? measuredHeight : height; } } } From d9390aebe71ff1d89c904980cb3b0401f9352b07 Mon Sep 17 00:00:00 2001 From: Vignesh-SF3580 Date: Thu, 19 Sep 2024 23:28:39 +0530 Subject: [PATCH 2/8] updated Items/ItemsViewHandler.Android.cs --- .../Items/ItemsViewHandler.Android.cs | 21 ++++--------------- 1 file changed, 4 insertions(+), 17 deletions(-) diff --git a/src/Controls/src/Core/Handlers/Items/ItemsViewHandler.Android.cs b/src/Controls/src/Core/Handlers/Items/ItemsViewHandler.Android.cs index cf6f3c76231a..a3c10b2bd038 100644 --- a/src/Controls/src/Core/Handlers/Items/ItemsViewHandler.Android.cs +++ b/src/Controls/src/Core/Handlers/Items/ItemsViewHandler.Android.cs @@ -110,10 +110,11 @@ void UpdateEmptyViewSize(double width, double height) if (adapter is EmptyViewAdapter emptyViewAdapter) { var emptyView = emptyViewAdapter.EmptyView ?? emptyViewAdapter.EmptyViewTemplate; + Size size = Size.Zero; - var size = Size.Zero; + IView view = emptyView as IView ?? (emptyView as DataTemplate)?.CreateContent() as IView; - if (emptyView is IView view) + if (view != null) { if (view.Handler == null) { @@ -122,22 +123,8 @@ void UpdateEmptyViewSize(double width, double height) size = view.Measure(double.PositiveInfinity, double.PositiveInfinity); } - else if (emptyView is DataTemplate dataTemplate) - { - var content = dataTemplate.CreateContent() as IView; - - if (content.Handler == null) - { - TemplateHelpers.GetHandler(content as View, this.MauiContext); - } - - size = content.Measure(double.PositiveInfinity, double.PositiveInfinity); - } - - var measuredHeight = size.Height; - if (!double.IsInfinity(size.Height)) - measuredHeight = Context.ToPixels(size.Height); + var measuredHeight = !double.IsInfinity(size.Height) ? Context.ToPixels(size.Height) : height; emptyViewAdapter.RecyclerViewWidth = width; emptyViewAdapter.RecyclerViewHeight = measuredHeight > 0 ? measuredHeight : height; From 404484d9cb4dac0b1369f3681109f4f340ce2c0a Mon Sep 17 00:00:00 2001 From: Vignesh-SF3580 Date: Thu, 26 Sep 2024 15:15:59 +0530 Subject: [PATCH 3/8] Added testcases. --- .../Items/Android/MauiRecyclerView.cs | 2 +- .../Items/ItemsViewHandler.Android.cs | 4 +- .../TestCases.HostApp/Issues/Issue11896.xaml | 57 ++++++++++++++ .../Issues/Issue11896.xaml.cs | 74 +++++++++++++++++++ .../Tests/Issues/Issue11896.cs | 55 ++++++++++++++ 5 files changed, 189 insertions(+), 3 deletions(-) create mode 100644 src/Controls/tests/TestCases.HostApp/Issues/Issue11896.xaml create mode 100644 src/Controls/tests/TestCases.HostApp/Issues/Issue11896.xaml.cs create mode 100644 src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue11896.cs diff --git a/src/Controls/src/Core/Handlers/Items/Android/MauiRecyclerView.cs b/src/Controls/src/Core/Handlers/Items/Android/MauiRecyclerView.cs index 66219acdf3cc..9bcc1758eb3e 100644 --- a/src/Controls/src/Core/Handlers/Items/Android/MauiRecyclerView.cs +++ b/src/Controls/src/Core/Handlers/Items/Android/MauiRecyclerView.cs @@ -567,7 +567,7 @@ internal void UpdateEmptyViewVisibility() itemCount++; } - var showEmptyView = ItemsView?.EmptyView != null && ItemsViewAdapter.ItemCount == itemCount; + var showEmptyView = (ItemsView?.EmptyView is not null || ItemsView?.EmptyViewTemplate is not null) && ItemsViewAdapter.ItemCount == itemCount; var currentAdapter = GetAdapter(); if (showEmptyView && currentAdapter != _emptyViewAdapter) diff --git a/src/Controls/src/Core/Handlers/Items/ItemsViewHandler.Android.cs b/src/Controls/src/Core/Handlers/Items/ItemsViewHandler.Android.cs index a3c10b2bd038..6853439348e6 100644 --- a/src/Controls/src/Core/Handlers/Items/ItemsViewHandler.Android.cs +++ b/src/Controls/src/Core/Handlers/Items/ItemsViewHandler.Android.cs @@ -114,9 +114,9 @@ void UpdateEmptyViewSize(double width, double height) IView view = emptyView as IView ?? (emptyView as DataTemplate)?.CreateContent() as IView; - if (view != null) + if (view is not null) { - if (view.Handler == null) + if (view.Handler is null) { TemplateHelpers.GetHandler(view as View, this.MauiContext); } diff --git a/src/Controls/tests/TestCases.HostApp/Issues/Issue11896.xaml b/src/Controls/tests/TestCases.HostApp/Issues/Issue11896.xaml new file mode 100644 index 000000000000..7de4c4225914 --- /dev/null +++ b/src/Controls/tests/TestCases.HostApp/Issues/Issue11896.xaml @@ -0,0 +1,57 @@ + + + + + + +