Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a sectionised ListView

enter image description here

I have been searching all day on how to make these sections in a ListView. Haven't found anything yet. I have been through many blogs and most of them talk about the approach CommonsWare takes (i.e. SectionAdapter) but the SectionAdapter.java is nowhere to be seen on his GitHub repo.

How can this be made? (i.e. the part marked A. I am not trying to make a Preferences list. Something more on the lines of a Contact List)

like image 511
prometheuspk Avatar asked Feb 28 '26 02:02

prometheuspk


1 Answers

I struggled a lot on this. There are a number of ways to do this. The one I found simplest and which I recommend is using separator view in your list item layout (the one you inflate in get view) and change its visibility based on whether on not there should be a header. I use something like this:

<TextView
    android:id="@+id/separator"
    android:layout_width="fill_parent"
    android:visibility="gone"
    android:layout_height="wrap_content" />

I found this much simpler than the other adapter. I just kept track of where I wanted to have a separator using a variable and based on that I setVisibility(View.VISIBLE) in my getView().

like image 152
Saad Farooq Avatar answered Mar 02 '26 16:03

Saad Farooq