gpt4 book ai didi

android - 如何修复 android 架构组件分页 onItemAtEndLoaded 进入循环?

转载 作者:行者123 更新时间:2023-11-30 05:02:59 25 4
gpt4 key购买 nike

我正在尝试练习android架构组件Paging

具有 Room、MVVM 和 LiveData 的本地 + 远程数据源

当我第一次滚动列表(获取远程数据)时,它通过 PagedList.BoundaryCallback 中的onItemAtEndLoaded 进入循环,但是当下次打开 Activity 时它滚动平滑时间(获取本地数据)

这是我的 github 链接 here !

任何人都可以看看并帮助我解决它,谢谢!

Activity

class PagingActivity : AppCompatActivity() {

lateinit var viewModel: PagingViewModel
lateinit var adapter: PagingAdapter

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_paging)

val factory = PagingViewModelFactory(PagingRepository(), application)
viewModel = ViewModelProviders.of(this,factory).get(PagingViewModel::class.java)
adapter = PagingAdapter()
recyclerView.adapter = adapter

viewModel.pagedListLiveData.observe(this, Observer {

adapter.submitList(it)

})
}
}

View 模型

class PagingViewModel(repository: PagingRepository, application: Application) :
AndroidViewModel(application) {

val pagedListLiveData = repository.getDataItem(application)
}

存储库

class PagingRepository : PagingRepositoryCallback {

private lateinit var localDataSource: DataSource.Factory<Int, DataItem>

override fun getDataItem(application: Application): LiveData<PagedList<DataItem>> {

val pagedListLiveData: LiveData<PagedList<DataItem>> by lazy {

localDataSource = DataItemDbHelper(application).getRoomDataItemDao().getAllDataItem()

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

LivePagedListBuilder(localDataSource, config)
.setBoundaryCallback(PagingBoundaryCallback(application))
.build()
}

return pagedListLiveData
}
}

interface PagingRepositoryCallback {
fun getDataItem(application: Application): LiveData<PagedList<DataItem>>
}

边界回调

class PagingBoundaryCallback(context: Context) :
PagedList.BoundaryCallback<DataItem>() {

private var page = 2

private val api = AllPlayerApi.api

private val dao = DataItemDbHelper(context).getRoomDataItemDao()

override fun onZeroItemsLoaded() {
super.onZeroItemsLoaded()

api.getAllPlayer().enqueue(createWebserviceCallback())
}

override fun onItemAtEndLoaded(itemAtEnd: DataItem) {
super.onItemAtEndLoaded(itemAtEnd)
api.getAllPlayer(page).clone().enqueue(createWebserviceCallback())
}

private fun createWebserviceCallback(): Callback<AllPlayerData> {

return object : Callback<AllPlayerData> {
override fun onFailure(call: Call<AllPlayerData>?, t: Throwable?) {
Log.d("Huang", " get player fail ")
}

override fun onResponse(call: Call<AllPlayerData>?, response: Response<AllPlayerData>) {

Log.d("Huang", " onResponse " + page)

response.body()!!.data!!.forEach {
it.imageUrl = "https://pdc.princeton.edu/sites/pdc/files/events/new-nba-logo-1.png"
}

insertItemsIntoDb(response)
page++
}
}
}

private fun insertItemsIntoDb(response: Response<AllPlayerData>) {

GlobalScope.launch {

response.body()!!.data!!.forEach {

dao.insert(it)
}
}
}
}

最佳答案

逻辑,如果 onItemAtEndLoaded 得到相同的 itemAtEnd ,则什么也不做。

var lastItemAtEnd:DataItem? = null
override fun onItemAtEndLoaded(itemAtEnd: DataItem) {
lastItemAtEnd?.timestamp?.apply{
if(itemAtEnd.timestamp==this){
return;
}
}
super.onItemAtEndLoaded(itemAtEnd)
api.getAllPlayer(page).clone().enqueue(createWebserviceCallback())
}

关于android - 如何修复 android 架构组件分页 onItemAtEndLoaded 进入循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57893263/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com