获取RecyclerView可见项 [英] Get visible items in RecyclerView
问题描述
我需要知道哪些元素在我的RecyclerView当前显示。没有等同于<一个href="http://developer.android.com/reference/android/widget/AbsListView.OnScrollListener.html#onScroll%28android.widget.AbsListView,%20int,%20int,%20int%29"><$c$c>OnScrollListener.onScroll(...)$c$c>方法上的列表视图。我试图与合作<一个href="http://developer.android.com/reference/android/view/View.html#getGlobalVisibleRect%28android.graphics.Rect,%20android.graphics.Point%29"><$c$c>View.getGlobalVisibleRect(...)$c$c>,但黑客是太丑陋,不总是工作了。
I need to know which elements are currently displayed in my RecyclerView. There is no equivalent to the OnScrollListener.onScroll(...)
method on ListViews. I tried to work with View.getGlobalVisibleRect(...)
, but that hack is too ugly and does not always work too.
有人什么想法?
推荐答案
第一个/最后一个可见子依赖于布局管理。 如果您使用的是LinearLayoutManager或GridLayoutManager,你可以使用
first / last visible child depends on the LayoutManager. If you are using LinearLayoutManager or GridLayoutManager, you can use
int findFirstVisibleItemPosition();
int findFirstCompletelyVisibleItemPosition();
int findLastVisibleItemPosition();
int findLastCompletelyVisibleItemPosition();
例如:
GridLayoutManager layoutManager = ((GridLayoutManager)mRecyclerView.getLayoutManager());
int firstVisiblePosition = layoutManager.findFirstVisibleItemPosition();
有关LinearLayoutManager,第一/最后取决于适配器排序。不询问从RecyclerView儿童;布局管理可能preFER布局比可见多个项目进行缓存。
For LinearLayoutManager, first/last depends on the adapter ordering. Don't query children from RecyclerView; LayoutManager may prefer to layout more items than visible for caching.
这篇关于获取RecyclerView可见项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!