无法通过分页刷新recyclerView [英] Unable to refresh recyclerView with pagination

查看:125
本文介绍了无法通过分页刷新recyclerView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个RecyclerView,其中充满了分页数据.

I have a RecyclerView that gets filled with paginated data.

我需要能够通过SwipeRefreshLayout刷新它,但是到目前为止,我还不能这样做.

I need to be able to refresh it via SwipeRefreshLayout, but so far, I am unable to do so.

我尝试了很多类似的参考,但是都没有成功.另外,为了让大家知道,当我刷新时,可以正确检索数据,但不会在recyclerView上显示该数据.

I have tried plenty references that does similar to this, but without any success. Also, for letting you guys know, When I do refresh, the data gets retrieved correctly, but it is not being displayed on the recyclerView...

此外,当我通过Navigation从那个目的地移动到另一个目的地,然后我回击时,它得到了更新.我不确定自己缺少什么或做错了什么.

Also, when I move from that destination to another via Navigation and then, I hit back, it gets updated. I am not sure what I am missing or what I am doing wrong.

ViewModel:

ViewModel:

class ActivityViewModel : ViewModel() {
    var itemDataSourceFactory = OperationsDataSourceFactory()
        private set
    val livePagedList: LiveData<PagedList<Result<Operation?>>>

    init {
        val config = PagedList.Config.Builder()
            .setEnablePlaceholders(false)
            .setInitialLoadSizeHint(10)
            .setPageSize(10)
            .build()

        livePagedList = LivePagedListBuilder(itemDataSourceFactory, config).build()
    }

    fun refresh() {
        itemDataSourceFactory.dataSourceLiveData.value?.endCursor = null
        itemDataSourceFactory.refresh()
    }
}

DataSourceFactory:

DataSourceFactory:

class OperationsDataSourceFactory :
    DataSource.Factory<String, Result<Operation?>>() {
    val dataSourceLiveData = MutableLiveData<OperationsDataSource>()
    lateinit var dataSource: OperationsDataSource
    override fun create(): DataSource<String, Result<Operation?>> {
        dataSource = OperationsDataSource()
        dataSourceLiveData.postValue(dataSource)
        return dataSource
    }

    fun refresh(){
        dataSourceLiveData.value?.invalidate()
    }
}

在Fragment上,我有这样的东西:

And, on the Fragment, I have something like this:

private fun initView() {
    binding.swipeToRefresh.setOnRefreshListener {
        itemViewModel.refresh()
    }
    getData() //observables with adapter.submitList(list)
}

推荐答案

好吧,经过长时间的研究,可能不是最好的解决方案,但开发人员也可以使用它.我们需要使"无效. layoutManager和适配器.

Ok, after a really long research, might not be the best solution, but developers use this too. We need to "invalidate" the layoutManager and adapter.

swipeToRefresh.setOnRefreshListener {
    operationsRecycler.apply {    
    layoutManager = null
    adapter = null
    layoutManager = LinearLayoutManager(
                        requireContext(),
                            LinearLayoutManager.VERTICAL,
                            false
                        )
    adapter = myAdapter
}

这篇关于无法通过分页刷新recyclerView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆