简体   繁体   English

带有分页 3 jetpack 组合的 Sticky Headers

[英]Sticky Headers with paging 3 jetpack compose

I am trying to implement sticky headers as shown in the example here , with paging using Jetpack compose LazyColumn as shown bellow :我正在尝试实现粘性标头,如此处的示例所示,使用 Jetpack compose LazyColumn 进行分页,如下所示:

LazyColumn(
        modifier = modifier.fillMaxSize(),
        contentPadding = PaddingValues(20.dp),
        horizontalAlignment = Alignment.CenterHorizontally,
    ) {

        // TODO (HADI) enhance later
        notifications.itemSnapshotList.items.groupBy { extractNotificationHeader(it) }
            .forEach { (header, messages) ->
                stickyHeader {
                    NotificationHeader(
                        modifier = Modifier.fillMaxWidth(),
                        value = header
                    )
                }

                items(
                    items = messages,
                    key = { message -> message.notificationId },
                ) {
                    NotificationMessage(
                        notification = it,
                        onNotificationClick = { onNotificationClick(it) }
                    )
                }
            }
}

but I get requests from back-end with only 2 pages as I am using code like :但是当我使用如下代码时,我从后端收到的请求只有 2 页:

// API service method 
@GET("notifications")
suspend fun getNotifications(
    @Query("page") page: Int,
    @Query("page_size") pageSize: Int = 30
): GenericResponse<NotificationsJson>


// data source impl
class NotificationDataSourceImpl @Inject constructor(private val notificationApiService: NotificationApiService) : NotificationDataSource {

override val invalidatingFactory = InvalidatingPagingSourceFactory {
    NotificationMediator(notificationApiService)
}
override fun loadNotifications(): Flow<PagingData<NotificationCenterMessage>> {
    return Pager(
        config = PagingConfig(
            pageSize = 30,
            enablePlaceholders = false,
        ),
        pagingSourceFactory = invalidatingFactory
    ).flow
}
 override fun invalidateNotifications() {
    invalidatingFactory.invalidate()
}
}

the problem is I have 7 more pages, but they are not loaded when I reach the bottom position of the lazy column items, I've tried to use only items without looping and sticky headers and it's working fine.问题是我还有 7 个页面,但是当我到达惰性列项目的底部位置时它们没有加载,我尝试只使用没有循环和粘性标题的项目,它工作正常。 I've looked up for many solutions like here , but nothing worked as required.我已经查找了许多像这里这样的解决方案,但没有按要求工作。 Also I am trying to mark notification at specific position when the user click it to be read, but when I use invalidateNotifications() the page reload and I lose the position even when I use animateScrollToItem() with rememberLazyListState()此外,当用户单击要阅读的通知时,我试图在特定位置标记通知,但是当我使用invalidateNotifications()时,页面会重新加载,即使我将animateScrollToItem()rememberLazyListState( ) 一起使用,我也会丢失位置

Loads are triggered by the LazyPagingItems.get method and you are not calling it anywhere.加载由LazyPagingItems.get方法触发,您不会在任何地方调用它。 Modify your extractNotificationHeader function to return indices of the header and messages instead of the models and then inside stickyHeader and items , use them like notifications.get(headerIndex) .修改您的extractNotificationHeader函数以返回标头和消息的索引而不是模型,然后在stickyHeaderitems中,像notifications.get(headerIndex)一样使用它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM