滚动RecyclerView滚动到始终位于顶部

erl*_*man 6 android scrolltop android-linearlayout android-recyclerview

我正在使用线性布局管理器,并RecyclerView使用a LinearLayout Manager来填充一些项目列表.当我recyclerview第一次显示并使用时:

linearLayoutManager.scrollToPosition(desiredindex);
Run Code Online (Sandbox Code Playgroud)

它滚动到我喜欢的顶部:

现在这里是棘手的部分,

当我滚动到顶部recyclerview(即新项目索引将低于desiredindex)并且我打电话:

linearLayoutManager.scrollToPosition(desiredindex);
Run Code Online (Sandbox Code Playgroud)

它仍然可以正常工作.

但是当recyclerview已滚动超出desiredindex,recycler view滚动使得desiredindex项目涉及到底部,而不是顶部.

但我希望瓷砖滚动到顶部而不是底部:

nsh*_*ura 23

使用scrollToPositionWithOffset这样:

linearLayoutManager.scrollToPositionWithOffset(desiredindex, 0);
Run Code Online (Sandbox Code Playgroud)

scrolltopositionwithoffset(position,offset)强制指示的项目以指示的偏移量显示.偏移量是距离顶部的距离RecyclerView.

  • `linearLayoutManager.scrollToPosition(desiredindex)` 更短并且也有效 (3认同)