Lint error “Do not treat position as fixed; only use immediately…”

Update:
The method getAdapterPosition() is deprecated now.


The documentation of RecyclerView.Adapter.onBindViewHolder() states:

Note that unlike ListView, RecyclerView will not call this method
again if the position of the item changes in the data set unless the
item itself is invalidated or the new position cannot be determined.
For this reason, you should only use the position parameter while
acquiring the related data item inside this method and should not keep
a copy of it. If you need the position of an item later on (e.g. in a
click listener), use getAdapterPosition() which will have the updated
adapter position

So, technically items may be re-arranged (like sorted, or moved around) and binding will not be necessary because items are not invalidated yet. This means onBindViewHolder() may NOT be called if items show the same data, but just their position/index in a list changes. The position variable received holds true only for the scope of bind function and will not always point to the correct position in the data set. That’s why the function getAdapterPosition() must be called every time updated position is needed.

IMHO, mLastPosition = holder.getAdapterPosition(); is still potentially wrong. Because item may be re-arranged and mLastPosition still points to old position.

About why lint is silent, perhaps Lint’s rule is not that thorough. It’s only checking whether the position parameter is being copied or not.

Leave a Comment

tech