我试图用分页库制作字典应用程序。
当用户写任何字母时,我将调用API并获取包括该字母在内的所有单词。

问题是,当我通过ViewHolder的init{}方法中的Paging库调用API时,它可以正常工作。但是之后,当我尝试编写其他内容并获取相关数据时,Paging库甚至不调用API。我在每个位置都放置了断点,并且我第一次看到,DataSource.Factory的create()方法没有得到调用。

这是我的代码:

我的 Activity :

    viewModel = ViewModelProviders.of(this).get(DictionaryViewModel::class.java)
    val adapter = DictionaryRecyclerPagedAdapter()
    recycler.layoutManager = LinearLayoutManager(this, RecyclerView.VERTICAL, false)
    recycler.setHasFixedSize(true)
    recycler.adapter = adapter

    RxTextView.textChanges(edtWord)
        .filter { it.isNotBlank() }
        .subscribe({
            adapter.submitList(null)
            viewModel.getWord(it.toString())
            Logger.d(it)

        }, {
            Logger.d(it)
        })



    viewModel.itemPagedList?.observe(this, Observer {
        adapter.submitList(it)
    })

我的ViewModel:
var liveDataSource: LiveData<PageKeyedDataSource<Int, DictionaryResult>>? = null
var itemPagedList: LiveData<PagedList<DictionaryResult>>? = null

init {

    getWord("pouya")
}

fun getWord(term: String) {

    val dataSourceFactory = DictionaryDataFactory(term)
    liveDataSource = dataSourceFactory.liveData


    val config = PagedList.Config.Builder()
        .setEnablePlaceholders(true)
        .setPageSize(10)
        .setPrefetchDistance(4)
        .build()

    itemPagedList = LivePagedListBuilder(dataSourceFactory, config).build()

}

我的数据源:
   class DictionaryDataSource(private val term: String) : PageKeyedDataSource<Int, DictionaryResult>() {

override fun loadInitial(params: LoadInitialParams<Int>, callback: LoadInitialCallback<Int, DictionaryResult>) {
    Logger.d("Initial")
    composite.add(
        repository.getWord(FIRST_PAGE, term)
            .subscribe({
                if (it.result != null)
                    callback.onResult(it.result, null, FIRST_PAGE + 1)
            }, {
                Logger.d(it)

            })
    )
}

override fun loadAfter(params: LoadParams<Int>, callback: LoadCallback<Int, DictionaryResult>) {
    Logger.d("AFTER")
    composite.add(repository.getWord(params.key, term)
        .subscribe({
            if (it.result != null)
                callback.onResult(it.result, params.key + 1)
        }, {
            Logger.d(it)
        })
      )

    }

}

我的DataSource.Factory:
  class DictionaryDataFactory(private val term: String) : DataSource.Factory<Int, DictionaryResult>() {

val liveData = MutableLiveData<PageKeyedDataSource<Int, DictionaryResult>>()

override fun create(): DataSource<Int, DictionaryResult> {
    val dataSource = DictionaryDataSource(term)
    liveData.postValue(dataSource)
    return dataSource
    }
 }

因此,请记住这一点,它在我启动该应用程序后就可以完美运行(它获得了"pouya"一词的所有含义,就像我在ViewHolder的init{}方法中调用它的意思一样),但是之后,当我在edittext中编写内容时,所有所需的方法会被调用,create()DataSource.Factory方法除外。

任何帮助将不胜感激。

最佳答案

我也遇到过同样的问题,为我解决的问题是在设置观察者之前构建列表,在您的情况下,请确保在观察itemPagedList之前调用getWord()

例如 :-
这是使用viewModel.reponseList的片段的片段

override fun onViewReady() {
    setRecyclerView()
    viewModel.buildResponseList() //For some reason responseList must be built before observing on it ,
    // otherwise create() in FoodDataSourceFactory won't be called
    setObservers()
}

private fun setObservers() {
    viewModel.responseList.observe(this, Observer {
        (this.rvFoodList.adapter as FoodListAdapter).submitList(it)
    })
}

private fun setRecyclerView() {
    val mRecyclerView = this.rvFoodList
    mRecyclerView.apply {
        layoutManager = LinearLayoutManager(this@HomeFragment.context)
        adapter = FoodListAdapter()
    }

ViewModel部分:-
fun buildResponseList() {
    val foodDataSourceFactory =
        FoodDataSourceFactory(this)

    val config = PagedList.Config.Builder()
        .setInitialLoadSizeHint(30)
        .setEnablePlaceholders(false)
        .setPageSize(30)
        .build()

    responseList = LivePagedListBuilder(foodDataSourceFactory, config).build()
}

在onViewReady()中

如果我在调用viewModel.buildResponseList()之前设置了setObservers()

DataSourceFactory类不会调用create()方法,因此不会在DataSource类中进行网络调用

07-24 09:49
查看更多