getAdapterPositiongetAdapterPosition() 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.
picture Unfortunately, the getAdapterPosition method is marked as deprecated. getBindingAdapterPositiongetBindingAdapterPosition() 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.
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.
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. getAbsoluteAdapterPositiongetAbsoluteAdapterPosition() is a method of RecyclerView.ViewHolder that is used to get the absolute position of the item associated with the ViewHolder in the adapter.
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. getLayoutPositiongetLayoutPosition() 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 valueIf 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
Algorithms are an indispensable evaluation mechan...
When China's Internet and mobile Internet dem...
IP, which stands for Intellectual Property, origi...
As an excellent content question-and-answer commu...
As the temperature gradually drops, the ginkgo be...
Calendar Girl Tips There are snakes in the pictur...
What weakness marketing does is to lead people to...
Wenchang position can be beneficial to academic a...
At present, the epidemic is spreading abroad, and...
"Anyone who has trouble sleeping should drin...
Previously, Zotye was always labeled as a "c...
Song Ge Not long ago, the topic "Why can'...
Recently, the State Administration of Cultural He...
Produced by: Science Popularization China Author:...
After the beginning of autumn, the weather has be...