Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically scroll to the bottom of a Recycler View? [duplicate]

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

like image 734
SuperBale Avatar asked Feb 15 '16 22:02

SuperBale


Video Answer


2 Answers

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..!!

like image 182
Mukesh Rana Avatar answered Sep 20 '22 13:09

Mukesh Rana


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

like image 30
Doug Stevenson Avatar answered Sep 18 '22 13:09

Doug Stevenson