Android:ListView 中的自定义分隔符(甚至项目)取决于项目的内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1606320/
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
Android: custom separator (or even item) in ListView depening on content of item
提问by znq
I've a ListView with items containing information about places with a ratingand the distanceto the current location.
我有一个 ListView ,其中包含有关带有评级的地点的信息以及到当前位置的距离的项目。
The items are sorted into groups:
这些项目被分为几组:
- Group 1: within 500m
- Group 2: 500m - 1km
- Group 3: 1km - 1.5km
- ...
- 第一组:500m以内
- 第 2 组:500m - 1km
- 第 3 组:1 公里 - 1.5 公里
- ...
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.
现在,我通过我的自定义适配器( 的扩展名BaseAdapter
)将这些项目放入 中ListView
,效果很好。
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 - 1kmfollowed by all the ListView
items in that group.
但是,我想做的是在每个组的每个第一项之前放置一个分隔符。这个分隔符可以是一个TextView
说法,例如500m - 1km后跟ListView
该组中的所有项目。
Any idea on how to realize this?
关于如何实现这一点的任何想法?
回答by CommonsWare
Here is one implementationthat does exactly what you describe.
这是一种完全符合您描述的实现。
That one is GPLv3, because it is derived from this implementation, which was GPLv3.
那个是 GPLv3,因为它派生自这个实现,也就是 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 TextView
s and Adapter
s containing each section's worth of content.
您也可以MergeAdapter
为此使用 my ,它的优点是 Apache License 2.0。只需将包含每个部分内容的一组交替的 header TextView
s 和Adapter
s交给它。
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()
.
或者,您可以查看所有这些并将它们的行为纳入您现有的Adapter
类。关键是要回报你TextView
在正确的位置标头和正确实施类似的方法getViewTypeCount()
,getItemViewType()
,areAllItemsEnabled()
,和isEnabled()
。
回答by Jarek Potiuk
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.
这是更好的实现,灵感来自 iOS 部分列表视图:http: //code.google.com/p/android-section-list/。关于它的 nie 事情是,即使您向下滚动该部分,该部分标题仍然可见。