Alf Alf - 1 year ago 96
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
Answer Source

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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download