99

Since the new support library version (22.x) the getPosition() method of the RecyclerView.ViewHolder class has been deprecated in lieu of the methods mentioned in the topic. I don't really get the difference from reading the docs. Could somebody explain the difference in layman's terms?

I have the following use case - I give my adapter a List, and also want to be able to associate extra info for each list item. I have a position-to-extra mapping, and the mapping is available for the holders so that they can fetch the extra for their position and do stuff with it. In the holder, which method should I use?

What happens with the holder positions when list items at indices 0 and 1 are switched places? What do the methods return?

CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
wujek
  • 10,112
  • 12
  • 52
  • 88

2 Answers2

123

This is a tricky situation, sorry that docs are not sufficient.

When adapter contents change (and you call notify***()) RecyclerView requests a new layout. From that moment, until layout system decides to calculate a new layout (<16 ms), the layout position and adapter position may not match because layout has not reflected adapter changes yet.

In your use case, since your data is related to your adapter contents (and I assume that data is changed at the same time with adapter changes), you should be using adapterPosition.

Be careful though, if you are calling notifyDataSetChanged(), because it invalidates everything, RecyclerView does not know that ViewHolder's adapter position until next layout is calculated. In that case, getAdapterPosition() will return RecyclerView#NO_POSITION (-1).

But lets say if you've called notifyItemInserted(0), the getAdapterPosition() of ViewHolder which was previously at position 0 will start returning 1 immediately. So as long as you are dispatching granular notify events, you are always in good state (we know adapter position even though new layout is not calculated yet).

Another example, if you are doing something on user click, if getAdapterPosition() returns NO_POSITION, it is best to ignore that click because you don't know what user clicked (unless you have some other mechanism, e.g. stable ids to lookup the item).

Edit For When Layout Position is Good

Lets say you are using LinearLayoutManager and want to access the ViewHolder above the currently clicked item. In that case, you should use layout position to get the item above.

mRecyclerView.findViewHolderForLayoutPosition(myViewHolder.getLayoutPosition() - 1)

You have to use layout position because it matches what user is currently seeing on the screen.

Sufian
  • 6,405
  • 16
  • 66
  • 120
yigit
  • 37,683
  • 13
  • 72
  • 58
  • 1
    I played around a bit and it turns out the getAdapterPosition() method always returns -1 on me. I debugged it and the reason is that the code in the method (final ViewParent parent = itemView.getParent(); if (!(parent instanceof RecyclerView)) { return -1; } always gets into the if block, i.e. the recycler view is not the parent of my cell view. How can it be? My code to create the holder is: return MyViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.test_list_item, viewGroup, false)); (Continued in another comment.) – wujek Apr 18 '15 at 15:06
  • When I change the code to call inflate(R.layout.test_list_item, viewGroup, true); (note the true for 'attach to root), Android throws: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. So what is the correct way to create a view holder with a view that is correctly attached to the recycler view? Strangely enough, even if getAdapterPosition() returns -1 because the parent is null, everything else works fine. – wujek Apr 18 '15 at 15:08
  • 1
    The boolean parameter in layout inflator is "addToParent". It must be false because it is LayoutManager's responsibility to add it. I think you are calling getAdapterPosition in onBind where you are already passed the position. Technically, view holder represents that position after onBind returns. Btw, we've updated getAdapter position to return a valid position (if possible) even if it is detached, it will be released soon. – yigit Apr 18 '15 at 18:00
  • You are right, I'm calling getAdapterPosition in onBind. What should I do in this case instead, I do need the position to get some information which influences the states of some views. Is calling getLayoutPosition in this case fine? – wujek Apr 18 '15 at 19:01
  • 5
    You should use the position parameter that is passed to onBind method. – yigit Apr 19 '15 at 08:44
  • Of course, silly me. Thank you for all your help, now I understand better. – wujek Apr 19 '15 at 10:23
  • I had also an issue getting -1 and could see now that calling notifyItemRemoved also invalidates like notifyDataSetChanged, notifyItemInserted does not as you already mentioned. Still not clear though how it is the correct way to handle onClick in this case, ideally the list should get greyed out while clicks are not possible. – David Nov 22 '17 at 15:51
  • One thing that I don't quite get it. When we remove some items and then the proper notify method on the adapter, the recycler view will request a layout. However, the layout manager would still keep those 'to be removed' items on the screen until the item animator completes it's job, right? So, does that mean the layout manager has always a bigger set of positions than the adapter itself? – stdout Feb 06 '18 at 15:37
  • WHY getLayoutPosition sometimes returns -1???it is not even documented this situation – Rafael Lima Apr 05 '20 at 05:04
3

In order to argue the difference(s) of getAdapterPosition(), getLayoutPosition(), and also position; we would notice the cases below:

1.position argument in onBindViewHolder() method:

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 "not to treat position as fixed and use holder.getAdapterPosition() instead".

2.getAdapterPosition():

This method always consists the updated adapter’s position of the holder. It means whenever you clicked on an item, you 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():

Sometimes, it is needed to find the position in terms of the updated layout (the last passed layout that the user is seeing now), for example: If the user asks for the third position he can 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.


For more information on this; see here . . .

akhilesh0707
  • 6,709
  • 5
  • 44
  • 51
elyar abad
  • 771
  • 1
  • 8
  • 27