callback.onResult() 在 android 中的 PageKeyedDataSource<Int, Item>() 中未向适配器返回值



我在我的项目中使用了android架构组件。所以我使用分页库进行分页,在我的情况下,我需要PageKeyedDataSource,但我无法将加载的数据返回UI,

代码是

DataSourceFactory.kt

class DataSourceFactory : DataSource.Factory<Int, Item>() {
private val mutableLiveData = MutableLiveData<SearchedItemsDataSource>()
private lateinit var feedDataSource: SearchedItemsDataSource
override fun create(): DataSource<Int, Item> {
feedDataSource = SearchedItemsDataSource()
mutableLiveData.postValue(feedDataSource)
return feedDataSource
}}

数据源.kt

class SearchedItemsDataSource() : PageKeyedDataSource<Int, Item>() {
private var client: Api
init {
val logging = HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY)
val httpClient = OkHttpClient.Builder()
httpClient.addInterceptor(logging)
val retrofit = Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.baseUrl("https://api.stackexchange.com/2.2/")
.client(httpClient.build())
.build()
client = retrofit.create(Api::class.java)
}
override fun loadInitial(params: LoadInitialParams<Int>, callback: LoadInitialCallback<Int, Item>) {
val callClient = client.getAnswers(1, params.requestedLoadSize, "stackoverflow")
callClient.enqueue(object : Callback<StackApiResponse> {
override fun onResponse(call: Call<StackApiResponse>, response: Response<StackApiResponse>) {
if (response.isSuccessful && response.body() != null) {
println("loadInitial onResponse1111 :${response.isSuccessful} *** ${params.requestedLoadSize}")
val searchedItems = response.body() as StackApiResponse
callback.onResult(searchedItems.items, 0,searchedItems.items.size,null, 21)
println("loadInitial onResponse1111 :${response.isSuccessful} *** ${params.requestedLoadSize} *** ${searchedItems.items.size}")
} else {
println("loadInitial onResponse :${response.isSuccessful} *** ${response.message()}")
}
}
override fun onFailure(call: Call<StackApiResponse>, t: Throwable) {
t.printStackTrace()
println("loadInitial onFailure :${t.message}   ${t.localizedMessage}")
}
})
}
override fun loadAfter(params: LoadParams<Int>, callback: LoadCallback<Int, Item>) {
println("loadAfter " + params.key + " Count " + params.requestedLoadSize);
val callClient = client.getAnswers(params.key, params.requestedLoadSize, "stackoverflow")
callClient.enqueue(object : Callback<StackApiResponse> {
override fun onResponse(call: Call<StackApiResponse>, response: Response<StackApiResponse>) {
if (response.isSuccessful && response.body() != null) {
println("loadAfter onResponse :${response.isSuccessful} *** ${params.key}")
val nextKey = params.key + 1
val searchedItems = response.body() as StackApiResponse
callback.onResult(searchedItems.items, nextKey)
} else {
println("loadAfter onResponse :${response.isSuccessful} *** ${response.message()}")
}
}
override fun onFailure(call: Call<StackApiResponse>, t: Throwable) {
t.printStackTrace()
println("loadAfter onFailure :${t.message}   ${t.localizedMessage}")
}
})
}
override fun loadBefore(params: LoadParams<Int>, callback: LoadCallback<Int, Item>) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}}

ViewModel.kt

class MainViewModel : ViewModel() {
internal lateinit var itemList: LiveData<PagedList<Item>>
fun getPagedItems(): LiveData<PagedList<Item>> {
val executor = Executors.newFixedThreadPool(5)
val feedDataFactory = DataSourceFactory()
val pagedListConfig = PagedList.Config.Builder()
.setEnablePlaceholders(true)
.setInitialLoadSizeHint(20)
.setPageSize(20).build()
itemList = (LivePagedListBuilder(feedDataFactory, pagedListConfig)).build()
return itemList
}}

MainActivity.kt

class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
private lateinit var viewModel: MainViewModel
private lateinit var mAdapter: ItemAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
viewModel = ViewModelProviders.of(this).get(MainViewModel::class.java)
mAdapter = ItemAdapter(this)
binding.recyclerView.adapter = mAdapter
viewModel.getPagedItems().observe(this, Observer { items ->
if (items != null && items.isNotEmpty()) {
println("observe called 1 ${items.size}")
mAdapter.submitList(items)
} else {
println("observe called 2 ${items?.size}")
}
})
}
}

在适配器中,不应手动管理列表,因为您使用的是 PagedListAdapter。

在您的@Override public int getItemCount() {}中,请:

@Override
public int getItemCount() {
return super.getItemCount()
}

最新更新