Android - 从 RecyclerView.findViewHolderForAdapterPosition() 获取行

Android - Get row from RecyclerView.findViewHolderForAdapterPosition()

我需要获取对 recyclerView 中特定行的引用。

我以前也是这样做的

View row = recyclerView.childAt(position);

由于childAt()方法是在ViewGroup的基础上实现的,一旦滚动列表,位置就乱了。

我读 我必须使用 recyclerView.findViewHolderForAdapterPosition() 其中 returns 一个 ViewHolder

如何从 ViewHolder 到我需要使用的实际 View

你应该使用 findViewByPosition

findViewByPosition(int position)

Finds the view which represents the given adapter position.

View row = recyclerView.getLayoutManager().findViewByPosition(YourPosition);

您可以使用其 public itemView 成员变量从 ViewHolder 获取视图:

RecyclerView.ViewHolder viewHolder = recyclerView.findViewHolderForAdapterPosition(position);
final View itemView = viewHolder.itemView;