我有一个大约5000个“地方”(即在地图上显示的经纬度)的数据库,并且我希望能够在地图上显示它们。
现在从“房间”中一次检索5000个位置给我带来了麻烦(超出了this question中所述的大小限制),所以我想使用分页库来检索大约100个批次并填充慢慢地图。
但是,Jetpack随附的分页库似乎非常适合与RecyclerView一起使用,整个documentation都围绕着构建一个无休止的滚动recyclerview。
所以我想知道是否可以将其与自定义组件一起使用,而不是与文档中所述的PagedAdapter一起使用。他们提到AsyncPagedListDiffer,其签名如下:
AsyncPagedListDiffer(ListUpdateCallback listUpdateCallback, AsyncDifferConfig<T> config)
所以我继续尝试执行以下操作:
DAO:
@Query("SELECT * FROM places join list_results where places.placeId = list_results.id ORDER by placeName ASC")
abstract fun getFilteredPlacesPaginated(): DataSource.Factory<Int, PlaceVO>
存储库:
override fun getListPlaces(pageSize: Int, boundaryCallback: PagedList.BoundaryCallback<StateAwarePlace>): Flowable<PagedList<StateAwarePlace>> {
return placesDao.getFilteredPlacesPaginated().map { StateAwarePlace.state(it) }.toFlowable(pageSize = pageSize, boundaryCallback = boundaryCallback)
}
ViewModel:
val boundaryCallback: PagedList.BoundaryCallback<StateAwarePlace> = object : PagedList.BoundaryCallback<StateAwarePlace>() {
override fun onZeroItemsLoaded() {
super.onZeroItemsLoaded()
Timber.w("PAGINATEDLIST - OnZeroItemsLoaded")
}
override fun onItemAtEndLoaded(itemAtEnd: StateAwarePlace) {
super.onItemAtEndLoaded(itemAtEnd)
Timber.w("PAGINATEDLIST - onItemAtEndLoaded ${itemAtEnd.place.placeName}")
}
override fun onItemAtFrontLoaded(itemAtFront: StateAwarePlace) {
super.onItemAtFrontLoaded(itemAtFront)
Timber.w("PAGINATEDLIST - onItemAtFrontLoaded ${itemAtFront.place.placeName}")
}
}
val paginatedPlaces = placesRepository.getListPlaces(50, boundaryCallback).toLiveData()
片段:
val asyncPagedListDiffer = AsyncPagedListDiffer<StateAwarePlace>(object : ListUpdateCallback {
override fun onChanged(position: Int, count: Int, payload: Any?) {
Timber.w("PAGINATEDLIST - Item in position $position has changed, now count is $count, payload $payload")
}
override fun onMoved(fromPosition: Int, toPosition: Int) {
Timber.d("PAGINATEDLIST - Item moved from $fromPosition to $toPosition")
}
override fun onInserted(position: Int, count: Int) {
Timber.i("PAGINATEDLIST - Item inserted on position $position, new count is $count")
}
override fun onRemoved(position: Int, count: Int) {
Timber.v("PAGINATEDLIST - Item removed on position $position, count is $count")
}
}, PlacesPaginatedAdapter.asyncConfig);
viewModel.paginatedPlaces.observe(this) { placesList: PagedList<StateAwarePlace> ->
asyncPagedListDiffer.submitList(placesList)
asyncPagedListDiffer.addPagedListListener { previousList, currentList ->
Timber.w("PAGINATEDLIST - Paged list listener ${previousList?.size} to ${currentList?.size}")
}
}
但是似乎它总是加载总的位置数。
我也不知道如何告诉pagedlist检索更多页面,或者如何使用差异将数据发送到我的地图实例...
toFlowable:
fun <Key, Value> DataSource.Factory<Key, Value>.toFlowable(
pageSize: Int,
initialLoadKey: Key? = null,
boundaryCallback: PagedList.BoundaryCallback<Value>? = null,
fetchScheduler: Scheduler? = null,
notifyScheduler: Scheduler? = null,
backpressureStrategy: BackpressureStrategy = BackpressureStrategy.LATEST
): Flowable<PagedList<Value>> {
return createRxPagedListBuilder(
dataSourceFactory = this,
config = Config(pageSize),
initialLoadKey = initialLoadKey,
boundaryCallback = boundaryCallback,
fetchScheduler = fetchScheduler,
notifyScheduler = notifyScheduler).buildFlowable(backpressureStrategy)
}
private fun <Key, Value> createRxPagedListBuilder(
dataSourceFactory: DataSource.Factory<Key, Value>,
config: PagedList.Config,
initialLoadKey: Key?,
boundaryCallback: PagedList.BoundaryCallback<Value>?,
fetchScheduler: Scheduler?,
notifyScheduler: Scheduler?
): RxPagedListBuilder<Key, Value> {
val builder = RxPagedListBuilder(dataSourceFactory, config)
.setInitialLoadKey(initialLoadKey)
.setBoundaryCallback(boundaryCallback)
if (fetchScheduler != null) builder.setFetchScheduler(fetchScheduler)
if (notifyScheduler != null) builder.setNotifyScheduler(notifyScheduler)
return builder
}