Scroll RecyclerView up accordingly when keyboard opens

扶醉桌前 提交于 2020-01-03 17:30:30

问题


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.


回答1:


recyclerView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
        @Override
        public void onLayoutChange(View view, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
            if ( bottom < oldBottom) {
                recyclerView.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        recyclerView.scrollToPosition(adapter.getItemCount());
                    }
                }, 100);
            }
        }
    });

if it not work Use smoothScrollToPosition instead of scrollToPosition

This is work for me.




回答2:


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.




回答3:


I had the same problem, what I did is when the keyboard opens, I scroll the recyclerView to the last item in the recyclerView, so no items will be hidden. I think this is much easier than the way you were proposing. So the code will be as follows:

  recyclerView.postDelayed(new Runnable() {
            @Override
            public void run() {
                recyclerView.scrollToPosition(recyclerView.getAdapter().getItemCount() - 1);
            }
        }, 300);



回答4:


Write this in your Manifest

    <activity
        android:name=".yourActivity"
        android:windowSoftInputMode="adjustResize" >
    </activity>



回答5:


Try this

LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.scrollToPosition(yourPosition);


来源:https://stackoverflow.com/questions/47473230/scroll-recyclerview-up-accordingly-when-keyboard-opens

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!