This came up before and I recommended a simple trick to implement this. I heard that it worked pretty well.

You will need your own list adapter, and two types of views, as Mark says.

Implementing per-group separators as self-contained list view items is tricky, as data item numbering will "slide" (the separators are not indexed by the same sequence of numbers as data items in the backing array).

The trick is to use a list item layout that includes both the regular list item view (for data), as well as a header that indicates the start of a new group.

This list item layout would then be used by the list adapter in the case where the data item is the start of its respective group. This only requires a simple lookup of the previous item in the data array, and then a test whether both the current and the previous item belong to the same group.

If both items belong to the same group, the adapter should return a layout that's just for displaying item data.

If the previous item belongs to a different group, it means that the current item is the start of a distinct new group. In this case, the adapter should return a layout that has both views for displaying data (as in the first case), as well as some kind of header / group indicator.

The advantage of this trick is that item numbering in the list view (including the number of items) stays the same as in the backing data array. So, you don't have to make a separate pass over data to determine where group boundaries are, how many of them exist, or perform any kind of mapping between item positions in the list view and in the data array.

-- Kostya


11.11.2010 15:26, Mark Murphy пишет:
On Thu, Nov 11, 2010 at 7:20 AM, Neilz<[email protected]>  wrote:
Hi all. I have created a ListView from a collection of objects, using
an ArrayAdaptor to set the items.

The objects used to populate the list have a NAME field, and are
sorted into alphabetical order, so ultimately the ListView is long
list of names.

I now want to make the list more user friendly. I'd like to add a
separator for each letter of the alphabet, saying "A", "B" etc. The
separator could be one of the list items (non-clickable) or just a
normal View.

I'm just not sure how I can manipulate the ListView to achieve this,
and would appreciate any help.
It's not a question of the ListView, but of the ListAdapter. Your
ListAdapter needs to:

-- Override getViewTypeCount() to return 2
-- Override getItemViewType() to return 0 for regular rows and 1 for headings
-- Override getView() (or newView() and bindView() for CursorAdapter)
and have it properly create the right rows and bind them

This is somewhat of a pain.

I have a MergeAdapter that can simplify it, but only where each
section is its own adapter, with plain Views being interspersed:

https://github.com/commonsguy/cwac-merge

Creating a HeaderCursorAdapter that injects headings based on some
rule (e.g., when the first letter of such-and-so column in the Cursor
changes) is on my list of 18,000 things to do. Though anyone is
welcome to go and beat me to writing it. :-)



--
Kostya Vasilyev -- WiFi Manager + pretty widget -- http://kmansoft.wordpress.com

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to