Java android listviews:页眉和页脚视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2017633/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 02:37:16  来源:igfitidea点击:

android listviews: header and footer views

javaandroidlistviewadapter

提问by jlim

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.

在我的 ListActivity 中,我需要将页眉和页脚视图(在列表的顶部和底部)分别用作列表中的上一页和下一页按钮,因为我想一次只显示 20 个项目。

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.

问题是,在调用 setListAdapter 后,我无法调用 addHeaderView 或 addFooterView。

Is there a way around this?

有没有解决的办法?

采纳答案by Roman Nurik

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.

在我看来,最好的用户体验是在需要时(即滚动时)动态加载更多项目,就像内置的 Gmail 应用程序那样。

回答by Christopher Orr

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

是的,这是ListView组件中的错误或疏忽。您可以通过编写自己的WrapperListAdapter处理添加和删除固定列表项的方法来解决此问题,但我可以告诉您,这并不完全简单。

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

或者 - 更容易 - 您可以ListView在放置下一个和上一个按钮的位置上方或下方添加一个固定组件。

回答by Jammy Lee

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);