滚动RecyclerView滚动到始终位于顶部的位置 [英] Scroll RecyclerView Scroll to position always on top

查看:2717
本文介绍了滚动RecyclerView滚动到始终位于顶部的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用线性布局管理器,并使用RecyclerViewLinearLayout 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆