Issue
I need to know which elements are currently displayed in my RecyclerView. There is no equivalent to the href="http://developer.android.com/reference/android/widget/AbsListView.OnScrollListener.html#onScroll%28android.widget.AbsListView,%20int,%20int,%20int%29">OnScrollListener.onScroll(...)
method on ListViews. I tried to work with View.getGlobalVisibleRect(...)
, but that hack is too ugly and does not always work too.
Someone any ideas?
Solution
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();
For example:
GridLayoutManager layoutManager = ((GridLayoutManager)mRecyclerView.getLayoutManager());
int firstVisiblePosition = layoutManager.findFirstVisibleItemPosition();
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.
Answered By - yigit
Answer Checked By - Gilberto Lyons (JavaFixing Admin)