将recyclerview焦点转移到列表中的最后一个元素

Div*_*ash 21 android android-layout android-recyclerview

我正在使用recyclerview在聊天窗口中显示消息.但是,在编辑新消息时,回收器视图焦点应位于最后一个元素上.我希望有一些方法可以将recyclerview的焦点转移到最后一个元素而不是第一个元素.

Vip*_*sri 39

使用 recyclerView.smoothScrollToPosition(position);

position是您插入的最后一项的索引.这将把你的RecyclerView注意力集中在最后一个元素上.


rd7*_*773 11

使用可以在LinearLayoutManager对象上添加任意两行,将Recyclerview滚动到底部...

llm.setStackFromEnd(true);
Run Code Online (Sandbox Code Playgroud)

要么

llm.setReverseLayout(true);
Run Code Online (Sandbox Code Playgroud)

  • 它从底部填充列表。这不是我要找的东西。Vipul 的回答解决了我的问题。顺便说一句,谢谢。 (3认同)

Pri*_*ary 6

使用

recyclerView.smoothScrollToPosition(position)
adapter.notifyDataSetChanged();
Run Code Online (Sandbox Code Playgroud)

位置是最后一个元素的索引

recyclerView.getAdapter().getItemCount()-1
Run Code Online (Sandbox Code Playgroud)

使用它来获取最后一个元素。

这对我有用。