Scroll RecyclerView up accordingly when keyboard opens

后端 未结 7 1899
情歌与酒
情歌与酒 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:22

    I have been searching for the correct solution, and I found it I think.

    When you place your recyclerview inside a ConstraintLayout thinking that way you can easily place your recyclerview correctly in relation to your other views (in your case your edittext), it will not work.

    This used to be my layout xml, where it didnt work (I have reverseLayout=true on my LinearLayoutManager):

    
    
      
    
      
    
    
    
    

    With this layout, everytime I opened or closed the keyboard by pressing the EditText the scroll position would move slightly up.

    I fixed it by not using ConstraintLayout, but replacing it for a good old RelativeLayout like so:

    
    
      
         />
    
      
    
    
    
    

    When setting the RecyclerView height to match_parent it will work. (The reason we use RelativeLayout is that on ConstraintLayout, you can't set the layouth_height to match_parent of child views, it will not function properly if you do that.)

提交回复
热议问题