I have a RecyclerView
inside a NestedScrollView
and I'm trying to get the position of the last visible list item during scroll events using. The following code allows me to reliably detect scroll events:
val recyclerView = nestedScrollView.getChildAt(0) as RecyclerView
nestedScrollView.setOnScrollChangeListener { v: NestedScrollView?, scrollX: Int, scrollY: Int, oldScrollX: Int, oldScrollY: Int ->
val layoutManager = recyclerView?.layoutManager as? LinearLayoutManager
val lastVisiblePosition = layoutManager?.findLastVisibleItemPosition()
}
The problem however is that here lastVisiblePosition
always ends up being the last item in the list whether this is actually on scree or not. Where could I be going wrong?