Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RecyclerView scrollToPosition not trigger scrollListener

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.

like image 436
David Avatar asked Jan 07 '15 12:01

David


1 Answers

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:

recyclerView.scrollToPosition(recyclerAdapter.getItemCount() - 1); 

and now I've changed to this:

recyclerView.getLayoutManager().smoothScrollToPosition(recyclerView, null, recyclerAdapter.getItemCount() - 1); 

and it's working fine for me.

like image 110
luis.mazoni Avatar answered Oct 04 '22 18:10

luis.mazoni