我正在尝试DataSource.Factory
上课,但在尝试返回我的PageKeyedDataSource
实现时出现错误。
class SubredditPageKeyedDataSource(private val service: LedditService,
private val subredditName: String): PageKeyedDataSource<String, Children<DataX>>() {
@SuppressLint("CheckResult")
override fun loadInitial(params: LoadInitialParams<String>, callback: LoadInitialCallback<String, Children<DataX>>) {
service.getSubredditTopics(subredditName).subscribe { result -> callback.onResult(result.data.children, result.data.before, result.data.after) }
}
@SuppressLint("CheckResult")
override fun loadAfter(params: LoadParams<String>, callback: LoadCallback<String, Children<DataX>>) {
service.getSubredditTopicsAfter(subredditName, after = params.key).subscribe {result -> callback.onResult(result.data.children, result.data.after)}
}
override fun loadBefore(params: LoadParams<String>, callback: LoadCallback<String, Children<DataX>>) {
// ignored, since we only ever append to our initial load
}
}
现在这里是我的工厂:
class SubredditDataSourceFactory(private val service: LedditService,
private val subredditName: String): DataSource.Factory<String, List<Children<DataX>>>() {
override fun create(): DataSource<String, List<Children<DataX>>> {
return SubredditPageKeyedDataSource(service, subredditName)
}
}
在上述create()
方法中返回我的 DataSource 时,AndroidStudio 给了我一个 IDE 错误:
Type mismatch.
Required: DataSource<String, List<Children<DataX>>>
Found: SubredditPageKeyedDataSource
我要疯了吗?PageKeyedDataSource
从中延伸出来ContiguousDataSource
的是 a DataSource
。为什么我得到类型不匹配?我检查了分页库的 2 个不同示例,它们的实现与我的完全一样。除非我错过了什么???