Android: custom separator (or even item) in ListView depening on content of item

非 Y 不嫁゛ 提交于 2019-11-26 12:38:33

问题


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?


回答1:


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().




回答2:


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.



来源:https://stackoverflow.com/questions/1606320/android-custom-separator-or-even-item-in-listview-depening-on-content-of-item

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!