Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is difference between holder.getAdapterPosition() and position attribute from constructor in onBindViewHolder() [duplicate]

Tags:

java

android

Here is my code -

public void onBindViewHolder(myViewHolder holder, int position) {

        //1. details obj = list.get(holder.getAdapterPosition());
        //2. details obj = list.get(position);

        holder.position = position;
    }

I am getting a warning

Do not treat position as fixed; only use immediately and call holder.getAdapterPosition() to look it up later RecyclerView will not call onBindViewHolder again when 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 later.

So i am confused from 1 and 2 which should i prefer and why? As it says getAdapterPosition() gives updated position and i am getting values from list based on position.

Thank you.

like image 849
Shunan Avatar asked Aug 16 '16 08:08

Shunan


People also ask

What is the use of onBindViewHolder?

onBindViewHolder. Called by RecyclerView to display the data at the specified position. This method should update the contents of the itemView to reflect the item at the given position.

How often is onBindViewHolder called?

However, in RecyclerView the onBindViewHolder gets called every time the ViewHolder is bound and the setOnClickListener will be triggered too. Therefore, setting a click listener in onCreateViewHolder which invokes only when a ViewHolder gets created is preferable.

What does notifyDataSetChanged do in RecyclerView?

What does notifyDataSetChanged() do on recyclerview ? Notify any registered observers that the data set has changed. There are two different classes of data change events, item changes and structural changes. Item changes are when a single item has its data updated but no positional changes have occurred.

How do I get the selected item position in the adapter?

You can use the ViewHolder's getAdapterPosition() to retrieve the item's position within an interface method. Then store the clicked position in a member variable.


1 Answers

The warning you get is not about using position or getAdapterPosition(). It is about saving the position:

holder.position = position;

You don't need to save the position in your holder, because its position can change and you can always get its position by calling holder.getAdapterPosition();

From the docs:

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.

About which to use:

Both should return the same result if called inside the onBindViewHolder method. I would recommend using position because it is easiest and safest.

like image 117
Robbe Avatar answered Oct 12 '22 19:10

Robbe