'Kotlin: findFirstVisibleItemPosition() also returns last visible item
For practice I am trying to replicate call logs app.
I want to show floating date, like this:

I'm using findFirstVisibleItemPosition() for getting date, but it is not returning the top most item.
When I scroll to last it also returns last most item. Why so? Is there any other method??
See:
https://youtu.be/Wum5h73Qqf0
Note: I'm using recyclerview in only half screen that's why I recorded only half screen.
binding.listRecentCalls.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
if (dy > 0 || dy < 0) {
val firstVisible = (recyclerView.layoutManager as LinearLayoutManager).findFirstVisibleItemPosition()
binding.tvScrolledDate.text = logsList[firstVisible].date
binding.tvScrolledDate.visibility = View.VISIBLE
}
}
})
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
