gpt4 book ai didi

android - 如何在 Android Jetpack Compose 中结合使用 LazyColumn stickyHeader 和 Paging?

转载 作者:行者123 更新时间:2023-12-04 23:54:09 32 4
gpt4 key购买 nike

我已经实现了LazyColumnPaging ,但我现在尝试添加 sticky headers也是。
stickyHeader() items() 中的功能不可用范围,所以我不明白这应该如何工作。

@Composable
fun MovieList(movies: Flow<PagingData<Movie>>) {
val lazyMovieItems: LazyPagingItems<Movie> = movies.collectAsLazyPagingItems()

LazyColumn {
// TODO: Add sticky headers
items(lazyMovieItems) { movie ->
MovieItem(movie = movie!!)
}
}
}
如何添加 stickyHeader年代?

最佳答案

@Composable
fun MovieList(movies: Flow<PagingData<Movie>>) {
val lazyMovieItems = movies.collectAsLazyPagingItems()

LazyColumn {
val itemCount = lazyMovieItems.itemCount
var lastCharacter: Char? = null

for (index in 0 until itemCount) {
// Gets item without notifying Paging of the item access,
// which would otherwise trigger page loads
val movie = lazyMovieItems.peek(index)
val character = movie?.name?.first()

if (movie !== null && character != lastCharacter) {
stickyHeader(key = character) {
MovieHeader(character)
}
}

item(key = movie?.id) {
// Gets item, triggering page loads if needed
val movieItem = lazyMovieItems[index]

Movie(movieItem)
}

lastCharacter = character
}
}
}

关于android - 如何在 Android Jetpack Compose 中结合使用 LazyColumn stickyHeader 和 Paging?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67702651/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com