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

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

问题描述

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

The RecyclerView.scrollToPosition() is extremely strange. for example, supposed a RecyclerView named "rv".

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

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

如果现在第 10 项在当前 RecyclerView 中,调用 rv.scrollToPosition(10),不会有任何滚动,什么也不会做.

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

如果现在第 10 项在当前 RecyclerView 的顶部,调用 rv.scrollToPosition(10)RecyclerView将第 10 项滚动到顶部.

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

但我需要的是,每当我调用它时,RecyclerView 都会像情况 3 一样将假定的位置滚动到当前视图的顶部.怎么做?

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?

推荐答案

如果我理解这个问题,您想滚动到特定位置,但该位置是适配器的位置,而不是 RecyclerView's 项目位置.

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.

你只能通过LayoutManager来实现这一点.

You can only achieve this through the LayoutManager.

做类似的事情:

rv.getLayoutManager().scrollToPosition(youPositionInTheAdapter).

这篇关于如何使用 RecyclerView.scrollToPosition() 将位置移动到当前视图的顶部?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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