This seems a question with many answers already; however, I can't find a common approach to this.
I'm trying to add data to a ListView
when the bottom is reached; data are retrieved from Internet using an AsyncTask
. ListView
already has an adapter attached to it.
So, searching for a good way to accomplish this, I arrived at two different approaches.
- The first one, the
onScrollStateChanged()
approach, is basically related to this page. However, it use parameters that don't have correspondence in the actual API. At the same time, this link use the right API, but I don't know if in the right way. I tried with the first of the two links, but it's kind of meh. Debugging the app,diff
values vary a lot and I don't know how to correctly interpet the expression. Also, I don't know how to fix an offset from which I can start to retrieving data; I mean, I'd like to execute code not when I'm about to reach the bottom, but just before I reach it. Also, sometimes it get called even if we scroll to the top. - The second one is the
onScroll()
approach, that is used in this answer or, in a different way, in this code. I tried adapting the last of the two codes, but it cause many problems and the data are loaded even if we don't reach the bottom of the list.
So, what approach is best? When and why should I prefer one or the other? Which of the two should I use in my case?
getView
check if it's the last item in your dataset, this means you've scrolled to the end of the list. – SacramentgetView
which is passed a position parameter. Check the position against the size of your adapter to see if you've reached the end. – SacramentonScrollStateChanged()
, but still I'm not too sure about it. – FaefaecesgetView
is executed whenever an item in your ListView is created. As you scroll it is called again and again. The point of my comment is to suggest a third approach which is to execute your method to load more data within this method. But only when the position parameter indicates that you've reached the end of the dataset. – Sacrament