如何使用RecyclerView.scrollToPosition()将位置移动到当前视图的顶部? [英] How to use RecyclerView.scrollToPosition() to move the position to the top of current view?
问题描述
RecyclerView.scrollToPosition()
非常奇怪.
例如,假设RecyclerView
名为"rv"
.
-
如果现在第10项在当前
RecyclerView
下,请调用rv.scrollToPosition(10)
,RecyclerView
会将第10项滚动到底部. -
如果现在第10项位于当前的
RecyclerView
中,请调用rv.scrollToPosition(10)
,将不会有任何滚动,将不会执行任何操作. -
如果现在第10项位于当前
RecyclerView
的顶部,请调用rv.scrollToPosition(10)
,RecyclerView
会将第10项滚动到顶部.
为帮助理解,请看这张图片
但是我需要的是,每当我调用它时,就像情况3一样,RecyclerView
会将假定位置滚动到当前视图的顶部.
如果我理解这个问题,则希望滚动到特定位置,但是该位置是适配器的位置,而不是RecyclerView
的项目位置./p>
您只能通过LayoutManager
来实现.
执行以下操作:
rv.getLayoutManager().scrollToPosition(youPositionInTheAdapter).
The RecyclerView.scrollToPosition()
is extremely strange.
for example, supposed a RecyclerView
named "rv"
.
if now item 10 is under the current
RecyclerView
, callrv.scrollToPosition(10)
, theRecyclerView
will scroll item 10 to bottom.if now item 10 is in the current
RecyclerView
, callrv.scrollToPosition(10)
, there won't be any scrolling, nothing will be done.if now item 10 is on the top of the current
RecyclerView
, callrv.scrollToPosition(10)
, theRecyclerView
will scroll item 10 to top.
To help understanding, look at this picture
But what i need is that, whenever i call it, the RecyclerView
will scroll the supposed position to the top of current view just like case 3. How to do that?
If I understand the question, you want to scroll to a specific position but that position is the adapter's position and not the RecyclerView
's item position.
You can only achieve this through the LayoutManager
.
Do something like:
rv.getLayoutManager().scrollToPosition(youPositionInTheAdapter).
这篇关于如何使用RecyclerView.scrollToPosition()将位置移动到当前视图的顶部?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!