Alf Alf - 3 months ago 5x
Android Question

Check whether a RecyclerView needs to scroll

I have a callback that is fired after a programmatic scroll to a certain item of a RecyclerView via

. The user taps on an item and the right item is scrolled to the top of the RecyclerView. I subclassed LinearLayoutManager to have it always snap to the top of the item.

This works in case the scroll event is fired, but when the RecyclerView is already in the right position, I don't get the
callback, as no scrolling occurs. Is there a way to get that event anyway? Like decide beforehand whether or not the RecyclerView needs to scroll or not?

Alf Alf

I found the following solution myself:

// get the view the user selected
View view = mLayoutManager.findViewByPosition(index);
// get top offset 
int offset = view.getTop();
if (offset == 0) { // the view is at the top of the scrollview
} else {
    // scrolling