android - DataSource.Factory 的创建方法没有得到调用

标签 android kotlin paging android-paging

我正在尝试使用分页库制作一个字典应用程序。 当用户写下任何字母时,我将调用 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)
    })

我的 View 模型:

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)
        })
      )

    }

}

我的数据源.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
    }
 }

所以请记住这一点,只要我启动应用程序,它就可以完美运行(它获得了我在 init{} 中调用的单词 "pouya" 的所有含义 我的 ViewHolder 的方法),但之后,当我在 edittext 中写入内容时,所有需要的方法都会被调用,除了 DataSource.Factorycreate() 方法。

任何帮助将不胜感激。

最佳答案

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

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

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() 之前设置Observers()

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

关于android - DataSource.Factory 的创建方法没有得到调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55993508/

相关文章:

android - 使用 RxJava 和 Android 的多个 API 调用

android - 如何在Android Studio中将输入数字分配为变量?

c - 如何通过分段或分页防止缓冲区溢出?

android - Kotlin 协程单元测试错误 : Exception in thread "main @coroutine#1 @coroutine#2" java. lang.NullPointerException

java - Kotlin 与列表末尾的新项目关联

database - 我如何告诉数据库只返回离散部分的数据?

listview - 当应用程序移至后台状态并再次移至前台状态时,无限列表会导致重复

android - 我的 Android 应用程序横幅未显示

java - 如何在 Android studio 中每 24 小时重置一次数据结构?

kotlin - 如何将自定义 View 组添加到 Anko DSL?