The different concepts of positions in RecyclerView can help you handle data items and user interactions more efficiently.

The different concepts of positions in RecyclerView can help you handle data items and user interactions more efficiently.

getAdapterPosition

getAdapterPosition() is a method of the RecyclerView.ViewHolder class that is used to get the position of the item associated with the ViewHolder in the adapter. It is very useful in data binding, click event handling, and other scenarios where you need to know the specific position of an item in a list or grid.

When you use the getAdapterPosition method from RecyclerView's onBindViewHolder, onClick, or other ViewHolder-related callbacks, it returns the index of the data item bound to the current ViewHolder in the adapter.

  1. "Data change": Before getAdapterPosition() is called, the data in the adapter has changed (for example, through notifyDataSetChanged(), notifyItemInserted(), notifyItemRemoved(), etc.), the ViewHolder may be out of sync with the actual position due to recycling and reuse, and the returned position may no longer reflect the current UI state.
  2. 「ViewHolder reuse」: In order to improve performance, RecyclerView reuses ViewHolder instances to bind different data items. Even if ViewHolder is reused to represent new data items, getAdapterPosition() will return the position of the new data item in the adapter.
  3. "Invalid position": ViewHolder is no longer associated with any data item (for example, because the list item was completely removed or scrolled off the screen), getAdapterPosition() may return RecyclerView.NO_POSITION (-1). Before using getAdapterPosition(), it is best to check whether the return value is valid.
 public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int adapterPosition = holder.getAdapterPosition(); if (adapterPosition != RecyclerView.NO_POSITION) { // 处理点击事件,使用adapterPosition作为索引} } }); }

picture

Unfortunately, the getAdapterPosition method is marked as deprecated.

getBindingAdapterPosition

getBindingAdapterPosition() is a method of the RecyclerView.ViewHolder class that is similar to getAdapterPosition() but has a slightly different purpose.

RecyclerView uses LayoutManager to determine the position and size of each item, and uses Adapter to provide data and bind data to views. To optimize performance, RecyclerView reuses ViewHolder objects, which means that the same ViewHolder instance may be used to bind data in different locations.

  1. 「getAdapterPosition()」

What is returned is the adapter position when the current ViewHolder was last bound to data (the position after the data last changed).

If the ViewHolder is not rebound after the data changes, the previous position may be returned.

  1. "getBindingAdapterPosition()"

Only valid in the context of the onBindViewHolder() method.

Returns the position of the data item currently being bound to the ViewHolder in the adapter.

Calling this method outside of onBindViewHolder() may return RecyclerView.NO_POSITION (-1), at which point the ViewHolder may not be bound to any data item.

getAbsoluteAdapterPosition

getAbsoluteAdapterPosition() is a method of RecyclerView.ViewHolder that is used to get the absolute position of the item associated with the ViewHolder in the adapter.

  • When the RecyclerView adapter is nested, use getAbsoluteAdapterPosition() to get the absolute position of the nested RecyclerView item in the outer RecyclerView adapter.
  • Compared to getAdapterPosition(), getAbsoluteAdapterPosition() takes into account all nested RecyclerView levels and returns a global, unique position that is not affected by the level.

1. "Return value"

If the ViewHolder is currently associated with an item, returns an integer greater than or equal to 0 that represents the absolute position of the item within the adapter.

If the ViewHolder is not currently associated with any item (for example, because the item has been removed from the list or the ViewHolder is being recycled), returns RecyclerView.NO_POSITION, which has a value of -1.

2. "Notes"

getAbsoluteAdapterPosition() takes into account the nested RecyclerView hierarchy and the returned position may be different from the position obtained directly via getAdapterPosition().

When dealing with nested RecyclerView interactions or data, use getAbsoluteAdapterPosition() to ensure that you get a global, unique position.

getAbsoluteAdapterPosition() may also return RecyclerView.NO_POSITION when the ViewHolder is recycled or not associated with any item, which should be checked before using its return value.

getLayoutPosition

getLayoutPosition() is a method of the RecyclerView.ViewHolder class that is used to get the position of the ViewHolder in the current layout. This position is determined based on the items currently visible on the screen and the scroll state of the RecyclerView.

1.「Use scenarios」

When you need to know the current position of a ViewHolder on the screen (not just its position in the adapter), you can use getLayoutPosition().

Unlike getAdapterPosition(), getLayoutPosition() returns the position of the ViewHolder in the current list of visible items, which may change due to scrolling.

2. Return value

If the ViewHolder is currently associated with an item and is visible on the screen, returns an integer greater than or equal to 0 indicating the position of the item in the current layout.

If the ViewHolder is not currently associated with any item (for example, because the item has been removed from the list or the ViewHolder is being recycled), returns RecyclerView.NO_POSITION, which has a value of -1.

3. "Notes"

The position returned by getLayoutPosition() is based on the items currently visible on the screen and the scroll state of the RecyclerView. If the RecyclerView is scrolled, the layout position of the item may change even if its adapter position does not change.

If the ViewHolder has been recycled by the RecyclerView or is no longer associated with any item, getLayoutPosition() will return RecyclerView.NO_POSITION.

In most cases, if you need to process data or events related to items and do not care about the specific location of these items on the screen, it is more appropriate to use the Adapter-related location methods.

<<:  Several practical methods to limit EditText input characters in Android development

>>:  The launcher process starts and the user interface is presented, revealing the key steps of each stage

Recommend

Doujinbihuo Academy "Douyin Money-Making Account Revealed Course"

Through studying the course "The Secret of D...

Weibo promotion skills, what are the Weibo promotion methods?

1. Understand all Weibo traffic entrances to help...

Top 10 keywords for website navigation in 2015

1. Polarization <p class="MsoListParagraph" st...

Samsung and Apple mobile phone sales comparison

Samsung Electronics' Galaxy S5 flagship smartp...

Video advertising placement and optimization!

Nowadays, video ads are no longer exclusive to vi...