Scroll RecyclerView up accordingly when keyboard opens

后端 未结 7 1856
情歌与酒
情歌与酒 2021-01-12 15:34

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 scro

7条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-12 16:23

    Late answer but maybe someone will find it useful.

    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.

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

    layoutManager.reverseLayout = true
    

    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.

提交回复
热议问题