Last active
September 21, 2018 18:19
-
-
Save sjudd/0776594543c4b6c30d38 to your computer and use it in GitHub Desktop.
RecyclerView scroll listener to AbsListView scroll listener
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Line 22 should be:
lastVisibleCount = visibleCount