We all use RecyclerView in many apps and we always need to bind data to the views and handle user clicks on any of RecyclerView items, to do this we have three options
position argument inside onBindViewHolder method
getAdapterPosition method from ViewHolder
and getLayoutPosition method from ViewHolder
So imagine that we have a simple RecyclerView that display a list of numbers. We need to bind data to the RecyclerView items and handle user click on any of them so which one of those three options should we use and what is the difference between them ?
1- position argument in onBindViewHolder method.
as you see we can use the position to bind data to the view and it is okay to use position argument to do this.
but it is not okay to use position argument to handle user clicks and if you used it you will see a warning tells you don’t treat position as fixed and use holder.getAdapterPosition instead.
Why? because if you deleted/added/changed any item in the data set and notified the RecyclerView Using notifyItem*(), RecyclerView will not call onBindViewHolder method and update all item’s positions, It will only update the position of the new ones for the new calls of onBindViewHolder and this will cause inconsistency between displayed items and position value.
Imagine that we have a RecyclerView that will display 10 items so it will create 10 items and call onBindView for those 10 items and pass the positions from 0 to 9, so if you fixed the position by using it to handle user clicks and later you added an item at position 0 and notified the data set that you inserted a new item by notifyItemInserted() the RecyclerView will create a new item with position 0 and pass it to the layout but the pre created ones still have the old positions and if you logged those positions you will have 00123…9 which is not true it should be 0123…10. Here come the power of holder.getAdapterPosition().
2- getAdapterPosition method.
The ViewHolder provide us the getAdapterPosition which is always have the updated adapter’s position of this holder. What does that mean ? it means that whenever you clicked on an item (ViewHolder item) we ask the adapter about it’s position. so you will get the latest position of this item in terms of Adapter’s logic.
3- getLayoutPosition method.
What is the getLayoutPosition method and when to use it ? RecyclerView isolates the data set from how we display it that why we use LayoutMangers to manage the way we display the data from the data set and the RecyclerView updates the Layout with the logic of batches which means that it waits for changes to be done and then passes those changes to the layout. This waiting period is less than 16 ms so in most times you won’t find any difference between getAdapterPosition and getLayoutPosition because of this small period of waiting time but it is still possible to find that you need to get the position in terms of the updated layout (the last passed layout that the user see now) for example:- If the user asks for the third position he see and you use swipe/dismiss for items or apply any animation or decorations for items it will be better to use getLayoutPosition instead of getAdapterPosition cause you will always be sure that you are dealing with the items’ position in terms of the latest passed layout.
notice that i used getAdapterPosition to bind data and used getLayoutPosition to tell the user the position of the pressed item.
Thanks for your time and hope this helps you.