Skip to content

Instantly share code, notes, and snippets.

@sjudd
Last active September 21, 2018 18:19
Show Gist options
  • Save sjudd/0776594543c4b6c30d38 to your computer and use it in GitHub Desktop.
Save sjudd/0776594543c4b6c30d38 to your computer and use it in GitHub Desktop.
RecyclerView scroll listener to AbsListView scroll listener
public class RecyclerToListViewScrollListener extends RecyclerView.OnScrollListener {
private final AbsListView.OnScrollListener scrollListener;
private int lastFirstVisible = -1;
private int lastVisibleCount = -1;
private int lastItemCount = -1;
public RecyclerToListViewScrollListener(AbsListView.OnScrollListener scrollListener) {
this.scrollListener = scrollListener;
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
int firstVisible = layoutManager.findFirstVisibleItemPosition();
int visibleCount = Math.abs(firstVisible - layoutManager.findLastVisibleItemPosition());
int itemCount = recyclerView.getAdapter().getItemCount();
if (firstVisible != lastFirstVisible || visibleCount != lastVisibleCount
|| itemCount != lastItemCount) {
scrollListener.onScroll(null, firstVisible, visibleCount, itemCount);
lastFirstVisible = firstVisible;
lastVisibleCount = visibleCount;
lastItemCount = itemCount;
}
}
}
@liorgonnen
Copy link

Line 22 should be:
lastVisibleCount = visibleCount

@sjudd
Copy link
Author

sjudd commented May 30, 2015

Yup thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment