Skip to content

Commit

Permalink
Swap less on Android
Browse files Browse the repository at this point in the history
  • Loading branch information
Redth committed Nov 28, 2023
1 parent a1bb44a commit 6f2b93d
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 24 deletions.
10 changes: 7 additions & 3 deletions VirtualListView/Platforms/Android/RvAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
26 changes: 17 additions & 9 deletions VirtualListView/Platforms/Android/RvItemHolder.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Android.Views;
using AndroidX.RecyclerView.Widget;
using Microsoft.Maui.Controls;

namespace Microsoft.Maui;

Expand All @@ -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);
// }
}
25 changes: 13 additions & 12 deletions VirtualListView/Platforms/Android/RvViewContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
}

0 comments on commit 6f2b93d

Please sign in to comment.