/EDIT: I’ve created a library (and demo): https://github.com/moagrius/EndlessRecyclerView
Most “endless” RecyclerViews wait until the last item is visible, then load more data, which is often fine and probably very more efficient strategy. However, sometimes we need to be a little more responsive, and try not to let the scroll come to a complete stop. Here’s how:
Add an onScrollListener, that in onScrolled determines how far we are from the bottom of the (emulated) scroll, and if that number is within an established threshold, loads more items into the RecyclerView.Adapter (and kick off a network request, if necessary). Add null values, and when displaying these null values in the Adapter, showed loading graphics, then replace the null values with real data when it comes in.
Unfortunately, it’s not quite that straightforward – the RecyclerView has no idea how tall it’s content it, nor does it use expected methods like getScrollY… However, there are some public methods the RecyclerView uses to compute it’s emulated scrolling behavior that we can leverage…
RecyclerView.computeVerticalScrollRange will return an approximation of the content height, by averaging the height of all visible views and multiplying that by the total number of items in the Adapter.
RecyclerView.computeVerticalScrollOffset() is roughly equivalent to what you’d expect for getScrollY (which will return 0).
To compute distance from bottom, do something like in onScrolled:
int distanceFromBottom = recyclerView.computeVerticalScrollRange() - recyclerView.computeVerticalScrollOffset() - recyclerView.getHeight();
If that is less than some arbitrary threshold (say, 3 screen heights), then add some null values to the adapter, kick off a network request, and notifyDataSetChanged.