简体   繁体   English

如何防止recyclerView自动滚动?

[英]how to prevent recyclerView auto scroll?

I have a recyclerView with chat message and open it will scroll to the latest position,but i update item's height in onBindViewHolder and recyclerView will auto scroll to incorrect position,how to pin item position after update item's height?我有一个带有聊天消息的 recyclerView,打开它将滚动到最新的 position,但是我在 onBindViewHolder 中更新了项目的高度,并且 recyclerView 将自动滚动到不正确的 position,如何在项目更新后固定项目 Z4757FE07FD492ADD36BE0EA6?

into getMessages method add this below code在 getMessages 方法中添加以下代码

 layoutManager.smoothScrollToPosition(messagesRecycler, null, chatAdapter.getItemCount());
 chatAdapter.notifyDataSetChanged();

if you want scroll to last item use lm.setStackFromEnd(true);如果要滚动到最后一项,请使用lm.setStackFromEnd(true);

use below code:使用以下代码:

LinearLayoutManager llm = new LinearLayoutManager(getContext());
llm.setStackFromEnd(true);
recyclerView.setLayoutManager(llm);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM