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

吃可爱长大的小学妹 提交于 2019-12-06 19:29:57

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!