Android: get the exact position on the screen that the user was at in a recyclerview

丶灬走出姿态 提交于 2019-12-06 22:17:41

Have you looked at RecyclerView.ScrollBy(x,y)?

ScrollBy(0,y) will scroll the RecyclerView in the same direction as it would if you were to move your finger up. It won't scroll to an absolute position, instead, it will just add pixels to the previous scroll position. To scroll in the opposite direction, simply use ScrollBy(0,-y). Notice that x is set to zero because you only want to scroll up/down.

Now, in order to achieve a complete solution, you will need to have a way to save the list's current scroll position. I did not test it, but I am pretty sure that RecyclerView.getScrollY() will always return 0. To overcome this, you will need to track the position yourelf by listening for scroll changes to the RecyclerView.

When you want to restore the scroll position, you can use your saved current position since the RecyclerView will, initially, have a scroll value of 0.

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