How Can I add Headers for sections in my item list...
# android
i
How Can I add Headers for sections in my item list? I sorted and grouped by
Category
items in my list and now I want to add Header for each single section.
Copy code
model.apply {
    val archiveList: MutableList<ArchiveItem> = mutableListOf()
    val itemList: MutableList<ListItem> = mutableListOf()

    archives.map {
        archiveList.add(
            ArchiveItem(
                category = it.translateToCategory(it.domain.toString()),
                title = it.title,
                agreementNumber = it.agreementNumber,
                startDate = it.startDate
            )
        )
        archiveList
            .sortedBy { it.category }
            .groupBy { it.category }
            .apply { 
                itemList.add(ArchiveListHeader(title = "My category"))
            }
    }

    itemList.addAll(archiveList)

    items = itemList

}
­čśÂ 5
Is it possible to simplify that code?
Copy code
model.apply {
    items =  archives.groupBy { it.domain }
        .map {
            listOf(ArchiveListHeader(it.key ?: ""))
                .plus(
                it.value.map {
                    ArchiveItem(
                        category = it.translateToCategory(it.domain.toString()),
                        title = it.title,
                        agreementNumber = it.agreementNumber,
                        startDate = it.startDate
                    )
                }
            )
        }
        .flatten()
}
105 Views