android listviews: header and footer views

In my ListActivity, I need header and footer views (on the top and bottom of the list) to be used as previous page and next page buttons on my list, respectively, because I want to display only 20 items at a time.

I set my header and foot views by doing:


This works fine, but I need to dynamically remove and add these header and footer views, because some pages of my list may not have a next page button or a previous page button.

The problem is, I cannot call addHeaderView or addFooterView after I have called setListAdapter.

Is there a way around this?

Asked by: Roland289 | Posted: 24-01-2022

Answer 1

Why not just collapse the header and footer to zero height, or gray out the buttons (even better).

And the best user experience, in my opinion, would be to dynamically load more items when needed (i.e. upon scroll), like the built-in Gmail app does.

Answered by: Andrew474 | Posted: 25-02-2022

Answer 2

Yes, this is a bug or oversight in the ListView component. You can work around this by writing your own WrapperListAdapter that handles adding and removing fixed list items, but I can tell you it's not entirely straightforward to do.

Alternatively — and much easier — you could add a fixed component above or below the ListView where you place the next and previous buttons.

Answered by: Lenny719 | Posted: 25-02-2022

Answer 3

How about reset the adapter at every time you need to add header view, like this way:

ListView.FixedViewInfo headerInfo = getListView().new FixedViewInfo();
headerInfo.isSelectable=false ;
headerInfo.view = feedInfoView;
headerViewListAdapter = new HeaderViewListAdapter(headerInfos,null,adapter);

Answered by: Melissa647 | Posted: 25-02-2022

Similar questions

Android ListViews: Update headerView of a ListView

I have a ListView as HeaderView of my ListView. I want to update HeaderView of my ListView when any element is added in the ListView of header. How can we do this? Because I am updating the adapter of ListView in the header and notifyingDataSetChange as well as invalidating the view. Still it is not updated. Here is my piece of code: drawerList = (ListView) findViewById(; View Header...

Still can't find your answer? Check out these communities...

Android Google Support | Android Community | Android Community (Facebook) | Android