How to draw a section header in android listview just like the ios's UITableview? How to draw a section header in android listview just like the ios's UITableview? android android

How to draw a section header in android listview just like the ios's UITableview?


Create a HEADER LAYOUT in your List Item Layout. We use the VISIBILITY option to show and hide the HEADER LAYOUT. This will act like a section header.

In the adapters "getView" method, check the first letter of the "name field (in the case you are showing in accords to Name)" with the first letter of the previous LIST ITEMS "name field". If it macthes hide the HEADER LAYOUT (with a text view) else show the HEADER LAYOUT with the Header Text showing the first letter of the Name Field.

Here is the code

String nameFirstLetter = "A"; // Declare this globally, not inside the getView.

// Inside the getViewString nameF = Name.slice(0,1);

 if(!nameFirstLetter.equals(nameF )){        nameFirstLetter = nameF ;                   holder.headerText.setText(nameFirstLetter );        holder.headerLayout.setVisibility(View.VISIBLE);    }else{        holder.headerLayout.setVisibility(View.GONE);    }

This is the easiest method to show section header in Android List view, but it wont work like Iphone section header, ie. The section header hide along with other list items when we scroll up/down.


I found some examples in Android can resolve this issuse:Please find the example about PinnedHeaderListViewPinnedHeaderListView Example