Android ListView Section Header Example Group By Header

I will explain about android listview section header with example in this article.

You will make a listview with a specific header between it’s row items.

Read how to make recyclerview with section header.

What is Section Header ?

Section header means that a header among listview items to sort them with particular category.

For example, you are making listview with county names. Now countries can be categorized by continents.

So, you will make the first row as a section header which have continent name like Asia. Then in the second, third row you will write Asian countries like India, Chine etc.

After Asia, again you will make listview row as a section header and will name it Europe and further this process continues.

In this example, we will group listview by vehicle type as a header. We will create custom view for header which is different than the normal listview item.

So let us follow all the below steps carefully.

Step 1. Layout Resources

Different layout for header and normal rows requires different layout xml files. Hence, we will create two layout resources files.

Make two xml files res->layout directory.

Give a name lv_child.xml to first file and lv_header.xml to second xml file.

Add below code in lv_child.xml

Above file will create the layout for normal listview row items.

Copy following code in lv_header.xml

This code will generate the layout for header rows.

Step 2. Separate Classes

We need to create two java classes for both normal and header rows.

This class will work as a general model class. So make two new java classes and name them HeaderModel.java and ChildModel.java respectively.

Write down the below code in HeaderModel.java

isHeader() method will tell compiler whether the listview row is a header row or a normal row.

setheader(String header) method will set the string as a header title which passes as a parameter.

getName() method will return a header text in a string format.

Now add the below code in ChildModel.java

Similar methods are used here as HeaderModel.java class. Only difference is that get method will return normal row item text and set method will set the text as a row item name.

Step 3. Custom Adapter

As always, every listview requires an adapter class which will provide the data to populate the listview rows.

Make a new java class named CustomeAdapter.java and add below code in it.

Here I am populating listview with the help of the listItemArraylist.

listItemArraylist is created in the Main activity and passed as a parameter to the constructor of this adapter class.

Hence, let us first understand the Main Activity so it would be easier to understand adapter class.

Step 4. Final Codes

Now the final task is to write Main Activity code.

Add below code in activity_main.xml

Write the following code in MainActivity.java file

Diving Deep In Main Activity

Consider below code

First string array vehicleTypes will provide the header names.

Secong string array childnames will provide the text names for normal listview rows.

listItemArraylist is defined as below

Here ListItem is an Interface which in implemented by both model classes :  HeaderModel.java and ChildModel.java.

So the methods present is the ListItem interface are overriden in HeaderModel.java and ChildModel.java.

Following is the code for the Interface ListItem

As we have seen earlier in Step 2 that both the methods of Interface ListItem are overriden in HeaderModel.java and ChildModel.java.

Now we will create data structure using populateList() method.

Below is the source code for the populateList() method.

Here I have make four child rows for each header.

Listview will start with the first header then four rows will be it’s child items. The fifth row will be second header and then four rows will be the child row for second header and it continues in this fashion.

So the first, sixth, eleventh and sixteenth row of the listview will be header rows.

Hence in the for loop if condition will be true for four times.

When the if condition is true, object of the HeaderModel will be added to the listIemArrayList

In all other for loop iterations, object of the ChildModel will be added to the listIemArrayList.

After completion of for loop, listIemArrayList is passed to the adapter constructor.

Now let’s see the adapter code.

Understanding Adapter

Read the below code for getView() method

This method will fetch the data from listIemArrayList and will populate the listview using that data.

Look at the following codes

It will check for the header object by using isHeader() method.

If isHeader() method returns true then compiler will inflate the lv_header.xml file as a listview row layout.

Otherwise it will inflate the lv_child.xml

After checking for above conditions and inflating the layout file compiler will set the text pf the listview row using the below line

Last Views

After all of this hard work, you should get the listview like below video

Other Resources

Download Source Code for Android ListView Section Header Example

Hardik Parsania

I am mobile app developer. I am always passionate about solving complex and real world problems.I have developed many apps with great interest and superior logical skills.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *