Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Redth committed Aug 19, 2023
2 parents 2734c19 + 4abec86 commit 23f83cc
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 22 deletions.
4 changes: 3 additions & 1 deletion Sample/VirtualListViewSample/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions VirtualListView/Controls/VirtualListView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,16 +111,16 @@ public Maui.SelectionMode SelectionMode

public event EventHandler<SelectedItemsChangedEventArgs> OnSelectedItemsChanged;

public event EventHandler<EventArgs> OnRefresh;
public event EventHandler<RefreshEventArgs> 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
Expand Down
2 changes: 1 addition & 1 deletion VirtualListView/IVirtualListView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public interface IVirtualListView : IView

Color RefreshAccentColor { get; }

void Refresh();
void Refresh(Action completionCallback);

bool IsRefreshEnabled { get; }

Expand Down
24 changes: 14 additions & 10 deletions VirtualListView/Platforms/Android/RvAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -79,7 +81,8 @@ public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int positi
}
}

List<string> cachedReuseIds = new List<string>();
Dictionary<string, int> cachedReuseIds = new ();
int reuseIdCount = 100;

public override int GetItemViewType(int position)
{
Expand All @@ -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)
{
Expand Down Expand Up @@ -146,9 +150,9 @@ public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int

public void Reset()
{
lock (lockObj)
{
cachedReuseIds.Clear();
}
//lock (lockObj)
//{
// cachedReuseIds.Clear();
//}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
12 changes: 12 additions & 0 deletions VirtualListView/RefreshEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace Microsoft.Maui;

public class RefreshEventArgs : EventArgs
{
public RefreshEventArgs(Action completion)
: base()
{
Complete = completion;
}

public readonly Action Complete;
}
2 changes: 1 addition & 1 deletion VirtualListView/SelectedItemsChangedEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ public SelectedItemsChangedEventArgs(
public IReadOnlyList<ItemPosition> PreviousSelection { get; }

public IReadOnlyList<ItemPosition> NewSelection { get; }
}
}

0 comments on commit 23f83cc

Please sign in to comment.