【发布时间】:2020-06-12 04:27:12
【问题描述】:
Paging 3 几天前刚刚发布。
目前,我们正在将以下 Paging 3 示例代码从 Kotlin 移植到 Java。
科特林
/**
* We use the Kotlin API to construct a [Flow]<[PagingData]>. Java developers should use the
* Java API: `PagingDataFlow.create`
*/
val allCheeses = Pager(
PagingConfig(
/**
* A good page size is a value that fills at least a few screens worth of content on a
* large device so the User is unlikely to see a null item.
* You can play with this constant to observe the paging behavior.
*
* It's possible to vary this with list device size, but often unnecessary, unless a
* user scrolling on a large device is expected to scroll through items more quickly
* than a small device, such as when the large device uses a grid layout of items.
*/
pageSize = 60,
/**
* If placeholders are enabled, PagedList will report the full size but some items might
* be null in onBind method (PagedListAdapter triggers a rebind when data is loaded).
*
* If placeholders are disabled, onBind will never receive null but as more pages are
* loaded, the scrollbars will jitter as new pages are loaded. You should probably
* disable scrollbars if you disable placeholders.
*/
enablePlaceholders = true,
/**
* Maximum number of items a PagedList should hold in memory at once.
*
* This number triggers the PagedList to start dropping distant pages as more are loaded.
*/
maxSize = 200
)
) {
dao.allCheesesByName()
}.flow
Java
构造PagingConfig 不是什么大问题。
final int pageSize = 60;
final int prefetchDistance = pageSize;
final boolean enablePlaceholders = false;
final int initialLoadSize = pageSize * PagingConfig.DEFAULT_INITIAL_PAGE_MULTIPLIER;
final int maxSize = PagingConfig.MAX_SIZE_UNBOUNDED;
final int jumpThreshold = PagingSource.LoadResult.Page.COUNT_UNDEFINED;
PagingConfig pagingConfig = new PagingConfig(
pageSize,
prefetchDistance,
enablePlaceholders,
initialLoadSize,
maxSize,
jumpThreshold
);
但是,我们被困住了。
下面的代码注释引起了我们的注意。
Java 开发人员应使用 Java API:
PagingDataFlow.create
根据代码注释,Java 开发者应该使用PagingDataFlow.create。但是,在 IDE 中,我们确实找不到名为 androidx.paging.PagingDataFlow 的类。
我们希望有LiveData 可以观察到。 Kotlin 的流程/协程不是我们在 Java 领域所期望的。
知道如何使用PagingDataFlow.create吗?
【问题讨论】:
标签: java android kotlin android-paging