1

When I add a new list item, with the keyboard still open, the list item appears behind the keyboard and is thus invisible. How do I scroll it automatically down to it becomes visible?

Keep in mind that I have already solved the following:

  • When opening keyboard scroll the RecyclerView to make the last item visible: Solved

The last remaining issue is when the keyboard is already open. It works fine when I open the keyboard for the first time. It's when I hit "send" that the item is added behind the keyboard and when I want it to scroll up

  1. I hit "send message" New message

  2. This is what happens. It's hidden behind the keyboard

When I've hit "send message" button

The Fluffy T Rex
  • 430
  • 7
  • 22
  • [https://stackoverflow.com/a/59471300/6253847](https://stackoverflow.com/a/59471300/6253847) Marat's answer is the simplest I've come across. – Sammy T Feb 25 '20 at 12:09
  • @SammyT setting reverse layout to true just makes the messages appear in the wrong order. Even when I set the incoming list items coming in in the other order to compensate for that and make them appear in the right order - it doesn't solve the issue. – The Fluffy T Rex Feb 25 '20 at 12:24
  • I have a RecyclerView I've been working on recently where this works perfectly. Yes, you have to reverse the order of your data set but the view stays aligned with the bottom edge. Are you scrolling to position in addition to reversing the layout? – Sammy T Feb 25 '20 at 12:29
  • yeah, I have tried both but issue persists. – The Fluffy T Rex Feb 25 '20 at 12:32
  • If you reverse the layout, you don't need to scroll to position. – Sammy T Feb 25 '20 at 12:36
  • I'm having some weird issue when I try that. The last list item I add to my recyclerview comes in at the very top at the list when it should come in at the bottom. When I close the fragment and re-open it again it's correctly at the bottom. Do you know what may be the cause? I do call adapter.notifyDataSetChanged(); – The Fluffy T Rex Feb 25 '20 at 12:54
  • Are you adding new items to the beginning of your adapter's list? – Sammy T Feb 25 '20 at 12:59
  • I just add items like so: messageArrayList.add(new message); then I call adapter.notifyDataSetChanged(); I'm also using Firebase so I have this in my code as well: .orderBy("TimeStamp", Query.Direction.DESCENDING) – The Fluffy T Rex Feb 25 '20 at 13:33
  • @TheFluffyTRex how did you solve scrolling the reyclerview to the bottom when the keyboard is opened? – AndroidDev123 Jun 13 '21 at 13:29

1 Answers1

3

Set your LayoutManager to reverse layout to have the RecyclerView retain its positioning against its bottom edge instead of the top one:

LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setReverseLayout(true);
myRecycler.setLayoutManager(layoutManager);

There should be no need to automatically scroll to the last position of your Adapter anymore so, if you previously had code to do so, you should remove it.

You will also have to reverse the order of the data set in your Adapter to match the ordering you originally had (If your data was originally sorted Ascending, then you should change it to Descending). For your case, you would want to sort by Descending time to have the items arranged by most recent at the bottom and oldest at the top of the RecyclerView.

And, if you're directly adding to the Adapter's data set, you should add to the beginning of the Adapter's list:

myDataList.add(0, myNewItem); // Add to the beginning of the list shifting previous items over
notifyDataSetChanged(); // Notify the adapter to refresh
Sammy T
  • 1,924
  • 1
  • 13
  • 20