键盘打开时,向上滚动RecyclerView [英] Scroll RecyclerView up accordingly when keyboard opens

查看:423
本文介绍了键盘打开时,向上滚动RecyclerView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个聊天活动,并且像facebook Messenger一样,底部有一个EditText,上方是消息的RecyclerView.键盘打开后,我想向上滚动RecyclerView到底应该向上滚动多少.更具体地说,假设RecyclerView中有5条消息,当前第3条消息位于EditText上方.因此,当键盘打开时,我想像以前一样将第3条消息保留在EditText上方.我已经搜索了,但是找不到.如果这是一个重复的问题,那么如果有人可以提供链接,我将不胜感激.

I have created a chat activity and like facebook messenger, there is an EditText at the bottom and above is the RecyclerView of messages. When Keyboard opens, I want to scroll RecyclerView up exactly how much it should scroll up. To be more specific, lets assume there are 5 messages in the RecyclerView and currently 3rd message is just above the EditText. So when keyboard is open, I want to keep 3rd message just above the EditText like before. I have searched but could not find. If this is a duplicate question, I will be grateful if anyone can provide the link.

推荐答案

最新答案,但也许有人会觉得有用.

Late answer but maybe someone will find it useful.

如果它是一个聊天应用程序,那么我想您想从底部开始构建列表.例如,有一个空的聊天室,添加了几条消息后,您希望它们位于列表的底部.后来,随着越来越多的消息,较旧的消息将上升,而较新的消息将在EditText上方的底部.

If it is a chat app then I suppose you want to start building the list from the bottom. For example, there is an empty chat and after you add a couple of message you expect them to be at the bottom of the list. Later, as there will be more and more messages, older messages will go up and newer ones will be on the bottom, right above the EditText.

假设最新消息位于列表/数组的第一位,则只需将LinearLayoutManager设置为反向布局即可.

Assuming newest messages are first in your list/array you can just set the LinearLayoutManager to be reverse layout.

layoutManager.reverseLayout = true

我无法解释为什么,但是看起来RecyclerView总是尝试保留顶部边框的滚动位置.因此,当显示键盘时,可用的垂直空间会缩小,并且RecyclerView只会尝试保持其顶部与以前相同.通过使其反向布局,您可以实现所需的行为.

I can't explain why but it looks like RecyclerView always tries to retain scroll position of the top border. So when keyboard is shown available vertical space shrinks and RecyclerView will just try to keep its top the same as it was before. By making it reverse layout you can achieve the desired behaviour.

这篇关于键盘打开时,向上滚动RecyclerView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆