【发布时间】:2021-05-22 13:19:29
【问题描述】:
我目前正在使用新的 Jetpack compose UI 工具包,我非常喜欢它。我想不通的一件事是如何在由分页库填充的LazyColumn 中使用stickyHeaders。 documentation 中的非分页示例是:
val grouped = contacts.groupBy { it.firstName[0] }
fun ContactsList(grouped: Map<Char, List<Contact>>) {
LazyColumn {
grouped.forEach { (initial, contactsForInitial) ->
stickyHeader {
CharacterHeader(initial)
}
items(contactsForInitial) { contact ->
ContactListItem(contact)
}
}
}
}
由于我使用的是分页库,我无法使用groupedBy,所以我尝试在PagingData 上使用insertSeparators 函数并自己插入/创建标题(请忽略旧的Date 代码, 只是为了测试):
// On my flow
.insertSeparators { before, after ->
when {
before == null -> ListItem.HeaderItem(after?.workout?.time ?: 0)
after == null -> ListItem.HeaderItem(before.workout.time)
(Date(before.workout.time).day != Date(after.workout.time).day) ->
ListItem.HeaderItem(before.workout.time)
// Return null to avoid adding a separator between two items.
else -> null
}
}
// In my composeable
LazyColumn {
items(workoutItems) {
when(it) {
is ListItem.HeaderItem -> this@LazyColumn.stickyHeader { Header(it) }
is ListItem.SongItem -> WorkoutItem(it)
}
}
}
但这会生成我所有项目的列表,并且标题项目附加在末尾。任何想法在使用分页库时使用stickyHeader 函数的正确方法是什么?
【问题讨论】:
标签: android android-jetpack-compose android-paging-library