How to generate a ListView with headers above some sections?

33,172

Solution 1

I got a solution. I don't know if it is the best one.

I use a custom adapter derived from ArrayAdapter for the list as described in this tutorial. In the adapter class I check if the position in the getView method is a normal row, then I inflate the row layout. If it is the first row from a new group I inflate a headline layout that is a normal row plus the group headline above it.

If you don't want to mix the header into one of your rows. Consider the following solution:

You can overwrite the two methods getItemViewType and getViewTypeCount. You now have a list that can display different rows. You need to check the expected view type for the item in the getView Method and inflate different layouts depending on it.

The list will handle the recycling for you in a way that it will return only correct recycle views to your getView method, this means if the recycleView is not null it can be used to display your current cell.

Solution 2

You can use my SectionedAdapter, if GPLv3 is acceptable (licensed that way due to some upstream code). You can use my MergeAdapter, if you need something more flexible and with a less-limiting license (Apache 2).

Solution 3

I think you might be looking for android.widget.ExpandableListView

http://developer.android.com/reference/android/widget/ExpandableListView.html

Share:
33,172
SHRISH M
Author by

SHRISH M

Updated on July 09, 2022

Comments

  • SHRISH M
    SHRISH M almost 2 years

    I want to generate a ListView that has some dividers between some of the entries, like it can be seen in some of the property sections. See the example below. I try to generate a List that consists of some textviews followed by one of the fancy dividers explaining the next part of the list and then again some text views. How can this be done? I thought about creating different views to add to the list? Is this the way to go?

    • Lance Nanek
      Lance Nanek over 14 years
      If you happen to be creating something very similar to the screenshot, screens like that can be made very easily using android.preference.PreferenceActivity. The dividers are created using the android:title attribute on the PreferenceCategory element in the preferences XML loaded by that. Not posting as an answer, though, since I'm not talking about using your own ListView in this case. It handles that for you.
  • SHRISH M
    SHRISH M over 14 years
    I think the expandable list is more like a tree view. As seen here:mylifewithandroid.blogspot.com/2008/05/expandable-lists‌​.html
  • Admin
    Admin over 14 years
    It's a two-level list. I though it could be coaxed into looking like your example by using the top-level entries as the big orange entries in your example. Now that I've given it more thought, I think that could end up being an ugly solution.
  • Artem Russakovskii
    Artem Russakovskii almost 13 years
    Just implemented everything I needed using MergeAdapter. It's exactly what I was looking for! Thanks, Mark (helpful as always).
  • SHRISH M
    SHRISH M about 12 years
    I guess that header and footer are really only used for header and footer views. Those views are above and under the list.