android - 获取 RecyclerView 中的可见项目

标签 android android-recyclerview

我需要知道我的 RecyclerView 中当前显示了哪些元素。没有等效于 OnScrollListener.onScroll(...) ListViews 上的方法。我尝试使用 View.getGlobalVisibleRect(...) ,但是这种 hack 太丑陋了,而且并不总是有效。

有人有什么想法吗?

最佳答案

第一个/最后一个可见子项取决于 LayoutManager。 如果您使用 LinearLayoutManagerGridLayoutManager,则可以使用

int findFirstVisibleItemPosition();
int findFirstCompletelyVisibleItemPosition();
int findLastVisibleItemPosition();
int findLastCompletelyVisibleItemPosition();

例如:

GridLayoutManager layoutManager = ((GridLayoutManager)mRecyclerView.getLayoutManager());
int firstVisiblePosition = layoutManager.findFirstVisibleItemPosition();

对于 LinearLayoutManager,first/last 取决于适配器的顺序。不要从 RecyclerView 查询 child ; LayoutManager 可能更喜欢布局比可见缓存更多的项目。

关于android - 获取 RecyclerView 中的可见项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24989218/

相关文章:

android - 在 recyclerview 中实现 Facebook 横幅广告

java - 将用户输入编辑文本与 SQLite 数据库 Android Java 进行比较

android - bin/bash : -c: line 0: syntax error near unexpected token `;;'

android - 如何使用 Kotlin 在 ListAdapter 中使用 Filterable?

android - RecyclerView 面临的小问题

android - 点击监听器应该在回收站 View 中的什么位置?

java - 大 Nerd 牧场 Android。 RecyclerView不会显示数据

android - Android 应用程序上的 Oauth 2

android - 当我滚动 gridview 时图像发生变化

android - 在 Kotlin 中使用 Replace 函数