Android:ListView 中的自定义分隔符(甚至项目)取决于项目的内容

发布于 2024-08-08 08:49:52 字数 469 浏览 1 评论 0原文

我有一个 ListView,其中的项目包含有关具有评级的地点以及到当前位置的距离的信息。

这些项目被分为几组:

  • 第 1 组:500m 以内
  • 第 2 组:500m - 1km
  • 第 3 组:1km - 1.5km
  • ...

在这些组中,项目按其评级排序。

现在,我通过自定义适配器(BaseAdapter 的扩展)将这些项目放入 ListView 中,效果非常好。

但是,我想做的是在每组的每个第一项之前放置一个分隔符。此分隔符可以是一个 TextView,例如 500m - 1km,后跟该组中的所有 ListView 项目。

关于如何实现这一点有什么想法吗?

I've a ListView with items containing information about places with a rating and the distance to the current location.

The items are sorted into groups:

  • Group 1: within 500m
  • Group 2: 500m - 1km
  • Group 3: 1km - 1.5km
  • ...

Withing these groups the items are sorted by their rating.

Now I put out these items via my custom adapter (extension of BaseAdapter) into the ListView, which works perfectly.

However, what I'd like to do is to put a separator before the each first item of each group. This separator can be a TextView saying e.g. 500m - 1km followed by all the ListView items in that group.

Any idea on how to realize this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

不必了 2024-08-15 08:49:52

这是一种实现,它完全按照您的描述进行操作。

那一个是 GPLv3,因为它源自 这个实现,是 GPLv3。

您还可以使用我的 MergeAdapter 来实现此目的,其优点是Apache 许可证 2.0。只需向其提供一组交替的标头 TextView 和包含每个部分内容的 Adapter 即可。

或者,您可以查看所有这些并将它们的行为滚动到现有的 Adapter 类中。诀窍是在正确的位置返回 TextView 标头,并正确实现 getViewTypeCount()getItemViewType()等方法areAllItemsEnabled() 和 isEnabled()。

Here is one implementation that does exactly what you describe.

That one is GPLv3, because it is derived from this implementation, which was GPLv3.

You can also use my MergeAdapter for this, which has the advantage of being Apache License 2.0. Just hand it an alternating set of header TextViews and Adapters containing each section's worth of content.

Or, you can peek at all of these and roll their behaviors into your existing Adapter class. The trick is to return your TextView headers at the right spot, and properly implement methods like getViewTypeCount(), getItemViewType(), areAllItemsEnabled(), and isEnabled().

心房敞 2024-08-15 08:49:52

这里有更好的实现,灵感来自 iOS 部分列表视图:http://code.google .com/p/android-section-list/。最重要的是,即使向下滚动该部分,该部分标题仍然可见。

Here is even nicer implementation, inspired from iOS section list view : http://code.google.com/p/android-section-list/. The nie thing about it is that the section header remains visible even if you scroll the section down.

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