At First, my RecyclerView items are ok, but on scrolling, items are shown on the wrong position for example: item 6 shown in position 67. Although onClick listener and getAdapte
In my case this problem was happening because I had setHasStableIds(true)
in one RecyclerView.Adapter
(in fact, it was HasStableIds = true;
since I work in Xamarin.Android).
After removing that line (HasStableIds
is false
by default), all positions got back to normal and I didn't need to disable recycling or to return position
from GetItemViewType(int position)
(which is nice because I actually use this method to choose between multiple ViewHolder
types). Then just out of interest I tried to enable HasStableIds
and override long GetItemId(int position)
– it also worked well.
So my conclusion is: if for some reason you need to enable HasStableIds
, you should override both long GetItemId(int position)
and int GetItemViewType(int position)
in your RecyclerView.Adapter
's subclass. If you do not have unique item IDs and/or different view types, just returning back the position
parameter will do the job.