I have one RecyclerView
and I added list of data into the RecyclerView
. I wanted to add more data in list, when last RecyclerView
item is visible on screen. After that I want to make a web service call and update the RecyclerView
data. How can I achieve this?
Any suggestions?
If someone stumble across this post this is a quick and simple tutorial on how to do it:
All you need to do is:
Then your loadMoreItems() should look something like this:
There are several ways to accomplish this, but one way would be to use your adapter's
onBindViewHolder
method:When the bind method is called with the last position of your data source, this is a good indication that the last cell is about to be displayed and a good time to trigger a reload call. You might need to add additional safeguards to the condition to make sure you're not already loading more data, but this should help you get started.
Another option would involve editing your
LayoutManager
. The idea here is to find the position of the last visible item. If that position is equal to the last item of your dataset, then you should trigger a reload. This call should be done in yourLayourManager's
onLayoutChildren
&scrollVerticallyBy
(assuming you use vertical scroll) methods:Same as before, you'll need to make sure you don't trigger a data load more than once, since it's possible this condition will occur more than once.
Hope this helps!