RecyclerView onScrolled not called when use scrollToPosition

Viewed 2996

I have RecyclerView.OnScrollListener like this

findViewById(R.id.button_scroll_to_position).setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        mLinearLayoutManager.scrollToPositionWithOffset(2,0);
    }
}); 

mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);
        Log.i("TAG", "scroll " + dy);
    }
});

ArrayList<String> list = new ArrayList<>();
for (int i = 0; i < 7; i++) {
    list.add("" + i);
}
mRecyclerView.setAdapter(new SimpleRVAdapter(list));
mLinearLayoutManager.scrollToPositionWithOffset(2,0);

However in some case onScrolled don't call event RecyclerView have scrolled (using scrollToPositionWithOffset).
I know the reason is when we use scrollToPositionWithOffset (or scrollToPosition) onScrolled only call when firstVisibleItem and lastVisibleItem change. You can see in the demo (if firstVisibleItem=2 and lastVisibleItem=5 => onScrolled not called, firstVisibleItem=2 and lastVisibleItem=6 => onScrolled will called)

Is there any trick or workaround way for always receive onScrolled when use scrollToPosition?

enter image description here

Any help or suggestion would be great appreciated.

3 Answers

I know I'm necroing thread but just in case someone runs into it like I did...

If you don't care about animation use:

recyclerView.smoothScrollToPosition(position);

It will trigger

OnScrollListener.onScrolled(RecyclerView recyclerView, int dx, int dy)
Related