Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

getListView().addHeaderView(myHeaderView);
getListView().addFooterView(myFooterView);
setListAdapter(adapter);

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?

like image 812
jlim Avatar asked Jan 07 '10 01:01

jlim


3 Answers

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.

like image 117
Roman Nurik Avatar answered Nov 07 '22 13:11

Roman Nurik


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.

like image 28
Christopher Orr Avatar answered Nov 07 '22 15:11

Christopher Orr


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;
headerInfos.add(headerInfo);
headerViewListAdapter = new HeaderViewListAdapter(headerInfos,null,adapter);
getListView().setAdapter(headerViewListAdapter);
like image 45
Jammy Lee Avatar answered Nov 07 '22 13:11

Jammy Lee