diff --git a/Sample/VirtualListViewSample/MainViewModel.cs b/Sample/VirtualListViewSample/MainViewModel.cs index 653fc72..ad879f0 100644 --- a/Sample/VirtualListViewSample/MainViewModel.cs +++ b/Sample/VirtualListViewSample/MainViewModel.cs @@ -15,9 +15,11 @@ public MainViewModel() MusicDataAdapter adapter; [RelayCommand] - async Task Refresh() + async Task Refresh(Action completion) { await Task.Delay(3000); + System.Diagnostics.Debug.WriteLine("Refresh Complete"); + completion?.Invoke(); } [RelayCommand] diff --git a/VirtualListView/Apple/VirtualListViewHandler.ios.maccatalyst.cs b/VirtualListView/Apple/VirtualListViewHandler.ios.maccatalyst.cs index b7d4b6e..5062a17 100644 --- a/VirtualListView/Apple/VirtualListViewHandler.ios.maccatalyst.cs +++ b/VirtualListView/Apple/VirtualListViewHandler.ios.maccatalyst.cs @@ -39,9 +39,7 @@ protected override UICollectionView CreatePlatformView() refreshControl.AddTarget(new EventHandler((s, a) => { refreshControl.BeginRefreshing(); - VirtualView?.Refresh(); - refreshControl.EndRefreshing(); - + VirtualView?.Refresh(() => refreshControl.EndRefreshing()); }), UIControlEvent.ValueChanged); collectionView.AddSubview(refreshControl); diff --git a/VirtualListView/Controls/VirtualListView.cs b/VirtualListView/Controls/VirtualListView.cs index e1be2bb..d4640ae 100644 --- a/VirtualListView/Controls/VirtualListView.cs +++ b/VirtualListView/Controls/VirtualListView.cs @@ -111,16 +111,16 @@ public Maui.SelectionMode SelectionMode public event EventHandler OnSelectedItemsChanged; - public event EventHandler OnRefresh; + public event EventHandler OnRefresh; - void IVirtualListView.Refresh() + void IVirtualListView.Refresh(Action completionCallback) { if (RefreshCommand != null && RefreshCommand.CanExecute(null)) { - RefreshCommand.Execute(null); + RefreshCommand.Execute(completionCallback); } - OnRefresh?.Invoke(this, EventArgs.Empty); + OnRefresh?.Invoke(this, new RefreshEventArgs(completionCallback)); } public ICommand RefreshCommand diff --git a/VirtualListView/IVirtualListView.cs b/VirtualListView/IVirtualListView.cs index 6dbc2e8..83b54d9 100644 --- a/VirtualListView/IVirtualListView.cs +++ b/VirtualListView/IVirtualListView.cs @@ -26,7 +26,7 @@ public interface IVirtualListView : IView Color RefreshAccentColor { get; } - void Refresh(); + void Refresh(Action completionCallback); bool IsRefreshEnabled { get; } diff --git a/VirtualListView/Platforms/Android/RvAdapter.cs b/VirtualListView/Platforms/Android/RvAdapter.cs index 696ec37..0dcb742 100644 --- a/VirtualListView/Platforms/Android/RvAdapter.cs +++ b/VirtualListView/Platforms/Android/RvAdapter.cs @@ -24,6 +24,8 @@ public override int ItemCount internal RvAdapter(Context context, VirtualListViewHandler handler, PositionalViewSelector positionalViewSelector) { Context = context; + HasStableIds = false; + this.handler = handler; this.positionalViewSelector = positionalViewSelector; } @@ -79,7 +81,8 @@ public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int positi } } - List cachedReuseIds = new List(); + Dictionary cachedReuseIds = new (); + int reuseIdCount = 100; public override int GetItemViewType(int position) { @@ -103,19 +106,20 @@ public override int GetItemViewType(int position) lock (lockObj) { - vt = cachedReuseIds.IndexOf(reuseId) + 1; - if (vt <= 0) + if (!cachedReuseIds.TryGetValue(reuseId, out var reuseIdNumber)) { - cachedReuseIds.Add(reuseId); - vt = cachedReuseIds.Count; + reuseIdNumber = ++reuseIdCount; + cachedReuseIds.Add(reuseId, reuseIdNumber); } + + vt = reuseIdNumber; } return vt; } public override long GetItemId(int position) - => position; + => RecyclerView.NoId; public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) { @@ -146,9 +150,9 @@ public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int public void Reset() { - lock (lockObj) - { - cachedReuseIds.Clear(); - } + //lock (lockObj) + //{ + // cachedReuseIds.Clear(); + //} } } \ No newline at end of file diff --git a/VirtualListView/Platforms/Android/VirtualListViewHandler.android.cs b/VirtualListView/Platforms/Android/VirtualListViewHandler.android.cs index 8d30f5f..295a76c 100644 --- a/VirtualListView/Platforms/Android/VirtualListViewHandler.android.cs +++ b/VirtualListView/Platforms/Android/VirtualListViewHandler.android.cs @@ -37,8 +37,7 @@ protected override void ConnectHandler(FrameLayout nativeView) { swipeRefreshLayout.SetOnRefreshListener(new SrlRefreshListener(() => { - VirtualView?.Refresh(); - swipeRefreshLayout.Refreshing = false; + VirtualView?.Refresh(() => swipeRefreshLayout.Refreshing = false); })); layoutManager = new LinearLayoutManager(Context); diff --git a/VirtualListView/RefreshEventArgs.cs b/VirtualListView/RefreshEventArgs.cs new file mode 100644 index 0000000..02b0f62 --- /dev/null +++ b/VirtualListView/RefreshEventArgs.cs @@ -0,0 +1,12 @@ +namespace Microsoft.Maui; + +public class RefreshEventArgs : EventArgs +{ + public RefreshEventArgs(Action completion) + : base() + { + Complete = completion; + } + + public readonly Action Complete; +} \ No newline at end of file diff --git a/VirtualListView/SelectedItemsChangedEventArgs.cs b/VirtualListView/SelectedItemsChangedEventArgs.cs index c076aeb..1a88f25 100644 --- a/VirtualListView/SelectedItemsChangedEventArgs.cs +++ b/VirtualListView/SelectedItemsChangedEventArgs.cs @@ -14,4 +14,4 @@ public SelectedItemsChangedEventArgs( public IReadOnlyList PreviousSelection { get; } public IReadOnlyList NewSelection { get; } -} \ No newline at end of file +}