使用Paging 3+Retrofit时API请求未触发问题排查求助
问题:Paging3结合Retrofit分页请求未触发排查
我尝试使用Paging 3结合Retrofit发起API分页请求,但不知为何请求未触发。多次检查代码仍未找到问题原因——单独调用该API时可正常执行,但结合Paging 3就失效。希望有人帮忙分析原因及排查方法?
请求的完整URL为:https://picsum.photos/v2/list?page=1&limit=20
Api Service类
interface ApiService { companion object{ const val BASE_URL = "https://picsum.photos/v2/" } @GET("list") suspend fun getImageList( @Query("page") pageNo:Int, @Query("limit") limit: Int=20 ):Response<ImageListResponse> }
Paging Source类
class ImagePagingSource(private val api:ApiService): PagingSource<Int, ImageItem>(){ override suspend fun load(params: LoadParams<Int>): LoadResult<Int, ImageItem> { try { Log.d("ImagePagingSource","Load is Called") val position = params.key ?: 1 val previousKey = if (position == 1) null else position - 1 /* * We don't have the total page key in this api response. * So restricting the call after page 10 statically. * */ val nextKey = if (position == 10) null else position + 1 val response = api.getImageList(position) if (!response.isSuccessful) return LoadResult.Error(Exception("Api Error : ${response.message()}")) if (response.body().isNullOrEmpty()) return LoadResult.Error(Exception("No Image Found")) Log.d("ImagePagingSource",response.body().toString()) return LoadResult.Page( data = response.body()!!, prevKey = previousKey, nextKey = nextKey ) } catch (e: Exception) { return LoadResult.Error(e) } } override fun getRefreshKey(state: PagingState<Int, ImageItem>): Int? { return state.anchorPosition?.let { anchorPosition -> val anchorPage = state.closestPageToPosition(anchorPosition) anchorPage?.prevKey?.plus(1) ?: anchorPage?.nextKey?.minus(1) } } }
Repository类
class HomePageRepo @Inject constructor( private val apiService: ApiService ) { fun getImages() = Pager( config = PagingConfig(pageSize = 20, maxSize = 100), pagingSourceFactory = { ImagePagingSource(apiService) } ).liveData suspend fun callApi(){ apiService.getImageList(1) } }
View Model代码
fun getSearchResult():LiveData<PagingData<ImageItem>> = repo.getImages().cachedIn(viewModelScope)
RecyclerView分页适配器
class ImageShowPagingAdapter(private val context:Context) : PagingDataAdapter< ImageItem, ImageShowPagingAdapter.ImageShowViewHolder >(COMPARATOR) { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ImageShowViewHolder { val inflater = LayoutInflater.from(context) val view = RecViewItemImagesBinding.inflate(inflater, parent, false) return ImageShowViewHolder(view) } override fun onBindViewHolder(holder: ImageShowViewHolder, position: Int) { val item = getItem(position) Glide.with(context) .load(item?.download_url) .apply( RequestOptions() .placeholder(R.drawable.all_type_content_default) .error(R.drawable.all_type_content_default) ) .into(holder.binding.ivImage) } inner class ImageShowViewHolder(val binding: RecViewItemImagesBinding) : RecyclerView.ViewHolder(binding.root) companion object { private val COMPARATOR = object : DiffUtil.ItemCallback<ImageItem>() { override fun areItemsTheSame(oldItem: ImageItem, newItem: ImageItem): Boolean { return oldItem.id == newItem.id } override fun areContentsTheSame(oldItem: ImageItem, newItem: ImageItem): Boolean { return oldItem == newItem } } } }
后续补充:经排查,问题出在RecyclerView分页适配器中,目前已解决。
内容的提问来源于stack exchange,提问作者samriddha
相关产品推荐
相关产品推荐

