13

I want to scroll to the bottom of a recycler view on click of a button, how do I do this?

SuperBale
  • 341
  • 2
  • 8
  • 20

2 Answers2

33

You have to make use of LayoutManager for that. Follow the below steps.

1). First of all, declare LayoutManager in your Activity/Fragment. For example, I have taken LinearLayoutManager

private LinearLayoutManager mLinearLayoutManager;

2). Initialise the LinearLayoutManager and set that to your RecyclerView

mLinearLayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(mLinearLayoutManager);

3). On your Button onClick, do this to scroll to the bottom of your RecyclerView.

mLinearLayoutManager.scrollToPosition(yourList.size() - 1); // yourList is the ArrayList that you are passing to your RecyclerView Adapter.

Hope this will help..!!

Mukesh Rana
  • 4,051
  • 3
  • 27
  • 39
  • 3
    3) It should be mLinearLayoutManager.scrollToPosition(yourList.size() - 1); – Eliseo Ocampos Apr 21 '17 at 17:15
  • 2
    If recycle view element is big (image for example) this show only start if item. So this not real scroll to bottom (I can still scroll more by hand after this) – Alex Oct 20 '18 at 16:03
  • @Alex Not working! Image in the list item is the cause? – Arnold Brown May 09 '20 at 05:38
  • 1
    @ArnoldBrown No. Problem is that scrollToPosition() scroll to top of last element. And need scroll to bottom of last element. – Alex May 12 '20 at 13:36
3

You can use scrollToPosition() with the index of the last position.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • 1
    Based on the doc, " RecyclerView does not implement scrolling logic, rather forwards the call to scrollToPosition(int)". It does not implement the logic, simply call this function will does nothing. – hjchin Nov 30 '16 at 14:05
  • 2
    My apologies, on second look, it works. – hjchin Nov 30 '16 at 14:10
  • Same as previous answer - If recycle view element is big (image for example) this show only start if item. So this not real scroll to bottom (I can still scroll more by hand after this) – Alex Oct 20 '18 at 16:03
  • This is not working. Image in the list item is the cause? – Arnold Brown May 09 '20 at 05:36