我正在使用 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
按照此处提供的说明,我已经能够成功实现新的 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) 我使用提交列表将 pagedlist 值传递给适配器。当我更新单个项目时,请考虑我正在点击回收器视图中的提要的类似按钮。如何更新单个项目。
我正在按照这个例子进行分页实现
https://github.com/saquib3705/PagingLibrarySampleApp
它只是加载数据并更新回收器视图。我想为项目添加“喜欢”按钮,并在用户喜欢如何实现它时更新列表。另请看看这正是我正在寻找的 更新列表项在 PagingLibrary 中不使用 Room(仅限网络)
android android-mvvm android-architecture-components android-jetpack android-paging-library
我正在学习 android 分页库,一切正常。但是每当我使数据源无效时,只要将新项目添加到列表顶部,列表就会跳转到起始位置。我想在列表中将项目添加到顶部。但是列表应该保持在当前的滚动位置。我想我应该使用 LoadInitialParams.requestedInitialKey 失效时它不为空。但我不确定如何使用此键加载数据以避免列表跳转到起始位置。
我使用 Firestore 数据库作为数据源。我发现提供给回调的requestedInitialKey实际上是当前滚动位置的recyclerview中的最后一个可见项目。我已经尝试了以下方法来获取有关 requestsInitialKey 的数据
.get().startAt(requestedIntitalKey).limit(25)
.get().endBefore(requestedIntitalKey).limit(25)
.get().startAfter(requestedIntitalKey).limit(25)
.get().endAt(requestedIntitalKey).limit(25)
Run Code Online (Sandbox Code Playgroud)
其中 25 是我的页面大小。以上都没有解决问题。有人可以为我提供一个示例,以便列表将停留在其当前滚动位置,只更新内容而不跳转开始吗?
我正在尝试使用分页库在 RecyclerView 中显示具有搜索功能的联系人。https://github.com/anujmiddha/paged-list-demo我关注这个 GitHub 项目。以下是我的项目中的代码片段。我正在尝试使用 pagedList 适配器逐渐获取联系人。但这个 LIMIT 和 OFFSET 在某些 Android 版本(例如 Android 9.0)中无法按预期工作。有什么替代方案吗?
private val PROJECTION = arrayOf(
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER
)
val cursor = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
PROJECTION,
null,
null,
ContactsContract.Contacts.DISPLAY_NAME +
" ASC LIMIT " + limit + " OFFSET " + offset)
Run Code Online (Sandbox Code Playgroud)
另外,我尝试了另一种方法,如下所示。
val queryArgs = Bundle()
val cursor: Cursor?
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
queryArgs.putInt(ContentResolver.QUERY_ARG_OFFSET, offset)
queryArgs.putInt(ContentResolver.QUERY_ARG_LIMIT, limit)
cursor = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
PROJECTION,
queryArgs,
null)
} else {
cursor = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
PROJECTION,
null,
null,
ContactsContract.Contacts.DISPLAY_NAME + …Run Code Online (Sandbox Code Playgroud) android android-contentresolver android-contentprovider android-architecture-components android-paging-library
请帮帮我。
该应用程序仅用于从https://trefle.io接收植物列表并将其显示在 RecyclerView 中。
我在这里使用分页库 3.0。
任务:我想添加一个标题,其中将显示植物总数。
问题:我只是找不到将总项目的值传递给标题的方法。
Data model:
data class PlantsResponseObject(
@SerializedName("data")
val data: List<PlantModel>?,
@SerializedName("meta")
val meta: Meta?
) {
data class Meta(
@SerializedName("total")
val total: Int? // 415648
)
}
data class PlantModel(
@SerializedName("author")
val author: String?,
@SerializedName("genus_id")
val genusId: Int?,
@SerializedName("id")
val id: Int?)
Run Code Online (Sandbox Code Playgroud)
数据源类:
class PlantsDataSource(
private val plantsApi: PlantsAPI,
private var filters: String? = null,
private var isVegetable: Boolean? = false
) : RxPagingSource<Int, PlantView>() {
override fun loadSingle(params: …Run Code Online (Sandbox Code Playgroud) android kotlin android-recyclerview android-paging android-paging-library
我正在使用paging3并且有两个不同的分页源。问题是Coroutine Scope只发出第一个分页流
在ViewModel我有两个分页流程
val pagingFlow1 = Pager(PagingConfig(pageSize = 50, prefetchDistance = 1)) {
pagingSource
}.flow.cachedIn(viewModelScope)
val pagingFlow2 = Pager(PagingConfig(pageSize = 50, prefetchDistance = 1)) {
pagingSource2
}.flow.cachedIn(viewModelScope)
Run Code Online (Sandbox Code Playgroud)
在活动中收集它们
lifecycleScope.launch(Dispatchers.IO) {
viewModel.pagingFlow1.collectLatest { pagingData ->
pagingAdapter.submitData(pagingData)
}
viewModel.pagingFlow2.collectLatest { pagingData ->
pagingAdapter2.submitData(pagingData)
}
}
Run Code Online (Sandbox Code Playgroud)
但lifecycleScope只能发出,pagingFlow1换句话说,分页只能在第一个 recyclerView 中起作用。
当我更改订单时,这次仅适用于pagingFlow2
lifecycleScope.launch(Dispatchers.IO) {
viewModel.pagingFlow2.collectLatest { pagingData ->
pagingAdapter.submitData(pagingData)
}
viewModel.pagingFlow1.collectLatest { pagingData ->
pagingAdapter2.submitData(pagingData)
}
}
Run Code Online (Sandbox Code Playgroud)
为了确保我用基本流程对其进行了测试并正常工作
// Flows in ViewModel
val testFlow1 …Run Code Online (Sandbox Code Playgroud) 我是 android kotlin 中分页 3 库的新手。我想要无限数据。所以我发现 Paging 3 库对我的情况很有帮助。我使用PagingSource创建了一个列表。我没有使用 Room。我嵌套了recyclerView。我正在将PagingDataAdapter与diff util用于我的 Recyclerview。我使用了codelab推荐的分页库教程我成功了,没有任何问题。我面临更新项目的困难。我使用分页源创建列表,在列表中我有一些来自服务器的数据。我完全没有任何问题。但是如何更新适配器或通知数据在 reyclerview 中发生了变化。我已经有机制来获取更新的列表。我搜索了如何在某个地方更新适配器,但每个地方都提到使用来自DataSource 的invalidate()。数据源用于分页 2对吗?现在,根据Migrate to Paging 3 中的文档,这是在Paging 3 中。我使用 Flow 来检索数据。此代码位于viewmodel类中。
fun createRepo(data: List<String>, repository: RepositoryData): Flow<PagingData<UnlimitData>> {
return repository.getStreamData(data).cachedIn(viewModelScope)
}
Run Code Online (Sandbox Code Playgroud)
我正在传递来自服务器的列表。getStreamData函数返回带有 int 和 string 数据的项目。我的数据类
data class UnlimitData(val id: Int, val name: String)
Run Code Online (Sandbox Code Playgroud)
createRepo正在调用我的活动类以在 Adpater 中发送数据。
lifecycleScope.launch {
viewModel.createRepo(serverData,repository).collectLatest …Run Code Online (Sandbox Code Playgroud) android kotlin nestedrecyclerview android-paging-library android-paging-3
我正在使用 Kotlin flow 和 Android Paging 3 库编写一个玩具 Android 应用程序。该应用程序调用一些远程 API 来获取照片列表,并RecyclerView使用PagingDataAdapter.
我发现后面的代码pagingAdapter.submitData()没有执行。
这是代码片段(该函数位于 a 中Fragment):
fun refreshList() {
lifecycleScope.launch {
photosViewModel.listPhotos().collect {
// `it` is PagingData<Photo>
pagingAdapter.submitData(it)
Log.e(TAG, "After submitData")
}
}
}
Run Code Online (Sandbox Code Playgroud)
日志After submitData不打印。
但是,如果我将日志记录放在该行的前面pagingAdapter.submitData(),则会打印它,如下所示:
fun refreshList() {
lifecycleScope.launch {
photosViewModel.listPhotos().collect {
// `it` is PagingData<Photo>
Log.e(TAG, "Before submitData")
pagingAdapter.submitData(it)
}
}
}
Run Code Online (Sandbox Code Playgroud)
打印日志Before submitData没有问题。
请问为什么会出现这种情况?
android kotlin android-paging android-paging-library kotlin-coroutines
我将 Paging 3 与 RemoteMediator 和 Room 结合使用,通过 PagingDataAdapter 在 RecyclerView 中显示项目列表。我们遇到一个问题,当数据库中保存的基础数据多次更新时(在某些情况下),会导致列表跳转到开头。我已经成功创建了一个可重现的场景:
然后我执行以下操作之一:
看来潜在的问题是,在数据已经失效一次并且除当前页面之外的所有页面都已从回收器视图中删除后,分页库无法正确处理额外的数据库失效。
我的问题是:
存储库中的相关代码:
@MainThread
fun fetchNewData(...): LiveData<PagingData<DisplayCard>> {
val pagingSourceFactory = ... // Get appropriate PagingSource based on some conditions
@OptIn(ExperimentalPagingApi::class)
return Pager(
config = PagingConfig(
pageSize = DiscussionUseCase.PAGE_SIZE, // PAGE_SIZE = 20
prefetchDistance = 2,
enablePlaceholders = false,
initialLoadSize = DiscussionUseCase.PAGE_SIZE // PAGE_SIZE = …Run Code Online (Sandbox Code Playgroud) android kotlin android-paging android-paging-library android-paging-3