ViewModel:
val eventDataFactory = AppDatabase.getInstance(context)!!.eventDao().getEventList()
val pagedListConfig = PagedList.Config.Builder()
.setEnablePlaceholders(false)
.setInitialLoadSizeHint(10)
.setPrefetchDistance(2)
.setPageSize(10).build()
eventListResponse = LivePagedListBuilder(eventDataFactory, pagedListConfig)
.build()
fun getEventListLiveData(): LiveData<PagedList<Event>> {
return eventListResponse!!
}
片段:
viewModel.getEventListLiveData().observe(this@Fragment, Observer {
Log.d("Fragment", "eventList Update: " + it!!.size)
for (event: Event in it) {
if (event != null)
Log.d("Fragment", "EventId: " + event.eventId)
}
eventListAdapter!!.submitList(it)
})
EventDao:
@Query("SELECT * FROM event")
fun getEventList(): DataSource.Factory<Int,Event>
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insertAll(events: List<Event>): List<Long>
我的本地数据库中有55个条目,并且我想每次滚动都会获得10个条目。该适配器获取更新值但观察者未触发后,我才第一次获得eventList Update: 10
。
同样,当我在数据库中插入新值时,观察者仅返回前10个值,而不是新添加的值。