Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement adding footer on demand to ListView when loading data

Tags:

android

everyone. I want to show the footer like "Loading..." when user reaches bottom of the list. Now I only managed to determine when we are on the last element. Then trouble comes. We need to set up footer before setting adapter, we need to hide it afterwards. Does anyone have solution to this? May be this issue is already discussed, but I haven't found an answer.

like image 276
Konstantin Milyutin Avatar asked Mar 02 '11 17:03

Konstantin Milyutin


2 Answers

you can add the Footer before setting the adapter with

listView.addFooterView(yourFooterView, null, true);

*Note that the parameters should suit your objective.

Then you activity could implement OnScrollListener and on the method onScroll you can get the last item like this:

public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount){
    int lastItem = firstVisibleItem + visibleItemCount;
    if(lastItem == totalItemCoun){
        // fill your next set of items
    }
}

After the new set is filled, the footer will go to the bottom again, so you dont have to hide it. However, if you want to remove it you can use

lv.removeFooterView(yourFooterView);

I hope this helps

like image 144
raukodraug Avatar answered Sep 30 '22 14:09

raukodraug


you can, as proposed on this question, call the setFooterView before setting adapter and remove it just after, and then use setFooterView and removeFooterView in onScroll as needed. This works and I have tested it, in aforementioned page there are other solutions proposed as well.

like image 34
Lukasz 'Severiaan' Grela Avatar answered Sep 30 '22 13:09

Lukasz 'Severiaan' Grela