如何从Android中的RecyclerView适配器获取项目

Raf*_*ael 54 android android-recyclerview

在使用ListView自定义适配器的旧代码中,我可以使用此代码获取项目.

Message msg = (Message) adapter.getItem(poistion);
Run Code Online (Sandbox Code Playgroud)

现在我正在实施RecyclerView.我如何从中获取物品RecyclerView.adapter

pde*_*d59 68

你必须自己实现它.

在自定义适配器中添加新方法,您就完成了.

public Message getItem(int position) {
  return messageList.get(position);
}
Run Code Online (Sandbox Code Playgroud)


Vol*_*ret 48

我认为正确的方法getItem()是通过RecyclerView LayoutManager.

View v = recyclerView.getLayoutManager().findViewByPosition(position);
Run Code Online (Sandbox Code Playgroud)

  • 这是如何获取视图,而不是视图的数据. (13认同)
  • @PhoenixFF是的,但是如果你有EditTexts作为项目,这是获取它们的价值的最佳方法(在用户更改之后)!:) (2认同)