我正在使用最新的 Jetpack 库。
Pagination3 版本:3.0.0-alpha05
房间版本:2.3.0-alpha02
我的实体有 Long asPrimaryKey和 Room 可以PagingSource为其他Int类型生成。
error: For now, Room only supports PagingSource with Key of type Int.
public abstract androidx.paging.PagingSource<java.lang.Long, com.example.myEntity>` getPagingSource();
Run Code Online (Sandbox Code Playgroud)
因此,我尝试实现我的 custom PagingSource,就像文档建议的那样。
问题是Data Refresh,因为 Room 生成的代码处理数据刷新,而我的代码无法处理这种情况。
任何建议如何实现自定义PagingSource的Room,也负责处理Data Refresh?
android android-room android-paging android-paging-library android-paging-3
我在我的 android 项目中使用 androidx 分页库的 alpha 版本。它曾经工作得很好,但今天我的 Android 工作室开始显示有关PagedListAdapter类的弃用警告。我在google上搜索,也查看了android开发者网站上的官方文档,但没有找到任何东西。
我正在使用以下依赖项:
def paging_version = "3.0.0-alpha06"
implementation "androidx.paging:paging-runtime:$paging_version" //pagination
Run Code Online (Sandbox Code Playgroud)
这只是 Android 工作室的一个小故障还是已经被弃用了?
我想实现 jetpack 分页库。
在从 api(使用 Retrofit2 和协程)获得响应后,一切似乎都在工作,android 分页回调正在调用他的 onResult 接口。
但是当接收到数据时,它并没有将它的值返回给主要活动中的 livedata。
解决办法是什么 ?
视图模型
fun getListData(): LiveData<PagedList<DataListModel>> {
return initializedPagedListBuilder(PagingConfig.config).build()
}
private fun initializedPagedListBuilder(config: PagedList.Config): LivePagedListBuilder<Int, DataListModel> {
val dataSourceFactory = object : DataSource.Factory<Int, DataListModel>() {
override fun create(): DataSource<Int, DataListModel> {
return ListDataPageSource()
}
}
return LivePagedListBuilder<Int, DataListModel>(dataSourceFactory, config)
}
Run Code Online (Sandbox Code Playgroud)
列表数据页源
class ListDataPageSource : PageKeyedDataSource<Int, DataListModel>() {
override fun loadInitial(
params: LoadInitialParams<Int>,
callback: LoadInitialCallback<Int, DataListModel>
) {
val apiCall = RetrofitApi.getApiList("http://jsonplaceholder.typicode.com/").create(
MoviesApi::class.java
)
CoroutineScope(Dispatchers.IO).launch {
val response = …Run Code Online (Sandbox Code Playgroud) 我想实现分页以在我的应用程序中显示我想要的项目视图的大部分列表。这就是为什么我选择使用Google新发布的分页库,即Paging库3。我在我的应用程序中使用Rxjava、Livedata和ViewModel。实现分页库后,我遇到了一个奇怪的问题。当我调用获取列表的方法时,它会一次又一次地调用,并且不会停止调用。事实上,尽管我没有滚动列表,但它会自动增加页码。这是我尝试过的代码
JobListRestApi.kt
interface JobListRestApi {
@GET("job/list")
fun getJobResponse(
@Query("page") pageNumber: Int
): Single<Response<JobResponse>>
}
Run Code Online (Sandbox Code Playgroud)
JobListDataSource.kt
class JobListDataSource @Inject constructor(
private val jobListRestApi: JobListRestApi
): RxPagingSource<Int, Job>() {
override fun loadSingle(params: LoadParams<Int>): Single<LoadResult<Int, Job>> {
val position = params.key ?: 1
return jobListRestApi.getJobResponse(position).toSingle()
.subscribeOn(Schedulers.io())
.map { jobResponse -> jobResponse.jobData.jobs }
.map { jobs -> toLoadResult(jobs, position) }
.onErrorReturn { LoadResult.Error(it) }
}
private fun toLoadResult(data: ArrayList<Job>, position: Int): LoadResult<Int, Job> {
val prevKey = if (position == 1) null else position-1 …Run Code Online (Sandbox Code Playgroud) android mvvm rx-java android-livedata android-paging-library
我正在使用 android 分页库来显示搜索结果项,有什么方法可以清除/删除所有加载的结果项,在实时分页列表上调用 Invalidate 刷新列表未清除/删除项
In Activity:
fun clearSearchResult() {
if (productSearchResultItemAdapter.itemCount > 0) {
viewModel.invalidateResultList()
}
}
In ViewModel
private fun searchProductByTerm(searchTerm: String): Listing<Item> {
sourceFactory = ProductSearchItemDataSourceFactory(productSearchUC, compositeDisposable, searchTerm, resourceResolver)
val livePagedList = LivePagedListBuilder(sourceFactory, pagedListConfig)
//The executor used to fetch additional pages from the DataSource
.setFetchExecutor(getNetworkExecutor())
.build()
return Listing(
pagedList = livePagedList,
networkState = switchMap(sourceFactory.sourceLiveData) {
it.networkState
},
retry = {
sourceFactory.sourceLiveData.value?.retryAllFailed()
}
)
}
fun invalidateResultList() {
sourceFactory?.sourceLiveData?.value?.invalidate()
}
private val productSearchName = MutableLiveData<String>()
private val repoResult …Run Code Online (Sandbox Code Playgroud) android android-architecture-components android-paging-library
我想通过loadInitial或loadafter 中的callback.onError(或 callback.onRetryableError)将错误异常传递给我的分页适配器,但总是收到崩溃。经过一些调试后,我在库源中找到了这个:

这实际上是我在崩溃跟踪中收到的信息。只是“待办事项”。依赖版本:
'androidx.paging:paging-runtime-ktx:2.1.1'
在生产中出现这种异常消息是否可以,或者我做错了什么?无论如何......问题:任何其他“内置”方式将加载错误传递给适配器?
android android-architecture-components android-paging android-paging-library
我在我的项目中使用了 Jetpack 的Paging 3库来处理数据分页。我有一个用例,当用户更改搜索请求中的某些内容(例如添加/删除一些过滤器)时,我必须调用 API 并根据新的搜索请求使用新数据重新填充我的列表。但是当我创建新Pager实例并将其传递给我的PagingDataAdapter适配器时,它会抛出:
java.lang.IllegalStateException: Collecting from multiple PagingData concurrently is an illegal operation.
Run Code Online (Sandbox Code Playgroud)
我的实现是这样的:
存储库
class Repository {
fun getDataStream(request: Request): Flow<PagingData<Response>> {
return Pager(
config = PagingConfig(
pageSize = 10,
initialLoadSize = 10,
prefetchDistance = 3
),
initialKey = 1,
pagingSourceFactory = {
DataPagingSource(
request = request,
repository = this
)
}
).flow
}
fun getData(page: Int, request: Request): Result<Response> {
return remoteDataSource.getData(page, request)
}
}
Run Code Online (Sandbox Code Playgroud)
数据分页源
class DataPagingSource(
private …Run Code Online (Sandbox Code Playgroud) 我试图在我的应用程序中使用 Flow 将我的 Paging 2 实现转换为 Paging 3,但受到“Channel was closed”异常的困扰。该应用程序运行良好,但在一段时间后崩溃(从几秒钟到几分钟)。它从网络加载数据,这可能很慢。使用 Room 作为内部数据库。模拟器上的崩溃比真实设备上的要频繁得多。
我不熟悉 Flow,我应该注意 Flow 系统中是否有任何已知的罪魁祸首?
有没有其他人在使用 androidx 的 Paging 3 时遇到过类似的情况?
因此,如果这是一个已知问题(可能有明显的修复方法),请给我一个提示;与否,我可能会发布 Paging 3 代码部分(这将需要一些时间)。
我的异常如下所示:
2020-06-29 09:23:59.929 28295-28295/no.rogo.emptyfuel E/AndroidRuntime: FATAL EXCEPTION: main
Process: no.rogo.emptyfuel, PID: 28295
kotlinx.coroutines.channels.ClosedSendChannelException: Channel was closed
at kotlinx.coroutines.channels.Closed.getSendException(AbstractChannel.kt:1035)
at kotlinx.coroutines.channels.AbstractSendChannel.helpCloseAndResumeWithSendException(AbstractChannel.kt:206)
at kotlinx.coroutines.channels.AbstractSendChannel.access$helpCloseAndResumeWithSendException(AbstractChannel.kt:19)
at kotlinx.coroutines.channels.AbstractSendChannel.sendSuspend(AbstractChannel.kt:196)
at kotlinx.coroutines.channels.AbstractSendChannel.send(AbstractChannel.kt:135)
at kotlinx.coroutines.channels.ChannelCoroutine.send$suspendImpl(Unknown Source:2)
at kotlinx.coroutines.channels.ChannelCoroutine.send(Unknown Source:0)
at androidx.paging.PageFetcherSnapshot$pageEventFlow$1$2$invokeSuspend$$inlined$collect$1.emit(Collect.kt:137)
at kotlinx.coroutines.flow.FlowKt__ChannelsKt.emitAllImpl$FlowKt__ChannelsKt(Channels.kt:58)
at kotlinx.coroutines.flow.FlowKt__ChannelsKt$emitAllImpl$1.invokeSuspend(Unknown Source:11)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:56)
at kotlinx.coroutines.EventLoop.processUnconfinedEvent(EventLoop.common.kt:69)
at kotlinx.coroutines.DispatchedTaskKt.resumeUnconfined(DispatchedTask.kt:184)
at kotlinx.coroutines.DispatchedTaskKt.dispatch(DispatchedTask.kt:108)
at kotlinx.coroutines.CancellableContinuationImpl.dispatchResume(CancellableContinuationImpl.kt:308)
at kotlinx.coroutines.CancellableContinuationImpl.completeResume(CancellableContinuationImpl.kt:395)
at …Run Code Online (Sandbox Code Playgroud) 按照此处提供的说明,我已经能够成功实现新的 alpha07 版本的 Paging 3 库:https : //developer.android.com/topic/libraries/architecture/paging/v3-paged-data#guava-livedata
但是,现在我需要检查返回的列表是否为空以便向用户显示视图或文本,但我无法在其分页设计的流结构中附加任何检查。
目前,这是我onViewCreated在遵循他们的指南后在 Java 中使用我的代码的方式:
MyViewModel viewModel = new ViewModelProvider(this).get(MyViewModel.class);
LifecycleOwner lifecycleOwner = getViewLifecycleOwner();
Lifecycle lifecycle = lifecycleOwner.getLifecycle();
Pager<Integer, MyEntity> pager = new Pager<>(new PagingConfig(10), () -> viewModel.getMyPagingSource());
LiveData<PagingData<MyEntity>> pagingDataLiveData = PagingLiveData.cachedIn(PagingLiveData.getLiveData(pager), lifecycle);
pagingDataLiveData.observe(lifecycleOwner, data -> adapter.submitData(lifecycle, data));
Run Code Online (Sandbox Code Playgroud)
我尝试.filter在我的datain上附加 a adapter.submitData(lifecycle, data),但null尽管列表为空,但它从未收到项目。
在这种情况下,如何检查提交给适配器的数据何时为空?我在他们的文档中找不到任何指针。
编辑:这是我找到的解决方案,在这里发布是因为所选的答案严格来说不是解决方案,也不是在 Java 中,而是引导我找到它的答案。
我有一个附加LoadStateListener我的转接器,监听时LoadType是REFRESH和LoadState的是NotLoading,再检查是否adapter.getItemCount是0。
LoadType对于这种情况,可能采用不同的方法更合适,但到目前为止,刷新对我来说是有效的,所以我选择了那个。
示例:
// …Run Code Online (Sandbox Code Playgroud) 由于我目前正在开发一个带有自定义数据库(不是 Room)的项目,因此我正在测试是否可以在该项目中使用 Paging 3 库。但是,我遇到了一个问题,如果您对数据进行更改并因此使分页源无效,则列表的重新创建会出现错误并跳转到不同的位置。发生这种情况是因为刷新键计算似乎是错误的,这很可能是由于初始加载加载了三页数据,但将其放入一页而引起的。
默认的分页源如下所示:
override fun getRefreshKey(state: PagingState<Int, CustomData>): Int? {
// Try to find the page key of the closest page to anchorPosition, from
// either the prevKey or the nextKey, but you need to handle nullability
// here:
// * prevKey == null -> anchorPage is the first page.
// * nextKey == null -> anchorPage is the last page.
// * both prevKey and nextKey null -> anchorPage is the initial page, so
// just …Run Code Online (Sandbox Code Playgroud) android android-paging android-paging-library android-paging-3
android ×10
android-architecture-components ×2
androidx ×2
android-room ×1
kotlin-flow ×1
mvvm ×1
rx-java ×1