views:

116

answers:

1

I am developing an application with a large number of elements that must be ordered alphabetically, and I'd like it to have the same look and feel as android's contact list, That is

[Letter]
<contact>
<contact>
[Letter]
<contact>
<contact>
<contact>

etc.

Which is the best way to achieve this same layout? I've seen several tutorials concerning scrollable lists, but this is a bit different. I've looked a bit through android's source code, but if anyone has the answer, it would save me a lot of time.

Thanks in advance.

+1  A: 

You could use my MergeAdapter for that, either directly or as a basis for creating your own adapter that gives you your desired look.

In a nutshell, you need to create a ListAdapter that handles both your contacts and your headings. You will wind up using different views for those, most likely, and there are methods you override on ListAdapter to each Android about those. You might also elect to say that the headings are not "enabled", meaning they will not respond to selection or click events.

CommonsWare
Thanks a lot.Are you aware of any major performance improvement that could be done to your code? It is very similar to this other one I was looking at: http://jsharkey.org/blog/2008/08/18/separating-lists-with-headers-in-android-09/I was afraid of using it because it was developed when 0.9 was out, and I feared it's use/performance had changed a lot, but yours is almost a year older and follows the same idea.PS: I can't vote yet :(
Maragues
"Are you aware of any major performance improvement that could be done to your code?" I am not aware of it having a performance problem. It is newer than Mr. Sharkey's, but his probably still works. Note, though, that his is GPLv3, which may or may not be a suitable license for your project. Mine is Apache 2.0 licensed.
CommonsWare
Sorry if I expressed myself wrong, I wasn't criticizing your work, just wondering if you knew of any possible improvement but hadn't developed it yet.thanks again for the answer and for the advice on the license differences, I'll check the conditions.
Maragues
"just wondering if you knew of any possible improvement but hadn't developed it yet." Well, there's not much to the adapter. I can see some variations on `MergeAdapter` that could offer a bit better performance via caching some calculations, at the cost of not being able to dynamically update the contents (e.g., calling `requery()` on one of the child adapters).
CommonsWare