Android:ListView 中的自定义分隔符(甚至项目)取决于项目的内容
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一种实现,它完全按照您的描述进行操作。
那一个是 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 headerTextView
s andAdapter
s 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 yourTextView
headers at the right spot, and properly implement methods likegetViewTypeCount()
,getItemViewType()
,areAllItemsEnabled()
, andisEnabled()
.这里有更好的实现,灵感来自 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.