Skip to content

Commit

Permalink
Android - no stable item ids
Browse files Browse the repository at this point in the history
Was returning 'position' for the item id which is potentially wrong if things aren't 'stable' (so if we add / remove items, etc).  According to the docs it should return `NoId` in this case, and we should set `HasStableIds` to `false` to indicate the Id should not be used.

Also, the cached reuse id mapping to int values was being cleared out when the data was invalidated, which means potentially the reuse id's mapped int value could be changing for a given reuse id, causing old and incorrect values to be returned and maybe recycling with a wrong / old template.

Finally, some of the built in item view types have values in the < 100 range, so we'll start our count returning values > 100 to avoid conflicts here.

This should fix some android weirdness that was a result of the incorrect assumptions.
  • Loading branch information
Redth committed Aug 15, 2023
1 parent 7fcf92d commit a1d91a4
Showing 1 changed file with 14 additions and 10 deletions.
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();
//}
}
}

0 comments on commit a1d91a4

Please sign in to comment.