AndroidPaging3:LoadType.APPEND返回null的远程键值
创始人
2024-10-09 11:32:00
0

在使用Paging 3进行分页时,我们可能会遇到在LoadType.APPEND时返回null的远程键值的问题。此时,我们需要检查以下几点:

  1. RemoteMediator对象中的load函数是否正确返回远程键值。在该函数中,我们需要获取最后一条加载数据的键值,并将其返回。

示例代码:

override suspend fun load(loadType: LoadType, state: PagingState): MediatorResult { val page = when (loadType) { LoadType.REFRESH -> { // This is triggered by a swipe-to-refresh. // Don't use remote key since refresh will clear all data. null } LoadType.PREPEND -> { // This is triggered when the user scrolls to the top of the list. // We should use the first available remote key, which is closest to the position. val remoteKeys = getClosestRemoteKeys(state) remoteKeys?.prevKey ?: return MediatorResult.Success(endOfPaginationReached = !remoteKeys.isNullOrEmpty()) } LoadType.APPEND -> { // This is triggered when the user scrolls to the end of the list. // We should use the last available remote key, which is closest to the position. val remoteKeys = getClosestRemoteKeys(state) remoteKeys?.nextKey ?: return MediatorResult.Success(endOfPaginationReached = !remoteKeys.isNullOrEmpty()) } }

// ...

val apiResponse = service.getData(page ?: 1)

val items = apiResponse.data
val endOfPaginationReached = items.isEmpty()

// ...

val keys = items.map {
    RemoteKeys(repoId = it.id, prevKey = page, nextKey = page)
}
db.remoteKeysDao().insertAll(keys)

return MediatorResult.Success(endOfPaginationReached = endOfPaginationReached)

}

  1. 在刷新(LoadType.REFRESH)时,不要使用远程键值(remote key)。因为刷新将清除所有数据,打破了该键与数据的对应关系。

  2. 在LoadType.PREPEND时,使用最接近位置的第一个可用远程键值(closest available remote key)。如果没有可用的键值,则返回success状态即可。

  3. 在LoadType.APPEND时,使用最接近位置的最后一个可用远程键值(closest available remote key)。如果没有可用的键值,则返回success状态即可。

相关内容

热门资讯

透视攻略"hhpok... 透视攻略"hhpoker免费透视脚本"详细辅助技巧教程(可以增加运气教程)这是由厦门游乐互动科技有限...
透视透视"德州来玩辅... 1、透视透视"德州来玩辅助器"详细辅助玩家教程(有人玩软件);详细教程。2、德州来玩辅助器透视辅助简...
透视了解"德普之星辅... 透视了解"德普之星辅助器app"详细辅助必胜教程(好友助力码方法)是一款可以让一直输的玩家,快速成为...
透视系统"aa po... 您好,aa poker辅助这款游戏可以开挂的,确实是有挂的,需要了解加微【136704302】很多玩...
透视系统"hhpok... 透视系统"hhpoker脚本"详细辅助玩家教你(私人局可以玩软件);支持多人共享记分板与复盘,通过邀...
透视有挂"wepok... 1、透视有挂"wepoker有脚本吗"详细辅助扑克教程(有辅助辅助)2、进入游戏-大厅左侧-新手福利...
透视app"hhpo... 透视app"hhpoker德州透视"详细辅助wepoke教程(私人局规律教程);建议优先通过hhpo...
透视神器"德州之星扫... 1、透视神器"德州之星扫描器"详细辅助解密教程(破解器有用攻略)2、进入游戏-大厅左侧-新手福利-激...
透视最新"wepok... 1、透视最新"wepoker透视脚本是什么"详细辅助分享教程(底牌透视透明);详细教程。2、wepo...
透视app"hh p... 透视app"hh poker辅助有用吗"详细辅助2025教程(可以免费玩透明);玩家必备必赢加哟《1...