From 6f2b93d1a442cde6aced79ad51355509676dd5bd Mon Sep 17 00:00:00 2001 From: redth Date: Tue, 28 Nov 2023 15:34:50 -0500 Subject: [PATCH 1/5] Swap less on Android --- .../Platforms/Android/RvAdapter.cs | 10 ++++--- .../Platforms/Android/RvItemHolder.cs | 26 ++++++++++++------- .../Platforms/Android/RvViewContainer.cs | 25 +++++++++--------- 3 files changed, 37 insertions(+), 24 deletions(-) diff --git a/VirtualListView/Platforms/Android/RvAdapter.cs b/VirtualListView/Platforms/Android/RvAdapter.cs index 73cfa71..c3403a3 100644 --- a/VirtualListView/Platforms/Android/RvAdapter.cs +++ b/VirtualListView/Platforms/Android/RvAdapter.cs @@ -75,11 +75,15 @@ public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int positi _ => null }; - var view = itemHolder?.VirtualView ?? positionalViewSelector?.ViewSelector?.CreateView(info, data); + if (itemHolder?.VirtualView is null) + { + var newView = positionalViewSelector?.ViewSelector?.CreateView(info, data); + itemHolder.SetupView(newView); + } - itemHolder.Update(info, view); + itemHolder.UpdatePosition(info); - positionalViewSelector?.ViewSelector?.RecycleView(info, data, itemHolder.ViewContainer.VirtualView); + positionalViewSelector?.ViewSelector?.RecycleView(info, data, itemHolder.ViewContainer.VirtualView); } } diff --git a/VirtualListView/Platforms/Android/RvItemHolder.cs b/VirtualListView/Platforms/Android/RvItemHolder.cs index ac1af88..f097d6e 100644 --- a/VirtualListView/Platforms/Android/RvItemHolder.cs +++ b/VirtualListView/Platforms/Android/RvItemHolder.cs @@ -1,5 +1,6 @@ using Android.Views; using AndroidX.RecyclerView.Widget; +using Microsoft.Maui.Controls; namespace Microsoft.Maui; @@ -22,18 +23,25 @@ public RvItemHolder(IMauiContext mauiContext, ListOrientation orientation) public IView VirtualView => ViewContainer?.VirtualView; - public void Update(PositionInfo positionInfo, IView newView) - { - PositionInfo = positionInfo; - if (newView is IPositionInfo viewWithPositionInfo) - viewWithPositionInfo.Update(PositionInfo); - - SwapView(newView); + public void SetupView(IView view) + { + ViewContainer.SetupView(view); } - void SwapView(IView view) + public void UpdatePosition(PositionInfo positionInfo) { - ViewContainer.SwapView(view); + PositionInfo = positionInfo; + ViewContainer.UpdatePosition(positionInfo); } + + //public void Update(PositionInfo positionInfo, IView newView) + //{ + // PositionInfo = positionInfo; + + // if (newView is IPositionInfo viewWithPositionInfo) + // viewWithPositionInfo.Update(PositionInfo); + + // ViewContainer.SwapView(newView); + // } } \ No newline at end of file diff --git a/VirtualListView/Platforms/Android/RvViewContainer.cs b/VirtualListView/Platforms/Android/RvViewContainer.cs index 65acff3..8dc80ea 100644 --- a/VirtualListView/Platforms/Android/RvViewContainer.cs +++ b/VirtualListView/Platforms/Android/RvViewContainer.cs @@ -18,20 +18,21 @@ public RvViewContainer(IMauiContext context) public AView NativeView { get; private set; } - public void SwapView(IView newView) + public void UpdatePosition(IPositionInfo positionInfo) { - if (VirtualView == null || VirtualView.Handler == null || NativeView == null) + if (VirtualView is IPositionInfo viewWithPositionInfo) + viewWithPositionInfo.Update(positionInfo); + } + + public void SetupView(IView view) + { + if (NativeView is null) + NativeView = view.ToPlatform(MauiContext); + + if (VirtualView is null) { - NativeView = newView.ToPlatform(MauiContext); - VirtualView = newView; + VirtualView = view; AddView(NativeView); } - else - { - var handler = VirtualView.Handler; - newView.Handler = handler; - handler.SetVirtualView(newView); - VirtualView = newView; - } - } + } } \ No newline at end of file From 2c8fe23c9039fa1a3fa9b5f93a923b70eade5269 Mon Sep 17 00:00:00 2001 From: redth Date: Tue, 28 Nov 2023 15:35:00 -0500 Subject: [PATCH 2/5] Make iOS more consistent with android --- .../Apple/CvCell.ios.maccatalyst.cs | 34 ++++++++++--------- .../Apple/CvDataSource.ios.maccatalyst.cs | 7 ++-- 2 files changed, 20 insertions(+), 21 deletions(-) diff --git a/VirtualListView/Apple/CvCell.ios.maccatalyst.cs b/VirtualListView/Apple/CvCell.ios.maccatalyst.cs index 0fdc3d8..53c0877 100644 --- a/VirtualListView/Apple/CvCell.ios.maccatalyst.cs +++ b/VirtualListView/Apple/CvCell.ios.maccatalyst.cs @@ -1,6 +1,7 @@ using CoreGraphics; using Foundation; using Microsoft.Maui.Platform; +using Microsoft.VisualBasic; using System.Diagnostics.CodeAnalysis; using UIKit; @@ -12,7 +13,7 @@ internal class CvCell : UICollectionViewCell public WeakReference IndexPath { get; set; } - public PositionInfo PositionInfo { get; set; } + public PositionInfo PositionInfo { get; private set; } public WeakReference> ReuseCallback { get; set; } @@ -109,31 +110,32 @@ public override void PrepareForReuse() } } - public void SwapView(IView newView) + public void SetupView(IView view) { // Create a new platform native view if we don't have one yet if (!(NativeView?.TryGetTarget(out var nativeView) ?? false)) { - nativeView = newView.ToPlatform(this.Handler.MauiContext); + nativeView = view.ToPlatform(this.Handler.MauiContext); nativeView.Frame = this.ContentView.Frame; nativeView.AutoresizingMask = UIViewAutoresizing.FlexibleHeight | UIViewAutoresizing.FlexibleWidth; this.ContentView.AddSubview(nativeView); NativeView = new WeakReference(nativeView); } - // Create a new virtual view if we don't have one yet - if (!(VirtualView?.TryGetTarget(out var virtualView) ?? false) || (virtualView?.Handler is null)) - { - virtualView = newView; - VirtualView = new WeakReference(virtualView); - } - else + if (!(VirtualView?.TryGetTarget(out var virtualView) ?? false) || (virtualView?.Handler is null)) + { + virtualView = view; + VirtualView = new WeakReference(virtualView); + } + } + + public void UpdatePosition(PositionInfo positionInfo) + { + PositionInfo = positionInfo; + if (VirtualView?.TryGetTarget(out var virtualView) ?? false) { - var handler = virtualView.Handler; - virtualView.Handler = null; - newView.Handler = handler; - handler.SetVirtualView(newView); - VirtualView.SetTarget(newView); - } + if (virtualView is IPositionInfo viewPositionInfo) + viewPositionInfo.Update(positionInfo); + } } } \ No newline at end of file diff --git a/VirtualListView/Apple/CvDataSource.ios.maccatalyst.cs b/VirtualListView/Apple/CvDataSource.ios.maccatalyst.cs index 96119e4..cf0873c 100644 --- a/VirtualListView/Apple/CvDataSource.ios.maccatalyst.cs +++ b/VirtualListView/Apple/CvDataSource.ios.maccatalyst.cs @@ -58,16 +58,13 @@ public override UICollectionViewCell GetCell(UICollectionView collectionView, NS if (cell.NeedsView) { var view = Handler?.PositionalViewSelector?.ViewSelector?.CreateView(info, data); - cell.SwapView(view); + cell.SetupView(view); } - cell.PositionInfo = info; + cell.UpdatePosition(info); if (cell.VirtualView.TryGetTarget(out var cellVirtualView)) { - if (cellVirtualView is IPositionInfo viewPositionInfo) - viewPositionInfo.IsSelected = info.IsSelected; - Handler?.PositionalViewSelector?.ViewSelector?.RecycleView(info, data, cellVirtualView); Handler.VirtualView.ViewSelector.ViewAttached(info, cellVirtualView); From d08495eda33a376afc33864c14df7716c0ac61a1 Mon Sep 17 00:00:00 2001 From: redth Date: Tue, 28 Nov 2023 16:16:03 -0500 Subject: [PATCH 3/5] Clean up needs view logic --- .../Apple/CvCell.ios.maccatalyst.cs | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/VirtualListView/Apple/CvCell.ios.maccatalyst.cs b/VirtualListView/Apple/CvCell.ios.maccatalyst.cs index 53c0877..076f2cf 100644 --- a/VirtualListView/Apple/CvCell.ios.maccatalyst.cs +++ b/VirtualListView/Apple/CvCell.ios.maccatalyst.cs @@ -92,7 +92,10 @@ public override UICollectionViewLayoutAttributes PreferredLayoutAttributesFittin } public bool NeedsView - => NativeView == null || !NativeView.TryGetTarget(out var _); + => NativeView == null + || VirtualView is null + || !NativeView.TryGetTarget(out var _) + || !VirtualView.TryGetTarget(out var _); public WeakReference VirtualView { get; set; } @@ -113,19 +116,20 @@ public override void PrepareForReuse() public void SetupView(IView view) { // Create a new platform native view if we don't have one yet - if (!(NativeView?.TryGetTarget(out var nativeView) ?? false)) + if (!(NativeView?.TryGetTarget(out var _) ?? false)) { - nativeView = view.ToPlatform(this.Handler.MauiContext); + var nativeView = view.ToPlatform(this.Handler.MauiContext); nativeView.Frame = this.ContentView.Frame; nativeView.AutoresizingMask = UIViewAutoresizing.FlexibleHeight | UIViewAutoresizing.FlexibleWidth; - this.ContentView.AddSubview(nativeView); - NativeView = new WeakReference(nativeView); + + this.ContentView.AddSubview(nativeView); + + NativeView = new WeakReference(nativeView); } if (!(VirtualView?.TryGetTarget(out var virtualView) ?? false) || (virtualView?.Handler is null)) { - virtualView = view; - VirtualView = new WeakReference(virtualView); + VirtualView = new WeakReference(view); } } From 79d7ff523b7254e37a5e0a3bd93b83d1927ca603 Mon Sep 17 00:00:00 2001 From: redth Date: Tue, 28 Nov 2023 16:16:19 -0500 Subject: [PATCH 4/5] Add needsview to android to be consistent --- VirtualListView/Platforms/Android/RvAdapter.cs | 2 +- VirtualListView/Platforms/Android/RvItemHolder.cs | 2 ++ VirtualListView/Platforms/Android/RvViewContainer.cs | 2 ++ 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/VirtualListView/Platforms/Android/RvAdapter.cs b/VirtualListView/Platforms/Android/RvAdapter.cs index c3403a3..6684e3e 100644 --- a/VirtualListView/Platforms/Android/RvAdapter.cs +++ b/VirtualListView/Platforms/Android/RvAdapter.cs @@ -75,7 +75,7 @@ public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int positi _ => null }; - if (itemHolder?.VirtualView is null) + if (itemHolder.NeedsView) { var newView = positionalViewSelector?.ViewSelector?.CreateView(info, data); itemHolder.SetupView(newView); diff --git a/VirtualListView/Platforms/Android/RvItemHolder.cs b/VirtualListView/Platforms/Android/RvItemHolder.cs index f097d6e..70f396c 100644 --- a/VirtualListView/Platforms/Android/RvItemHolder.cs +++ b/VirtualListView/Platforms/Android/RvItemHolder.cs @@ -23,6 +23,8 @@ public RvItemHolder(IMauiContext mauiContext, ListOrientation orientation) public IView VirtualView => ViewContainer?.VirtualView; + public bool NeedsView + => ViewContainer?.NeedsView ?? true; public void SetupView(IView view) { diff --git a/VirtualListView/Platforms/Android/RvViewContainer.cs b/VirtualListView/Platforms/Android/RvViewContainer.cs index 8dc80ea..0a03a96 100644 --- a/VirtualListView/Platforms/Android/RvViewContainer.cs +++ b/VirtualListView/Platforms/Android/RvViewContainer.cs @@ -18,6 +18,8 @@ public RvViewContainer(IMauiContext context) public AView NativeView { get; private set; } + public bool NeedsView => VirtualView is null || VirtualView.Handler is null || NativeView is null; + public void UpdatePosition(IPositionInfo positionInfo) { if (VirtualView is IPositionInfo viewWithPositionInfo) From bbf31a332d6ed021026881e5fae26c4d3b00182a Mon Sep 17 00:00:00 2001 From: redth Date: Tue, 28 Nov 2023 16:16:32 -0500 Subject: [PATCH 5/5] Add needsview to windows for consistency --- .../Platforms/Windows/IrElementContainer.cs | 36 ++++++++----------- .../Platforms/Windows/IrElementFactory.cs | 15 ++++---- 2 files changed, 23 insertions(+), 28 deletions(-) diff --git a/VirtualListView/Platforms/Windows/IrElementContainer.cs b/VirtualListView/Platforms/Windows/IrElementContainer.cs index 78b2ad4..573dca8 100644 --- a/VirtualListView/Platforms/Windows/IrElementContainer.cs +++ b/VirtualListView/Platforms/Windows/IrElementContainer.cs @@ -48,33 +48,27 @@ internal bool IsRecycled } } + public bool NeedsView + => VirtualView is null || VirtualView.Handler is null; + public IView VirtualView { get; private set; } - public void Update(PositionInfo positionInfo, IView newView) + public void SetupView(IView view) { - PositionInfo = positionInfo; - - if (newView is IPositionInfo viewWithPositionInfo) - viewWithPositionInfo.Update(PositionInfo); - - SwapView(newView); + if (VirtualView is null || VirtualView.Handler is null) + { + Content = view.ToPlatform(MauiContext); + VirtualView = view; + } } - void SwapView(IView newView) + public void UpdatePosition(PositionInfo positionInfo) { - if (VirtualView == null || VirtualView.Handler == null || Content == null) - { - Content = newView.ToPlatform(MauiContext); - VirtualView = newView; - } - else - { - var handler = VirtualView.Handler; - newView.Handler = handler; - handler.SetVirtualView(newView); - VirtualView = newView; - } - } + PositionInfo = positionInfo; + + if (VirtualView is IPositionInfo viewWithPositionInfo) + viewWithPositionInfo.Update(PositionInfo); + } protected override void OnTapped(TappedRoutedEventArgs e) { diff --git a/VirtualListView/Platforms/Windows/IrElementFactory.cs b/VirtualListView/Platforms/Windows/IrElementFactory.cs index 0f87a29..2efe363 100644 --- a/VirtualListView/Platforms/Windows/IrElementFactory.cs +++ b/VirtualListView/Platforms/Windows/IrElementFactory.cs @@ -85,16 +85,17 @@ public UIElement GetElement(UI.Xaml.ElementFactoryGetArgs args) && (Handler?.IsItemSelected(info.SectionIndex, info.ItemIndex) ?? false); - var view = container.VirtualView ?? PositionalViewSelector.ViewSelector?.CreateView(info, data); - - container.Update(info, view); - - + if (container.NeedsView) + { + var virtualView = PositionalViewSelector.ViewSelector?.CreateView(info, data); + container.SetupView(virtualView); + } + container.UpdatePosition(info); container.IsRecycled = false; - PositionalViewSelector.ViewSelector?.RecycleView(info, data, view); - PositionalViewSelector.ViewSelector?.ViewAttached(info, view); + PositionalViewSelector.ViewSelector?.RecycleView(info, data, container.VirtualView); + PositionalViewSelector.ViewSelector?.ViewAttached(info, container.VirtualView); return container; }