Skip to content

Instantly share code, notes, and snippets.

@mtsahakis
Created February 21, 2016 21:44
Show Gist options
  • Save mtsahakis/abde764a54eba007483a to your computer and use it in GitHub Desktop.
Save mtsahakis/abde764a54eba007483a to your computer and use it in GitHub Desktop.
Check if an Android Recycler View is scrolled all the way down. Implementation is taken from a Fragment.
private RecyclerView mRecyclerView;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_view, container, false);
mRecyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
// ommiting other recycler view set up, such as adapter and Layout manager set up ..
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if (isLastItemDisplaying()) {
// do something cool like add more items
}
}
});
return view;
}
private boolean isLastItemDisplaying() {
RecyclerView.Adapter adapter = mRecyclerView.getAdapter();
if (adapter != null && adapter.getItemCount() != 0) {
int lastVisibleItemPosition = ((LinearLayoutManager) mRecyclerView.getLayoutManager()).findLastCompletelyVisibleItemPosition();
if (lastVisibleItemPosition != RecyclerView.NO_POSITION && lastVisibleItemPosition == adapter.getItemCount() - 1) {
return true;
}
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment