android listviews:页眉和页脚视图

发布于 2024-08-16 21:40:34 字数 399 浏览 1 评论 0原文

在我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

执着的年纪 2024-08-23 21:40:34

为什么不将页眉和页脚折叠到零高度,或者使按钮变灰(更好)。

在我看来,最好的用户体验是在需要时(即滚动时)动态加载更多项目,就像内置的 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.

鱼窥荷 2024-08-23 21:40:34

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

或者,更简单的是,您可以在放置下一个和上一个按钮的 ListView 上方或下方添加一个固定组件。

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.

懷念過去 2024-08-23 21:40:34

每次需要添加标题视图时重置适配器怎么样,如下所示:

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

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文