如何获得RecyclerView的偏移量?

tei*_*luj 4 java android

我想保存RecyclerView的位置,然后稍后重置.如果顶部项目只有一半可见,我可以用什么来保存这个偏移?

对于我正在使用的位置:findFirstCompletelyVisibleItemPosition()

要重置我正在使用的位置:

        ((LinearLayoutManager) recyclerView.getLayoutManager()).scrollToPositionWithOffset(position, offset);
Run Code Online (Sandbox Code Playgroud)

但我不确定作为偏移传递什么?

小智 14

你必须使用LinearLayoutManager这个..

LinearLayoutManager manager = (LinearLayoutManager) recyclerView.getLayoutManager();
int position = manager.findFirstVisibleItemPosition();
View firstItemView = manager.findViewByPosition(position);
float Offset = firstItemView.getTop();
Run Code Online (Sandbox Code Playgroud)

并获取当前窗口中第一个项目的位置和偏移量.