如何使用RecyclerView.scrollToPosition()将位置移动到当前视图的顶部? [英] How to use RecyclerView.scrollToPosition() to move the position to the top of current view?

查看:3391
本文介绍了如何使用RecyclerView.scrollToPosition()将位置移动到当前视图的顶部?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

RecyclerView.scrollToPosition()非常奇怪. 例如,假设RecyclerView名为"rv".

  1. 如果现在第10项在当前RecyclerView下,请调用rv.scrollToPosition(10)RecyclerView会将第10项滚动到底部.

  2. 如果现在第10项位于当前的RecyclerView中,请调用rv.scrollToPosition(10),将不会有任何滚动,将不会执行任何操作.

  3. 如果现在第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".

  1. if now item 10 is under the current RecyclerView, call rv.scrollToPosition(10), the RecyclerView will scroll item 10 to bottom.

  2. if now item 10 is in the current RecyclerView, call rv.scrollToPosition(10), there won't be any scrolling, nothing will be done.

  3. if now item 10 is on the top of the current RecyclerView, call rv.scrollToPosition(10), the RecyclerView 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屋!

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