Android:ListView 中的自定义分隔符(甚至项目)取决于项目的内容
我有一个 ListView,其中包含有关具有评级的地点信息以及到当前位置的距离的项目.
I've a ListView with items containing information about places with a rating and the distance to the current location.
项目按组分类:
- 第 1 组:500m 以内
- 第 2 组:500m - 1km
- 第 3 组:1 公里 - 1.5 公里
- ...
对于这些组,项目按评分排序.
Withing these groups the items are sorted by their rating.
现在我通过我的自定义适配器(BaseAdapter
的扩展)将这些项目放到 ListView
中,效果很好.
Now I put out these items via my custom adapter (extension of BaseAdapter
) into the ListView
, which works perfectly.
但是,我想做的是在每个组的每个第一项之前放置一个分隔符.这个分隔符可以是一个 TextView
说例如500m - 1km 之后是该组中的所有 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 - 1km followed by all the ListView
items in that group.
知道如何实现这一点吗?
Any idea on how to realize this?
这是一种实现,完全符合您的描述.
那个是 GPLv3,因为它派生自 这个实现,这是 GPLv3.
That one is GPLv3, because it is derived from this implementation, which was GPLv3.
你也可以使用我的 MergeAdapter
,它有Apache License 2.0 的优势.只需将一组交替的标头 TextView
和 Adapter
包含每个部分的内容.
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.
或者,您可以查看所有这些并将它们的行为滚动到您现有的 Adapter
类中.诀窍是在正确的位置返回您的 TextView
标头,并正确实施诸如 getViewTypeCount()
、getItemViewType()
、 之类的方法areAllItemsEnabled()
和 isEnabled()
.
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()
.