简体   繁体   English

Android Paging 3 库,分组项

[英]Android Paging 3 library, grouping items

We are developing a chat application.我们正在开发一个聊天应用程序。 If messages contain images, and they come one after another, we in adapter group these images in one message.如果消息包含图像,并且它们一个接一个地出现,我们在适配器中将这些图像分组到一个消息中。

Question 1问题 1

In the database, and in the rest of API we have these messages separately, but in the adapter, we group them to display to the user as one big message with small icons of images inside.在数据库中,以及在 API 的 rest 中,我们将这些消息分开,但在适配器中,我们将它们分组以作为一条大消息显示给用户,其中包含图像的小图标。

We want to implement a paging library to have the possibility to scroll to any message in history.我们希望实现一个分页库,以便可以滚动到历史记录中的任何消息。 Currently, we are using a RemoteMediator<Int, Message> , which makes requests to our server, and gets messages.目前,我们正在使用RemoteMediator<Int, Message> ,它向我们的服务器发出请求并获取消息。 To store these messages we are using Room , which returns PagingSource<Int, Message> .为了存储这些消息,我们使用Room ,它返回PagingSource<Int, Message> To display messages we are using PagingDataAdapter为了显示消息,我们使用PagingDataAdapter

Everything works well, the question is how we can implement this grouping images logic in Paging 3?一切正常,问题是我们如何在 Paging 3 中实现这种分组图像逻辑? On what steps and when we can make this change and group message in one?我们可以在哪些步骤以及何时进行此更改并将消息组合在一起?

Example :示例

Message1_Text1 - Message2_Image1 - Message3_Image2 - Message4_Text2

After grouping, we should receive分组后,我们应该收到

Message1_Text1 - Message2_(Image1_Image2) - Message3_Text2

Question 2问题2

Depending on what messages are before and after we display messages differently, for example, we display profile icon only on the first message, all other messages from the same user displayed without a profile icon.根据beforeafter的消息,我们以不同的方式显示消息,例如,我们仅在第一条消息上显示配置文件图标,来自同一用户的所有其他消息显示时没有配置文件图标。

We can change our data in PagingData.map{} but we don't have any information about before and after items.我们可以在PagingData.map{}中更改我们的数据,但我们没有关于之前和之后项目的任何信息。 How can be this achieved?这怎么可能实现?

I am using paging with Jetpack Compose, I got a way to access the before and after item, making it possible to finish some grouping work.我正在使用 Jetpack Compose 进行分页,我有一种方法可以访问beforeafter的项目,从而可以完成一些分组工作。 This might be a dumb way, but it works:这可能是一种愚蠢的方式,但它有效:

Create a function like:创建一个 function 像:

fun <T : Any> LazyListScope.itemsBeforeAndAfter(
    items: LazyPagingItems<T>,
    key: ((item: T) -> Any)? = null,
    itemContent: @Composable LazyItemScope.(value: T?, beforeValue: T?, AfterValue: T?) -> Unit
) {
    items(
        count = items.itemCount,
        key = if (key == null) null else { index ->
            val item = items.peek(index)
            if (item == null) {
                index
            } else {
                key(item)
            }
        }
    ) { index ->
        val beforeValue = if (index - 1 < 0) null else items.peek(index - 1)
        val afterValue = if (index + 1 >= items.itemCount) null else items.peek(index + 1)
        itemContent(items[index], beforeValue, afterValue)
    }
}

Now we can access the values we need:现在我们可以访问我们需要的值:

val lazyPagingItems = pagingDataFlow.collectAsLazyPagingItems()
            LazyColumn() {
                itemsBeforeAndAfter(
                    items = lazyPagingItems,
                    key = { // your own key logic }) { value, beforeValue, AfterValue ->
                    // Do whatever you want here
                }
            }

In this way I broke the PagingPlaceholderKey , so don't forget to disable it:这样我打破了PagingPlaceholderKey ,所以不要忘记禁用它:

Pager(
    PagingConfig(
        pageSize = 20,
        enablePlaceholders = false
    )
) { // pagingSource }

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

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