开发者

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

开发者 https://www.devze.com 2023-02-14 05:03 出处:网络
everyone. I want to show the footer like \"Loading...\" when user reaches bottom of the list. Now I on开发者_如何学Pythonly managed to determine when we are on the last element. Then trouble comes. We

everyone. I want to show the footer like "Loading..." when user reaches bottom of the list. Now I on开发者_如何学Pythonly 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.


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号