callback.onResult()不会在android中的PageKeyedDataSource <int,item =“”>()中返回值给适配器

时间:2018-09-27 03:05:28

标签: android kotlin android-livedata

我在项目中使用的是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
    }}

DataSource.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}")
            }
        })
    }
}

1 个答案:

答案 0 :(得分:0)

在适配器中,由于您正在使用PagedListAdapter,因此您不应该手动管理列表。

在您的@Override public int getItemCount() {}中执行以下操作:

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