I'm using RecyclerView, with ScrollListener:
mRecyclerView.setOnScrollListener(new RecyclerView.OnScrollListener()
{
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState)
{
super.onScrollStateChanged(recyclerView, newState);
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy)
{
super.onScrolled(recyclerView, dx, dy);
// Do my logic
}
});
When I scroll with the finger, the scroll listener triggered fine.
But when I scroll progrematically, like that:
mRecyclerView.scrollToPosition(LAST_POSITION);
The scroll listener is not triggered.
Muito legal seu conselho.
To explicitly trigger onScrollListener over recycler view use:
This is a known issue. It is caused by the fact that RecyclerView does not know how LayoutManager will handle the scroll or if it will handle it at all.
In the next release, you'll receive a call to
onScrolled
if first and or last child position changes after a layout (which is generally the result of calling scroll to position).Unfortunately, dx and dy will be 0 because RecyclerView does not really know how much layout manager did scroll to handle
scrollTo
request. Alternatively, you can also use the the ViewTreeObserver's scroll callback.I could not use the smoothScrollToPosition (we had some problems with it in the past) and depending on the position of the first item seemed unreliable, so I ended up using yigit's answer (it has been released already), unfortunately it didn't always work (I am not sure why).
So I ended up using a scroll listener I've added previously to the RecyclerView, sadly I cannot find the origin of this solution.
I don't think you should use this listener if you need a constant one, luckily I need to listen only at specific times.
Override the RecyclerView:
Usage:
It's a bit rudimentary, but you could try this Subclass of RecyclerView:
I've faced the same issue and found a workaround, which is to use the smoothScrollToPosition in layout manager. This method triggers the scroll listener. So basically this is what I use to have:
and now I've changed to this:
and it's working fine for me.