4

How can i check if the recycler view is scrolled to its top (the begin of list) without using findFirstCompletelyVisibleItemPosition ?

Explanation: I cant use this method, because the items on RecyclerView are big and sometimes will never be "completely visible", so the method will return -1 (RecyclerView.NO_POSITION)

I need to know this to fire an action only when recycler view reach its top.

 val manager = timelineRecyclerView?.layoutManager as StaggeredGridLayoutManager
 val visibles = manager.findFirstCompletelyVisibleItemPositions(null)

Solved: Including some code to help

    timelineRecyclerView.addOnScrollListener(object:RecyclerView.OnScrollListener() {
        override fun onScrollStateChanged(recyclerView:RecyclerView,
                                 newState:Int) {
            super.onScrollStateChanged(recyclerView, newState)

        }
        override fun onScrolled(recyclerView:RecyclerView, dx:Int, dy:Int) {
            super.onScrolled(recyclerView, dx, dy)

            currentTimelinePosition += dy

            Log.wtf("position", currentTimelinePosition.toString())
            //if the timeline position is on its start, allow swipe and refresh
            swipeLayout.isEnabled = currentTimelinePosition == 0
        }
    })
cesarsicas
  • 437
  • 1
  • 5
  • 17
  • I noticed you tagged with `pull-to-refresh`, what exactly does that have to do with this? There may be a better solution if we know more details. – advice Sep 02 '17 at 00:49
  • The issue behind is that the RecyclerView is inside a SwipeRefreshLayout. The swipe sometimes refresh on wrong position. I.E. when the list isn't on top. So i will allow refresh only when RecyclerView is on top – cesarsicas Sep 02 '17 at 11:05
  • Ah, I see. That's very odd, I don't have that issue within `RecyclerView`, but perhaps it is those larger items. – advice Sep 02 '17 at 20:34
  • As i found, this bug used to happen with old support versions (https://issuetracker.google.com/issues/37008297) and were fixed. Maybe its happening because im using StaggeredGridLayout. Idk, but thanks ! – cesarsicas Sep 03 '17 at 11:30

1 Answers1

9

You could create a RecyclerView.OnScrollListener, and keep track of how much it has scrolled.

For example, all you need to do is keep a variable, and update it with onScroll based on the vertical scroll amount.

class MyScrollListener() : RecyclerView.OnScrollListener() {

    var currentScrollPosition = 0

    override fun onScrolled(view: RecyclerView, dx: Int, dy: Int) {
        currentScrollPosition += dy

        if( currentScrollPosition == 0 ) {
            // We're at the top
        }
    }
}
advice
  • 5,778
  • 10
  • 33
  • 60
  • Thank you very much ! The only thing i had to adapt was that when the items of RecyclerView were updated, i had to restart the value of "current position" with 0. – cesarsicas Sep 03 '17 at 11:23
  • I try it, sometimes it scrolled to top, but currentScrollPosition is not equals 0 – PhongBM Jun 23 '22 at 03:10