问题
I want to scroll to the bottom of a recycler view on click of a button, how do I do this?
回答1:
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..!!
回答2:
You can use scrollToPosition() with the index of the last position.
来源:https://stackoverflow.com/questions/35419985/how-to-programmatically-scroll-to-the-bottom-of-a-recycler-view