How can I create a list where when you reach the end of the list I am notified so I can load more items?
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- how to split a list into a given number of sub-lis
- Listening to outgoing sms not working android
Just wanted to contribute a solution that I used for my app.
It is also based on the
OnScrollListener
interface, but I found it to have a much better scrolling performance on low-end devices, since none of the visible/total count calculations are carried out during the scroll operations.ListFragment
orListActivity
implementOnScrollListener
Add the following methods to that class:
where
currentPage
is the page of your datasource that should be added to your list, andthreshold
is the number of list items (counted from the end) that should, if visible, trigger the loading process. If you setthreshold
to0
, for instance, the user has to scroll to the very end of the list in order to load more items.(optional) As you can see, the "load-more check" is only called when the user stops scrolling. To improve usability, you may inflate and add a loading indicator to the end of the list via
listView.addFooterView(yourFooterView)
. One example for such a footer view:(optional) Finally, remove that loading indicator by calling
listView.removeFooterView(yourFooterView)
if there are no more items or pages.You can detect end of the list with help of onScrollListener, working code is presented below:
Another way to do that (inside adapter) is as following:
Be aware that this method will be called many times, so you need to add another condition to block multiple calls of
addMoreData()
.When you add all elements to the list, please call notifyDataSetChanged() inside yours adapter to update the View (it should be run on UI thread - runOnUiThread)
Best solution so far that I have seen is in FastAdapter library for recycler views. It has a
EndlessRecyclerOnScrollListener
.Here is an example usage: EndlessScrollListActivity
Once I used it for endless scrolling list I have realised that the setup is a very robust. I'd definitely recommend it.