不建议使用getAdapterPosition() [英] getAdapterPosition() is deprecated
问题描述
我已经将targetSdkVersion
从28更新到30,并且我发现getAdapterPosition()
已过时(我不确定何时发生).
I've updated my targetSdkVersion
from 28 to 30 and I've noticed that getAdapterPosition()
is deprecated (I'm not sure when this happened).
在文档中,说以下话:
不建议使用此方法.
当适配器嵌套其他适配器时,此方法会造成混淆.如果您是在适配器的上下文中调用此方法,则可能要调用 getBindingAdapterPosition(),或者如果您希望RecyclerView看到该位置,则应调用
This method is deprecated.
This method is confusing when adapters nest other adapters. If you are calling this in the context of an Adapter, you probably want to call getBindingAdapterPosition() or if you want the position as RecyclerView sees it, you should call getAbsoluteAdapterPosition().
文档还显示以下内容:
请注意,如果您要查询RecyclerView看到的位置,则应使用 getAbsoluteAdapterPosition()(例如,您想使用它来保存滚动状态).如果要查询访问RecyclerView.Adapter内容的位置,则应使用 getBindingAdapterPosition().
Note that if you are querying the position as RecyclerView sees, you should use getAbsoluteAdapterPosition() (e.g. you want to use it to save scroll state). If you are querying the position to access the RecyclerView.Adapter contents, you should use getBindingAdapterPosition().
我的理解是:
-
当您要获取适配器位置时(如果适配器中仍然存在),应使用
-
getBindingAdapterPosition
.如果适配器中不再存在它,它将返回-1
( NO_POSITION ).
应该使用 -
getAbsoluteAdapterPosition
来获取RecyclerView
所看到的位置.例如,如果某项已被删除但尚未从ViewHolder
中删除.
getBindingAdapterPosition
should be used when you want to get the adapter position (if it still exists in the adapter). If it no longer exists in the adapter, it will return-1
(NO_POSITION).getAbsoluteAdapterPosition
should be used to get the position as theRecyclerView
sees it. For example, if an item has been deleted, but not yet removed from theViewHolder
.
换句话说,如果我的Adapter
中有4
个项目,则删除位置0
并查询getAbsoluteAdapterPosition
和getBindingAdapterPosition
,然后将该项目从ViewHolder
中删除,然后将0
(因为视图仍在ViewHolder
中),而getBindingAdapterPosition
返回-1
(因为适配器中不再存在该视图).
In other words, if I have 4
items in my Adapter
, I delete position 0
and query getAbsoluteAdapterPosition
and getBindingAdapterPosition
before the item has been removed from the ViewHolder
, then getAbsoluteAdapterPosition
will return 0
(because view is still in the ViewHolder
) and getBindingAdapterPosition
return -1
(Because it no longer exists in the adapter).
我通过记录以下内容测试了差异:
I have tested the difference by logging the following:
Log.e("difference", "getAdapterPosition = "+myHolder.getAdapterPosition()+" getBindingAdapterPosition = "+myHolder.getBindingAdapterPosition()+" getAbsoluteAdapterPosition = "+myHolder.getAbsoluteAdapterPosition());
它们返回完全相同的值.我看不出有什么区别.
They return exactly the same values. I could not see any difference.
在调用notifyItemChanged
,notifyDataSetChanged
或notifyItemRangeChanged
之前或之后,我也看不出有什么区别.但是,当我删除位置0
并调用notifyItemRemoved
时,它随后将返回-1
(对于所有它们).
I also see no difference before or after calling notifyItemChanged
, notifyDataSetChanged
or notifyItemRangeChanged
. But when I delete position 0
and call notifyItemRemoved
it returns -1
afterward (for all of them).
我是否正确理解这点?何时使用?还有,什么时候会有区别?
Do I understand this correctly, and when should we be using which? Also, when will there be a difference?
推荐答案
recyclerview:1.2.0-alpha02
引入了MergeAdapter
"rel =" nofollow noreferrer> strong> ConcatAdapter
和 recyclerview:1.2.0-alpha04
.
该RecyclerView适配器可以线性组合多个适配器.
The recyclerview:1.2.0-alpha02
introduced the MergeAdapter
that has been renamed to ConcatAdapter
with recyclerview:1.2.0-alpha04
.
This RecyclerView Adapter that can combine multiple adapters linearly.
类似的东西:
FirstAdapter adapter1 = ...;
SecondoAdapter adapter2 = ...;
MergeAdapter merged = new MergeAdapter(adapter1, adapter2);
recyclerView.setAdapter(mergedAdapter);
使用这些更改后,方法 getAdapterPosition()
被弃用,因为当适配器嵌套其他适配器时,该名称会造成混淆.现在,您应该使用 getBindingAdapterPosition()
方法,该方法将返回特定适配器中的位置(bindingAdapter
).
With these changes the method getAdapterPosition()
was deprecated because the name is confusing when adapters nest other adapters. Now you should use the method getBindingAdapterPosition()
which returns the position in the specific adapter (bindingAdapter
).
还要检查RecyclerView.ViewHolder
的源代码:
@Deprecated
public final int getAdapterPosition() {
return getBindingAdapterPosition();
}
相反,方法 getAbsoluteAdapterPosition()
返回项目相对于整个 RecyclerView
的适配器位置.如果使用的是ConcatAdapter
,则它是ConcatAdapter
中的位置,而不是单个bindingAdapter中的位置.
Instead the method getAbsoluteAdapterPosition()
returns the Adapter position of the item with respect to the entire RecyclerView
. If you are using a ConcatAdapter
it is the position in the ConcatAdapter
and not the position in the single bindingAdapter.
仅举一个带有2个适配器的示例:
Just an example with 2 adapters:
这篇关于不建议使用getAdapterPosition()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!