Java – constructor. In onbindviewholder() What is the difference between getadapterposition () and the position attribute of the constructor [copy]

See English answer > lint error "do not treat position as fixed; only use immediately..." 1

public void onBindViewHolder(myViewHolder holder,int position) {

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

        holder.position = position;
    }

I received a warning

So I'm confused about 1 and 2. Which should I choose? Why? As it says, getadapterposition () provides the updated location, and I get the location - based value from the list

thank you.

Solution

The warning you get is not about using position or getadapterposition() This is about the Save Location:

holder.position = position;

You do not need to save the position in the holder, because its position can be changed, you can call holder getAdapterPosition();

From document:

About which to use:

If invoked in the onBindViewHolder method, both of them should return the same result. I recommend using location because it is the simplest and safest

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>