#android #kotlin #android-jetpack #android-paging
#Android #kotlin #android-jetpack #android-подкачка
Вопрос:
В моей реализации библиотеки подкачки Android 2.0 OnItemAtEndLoaded() вызывается множество раз, даже если он не прокручивается до конца списка. Вызов продолжается без остановки.
Эпизоды, полученные от вызова API, упорядочены по дате публикации точно так же, как метод getAllEpisodes() EpisodeDao.
Любая помощь будет высоко оценена.
class DetailsViewModel(
private val episodeBoundaryCallback: EpisodeBoundaryCallback,
private val episodeDao: EpisodeDao,
private val pagedListConfig: PagedList.Config
) : ViewModel() {
var episodes: LiveData<PagedList<Episode>>? = null
fun getEpisodes(podcastId: String) {
if (episodes != null
amp;amp; episodes!!.value != null
amp;amp; !episodes!!.value!!.isEmpty()
) {
return
}
episodeBoundaryCallback.param = podcastId
val dataSourceFactory = episodeDao.getAllEpisodes(podcastId)
val pagedListBuilder =
LivePagedListBuilder<Int, Episode>(dataSourceFactory, pagedListConfig)
pagedListBuilder.setInitialLoadKey(1)
pagedListBuilder.setBoundaryCallback(episodeBoundaryCallback)
episodes = pagedListBuilder.build()
}
}
class EpisodeBoundaryCallback(
private val podcastsApi: PodcastsApi,
private val skipToItDatabase: SkipToItDatabase
): PagedList.BoundaryCallback<Episode>() {
private val tag = this::class.simpleName
private val loadTypeRefresh = 0
private val loadTypeAppend = 1
lateinit var param: String
var startPage: Long? = null
val loading: MutableLiveData<Boolean> = MutableLiveData()
val loadError: MutableLiveData<Boolean> = MutableLiveData()
override fun onZeroItemsLoaded() {
Log.d("REFRESHING","REFRESHING")
AppExecutors.getInstance().diskIO().execute {
requestPage(loadTypeRefresh, startPage)
}
}
override fun onItemAtEndLoaded(itemAtEnd: Episode) {
Log.d(tag,"APPEND")
AppExecutors.getInstance().diskIO().execute {
requestPage(loadTypeAppend, itemAtEnd.publishDate)
}
}
override fun onItemAtFrontLoaded(itemAtFront: Episode) {
}
private fun requestPage(requestType: Int, loadKey: Long?) {
val episodeResultCall = podcastsApi.getEpisodes(param, loadKey)
val response = episodeResultCall.execute()
if (response.isSuccessful) {
Log.d(tag, "isSuccessful")
loading.postValue(false)
loadError.postValue(false)
val body: PodcastDetails = response.body()!!
onEpisodeFetchCallback(body, requestType)
} else {
Log.d(tag, "!isSuccessful")
loading.postValue(false)
loadError.postValue(true)
}
}
var lastEpisode: Episode? = null
private fun onEpisodeFetchCallback(
podcastDetails: PodcastDetails,
loadType: Int
) {
skipToItDatabase.beginTransaction()
try {
if (loadType == loadTypeRefresh) {
skipToItDatabase.episodeDao().deleteAllEpisodes()
}
val episodes = podcastDetails.episodes
for (i in episodes) {
i.podcastId = param
lastEpisode = i
}
skipToItDatabase.episodeDao().insertEpisodes(episodes)
skipToItDatabase.setTransactionSuccessful()
} finally {
skipToItDatabase.endTransaction()
}
}
}
@Dao
interface EpisodeDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insertEpisodes(episodes: List<Episode>)
@Query("SELECT * FROM episodes WHERE podcast_id = :podcastId ORDER BY publish_date DESC")
fun getAllEpisodes(podcastId: String): DataSource.Factory<Int, Episode>
@Query("DELETE FROM episodes")
fun deleteAllEpisodes()
}
@Headers("X-ListenAPI-Key: ${BuildConfig.apiKey}")
@GET("podcasts/{podcast_id}?sort=recent_first")
fun getEpisodes(
@Path("podcast_id") podcastId: String,
@Query("next_episode_pub_date") nextEpisodePubDate: Long?
): Call<PodcastDetails>
return new PagedList.Config.Builder()
.setPageSize(10)
.setPrefetchDistance(5)
.setEnablePlaceholders(true)
.setInitialLoadSizeHint(30)
.setMaxSize(60)
.build();
Ответ №1:
По-видимому, это была проблема с размером моего представления recycler в XML