android listviews:页眉和页脚视图
在我的 ListActivity 中,我需要页眉和页脚视图(位于列表的顶部和底部)分别用作列表上的上一页和下一页按钮,因为我只想一次仅显示 20 个项目。
我通过以下方式设置页眉和页脚视图:
getListView().addHeaderView(myHeaderView);
getListView().addFooterView(myFooterView);
setListAdapter(adapter);
这工作正常,但我需要动态删除和添加这些页眉和页脚视图,因为列表中的某些页面可能没有下一页按钮或上一页按钮。
问题是,在调用 setListAdapter 后,我无法调用 addHeaderView 或 addFooterView。
有办法解决这个问题吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为什么不将页眉和页脚折叠到零高度,或者使按钮变灰(更好)。
在我看来,最好的用户体验是在需要时(即滚动时)动态加载更多项目,就像内置的 Gmail 应用程序一样。
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.
是的,这是
ListView
组件中的错误或疏忽。您可以通过编写自己的WrapperListAdapter
来解决此问题,该WrapperListAdapter
可以处理添加和删除固定列表项,但我可以告诉您,这并不完全简单。或者,更简单的是,您可以在放置下一个和上一个按钮的
ListView
上方或下方添加一个固定组件。Yes, this is a bug or oversight in the
ListView
component. You can work around this by writing your ownWrapperListAdapter
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.每次需要添加标题视图时重置适配器怎么样,如下所示:
How about reset the adapter at every time you need to add header view, like this way: