滚动RecyclerView滚动到始终位于顶部的位置 [英] Scroll RecyclerView Scroll to position always on top
问题描述
我正在使用线性布局管理器,并使用RecyclerView
和LinearLayout Manager
来填充某些项目列表.第一次显示recyclerview
并使用时:
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.
现在这是棘手的部分-当我滚动到recyclerview
的顶部时(即新商品索引将低于desiredindex
),然后我打电话给我:
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);
它仍然可以正常工作,但是当recyclerview
滚动到desiredindex,
之外时,recycler view
滚动以使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.
推荐答案
像这样使用scrollToPositionWithOffset
:
linearLayoutManager.scrollToPositionWithOffset(desiredindex, 0);
scrolltopositionwithoffset(position,偏移量),使带有指示偏移量的指示项可见.偏移量是距RecyclerView
顶部的距离.
scrolltopositionwithoffset(position, offset) forces the indicated item visible with indicated offset. The offset is distance from the top of RecyclerView
.
这篇关于滚动RecyclerView滚动到始终位于顶部的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!