Skip to content

Commit a1d91a4

Browse files
committed
Android - no stable item ids
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.
1 parent 7fcf92d commit a1d91a4

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

VirtualListView/Platforms/Android/RvAdapter.cs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ public override int ItemCount
2424
internal RvAdapter(Context context, VirtualListViewHandler handler, PositionalViewSelector positionalViewSelector)
2525
{
2626
Context = context;
27+
HasStableIds = false;
28+
2729
this.handler = handler;
2830
this.positionalViewSelector = positionalViewSelector;
2931
}
@@ -79,7 +81,8 @@ public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int positi
7981
}
8082
}
8183

82-
List<string> cachedReuseIds = new List<string>();
84+
Dictionary<string, int> cachedReuseIds = new ();
85+
int reuseIdCount = 100;
8386

8487
public override int GetItemViewType(int position)
8588
{
@@ -103,19 +106,20 @@ public override int GetItemViewType(int position)
103106

104107
lock (lockObj)
105108
{
106-
vt = cachedReuseIds.IndexOf(reuseId) + 1;
107-
if (vt <= 0)
109+
if (!cachedReuseIds.TryGetValue(reuseId, out var reuseIdNumber))
108110
{
109-
cachedReuseIds.Add(reuseId);
110-
vt = cachedReuseIds.Count;
111+
reuseIdNumber = ++reuseIdCount;
112+
cachedReuseIds.Add(reuseId, reuseIdNumber);
111113
}
114+
115+
vt = reuseIdNumber;
112116
}
113117

114118
return vt;
115119
}
116120

117121
public override long GetItemId(int position)
118-
=> position;
122+
=> RecyclerView.NoId;
119123

120124
public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
121125
{
@@ -146,9 +150,9 @@ public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int
146150

147151
public void Reset()
148152
{
149-
lock (lockObj)
150-
{
151-
cachedReuseIds.Clear();
152-
}
153+
//lock (lockObj)
154+
//{
155+
// cachedReuseIds.Clear();
156+
//}
153157
}
154158
}

0 commit comments

Comments
 (0)