Scroll RecyclerView Scroll to position always on top

拥有回忆 提交于 2019-12-06 01:43:21

问题


I'm using linear layout manager and RecyclerView with a LinearLayout Manager to populate some list of items. When I'm displaying the recyclerview for the first time and I use:

linearLayoutManager.scrollToPosition(desiredindex);

it scrolls to the top exactly where I want.

Now here is the tricky part - When I'm scrolling to top of recyclerview (i.e. new items indices will be lower than the desiredindex) and I call:

linearLayoutManager.scrollToPosition(desiredindex);

It still works fine, but when the recyclerview has been scrolled beyond the desiredindex, the recycler view scrolls such that the desiredindex item comes to the bottom rather than on top, but I want the tile to scroll to the top not the bottom.


回答1:


Use scrollToPositionWithOffset like this:

linearLayoutManager.scrollToPositionWithOffset(desiredindex, 0);

scrolltopositionwithoffset(position, offset) forces the indicated item visible with indicated offset. The offset is distance from the top of RecyclerView.



来源:https://stackoverflow.com/questions/38811118/scroll-recyclerview-scroll-to-position-always-on-top

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