Api调用与Paging 3未被触发[Closed]



我正在尝试使用分页3和改造进行api调用。但由于某些原因,api调用没有被触发。我检查了我的代码多次找到一个问题,但无法理解为什么我做错了。当我单独调用那个api时它就会被调用。但不是寻呼3。有人能告诉我为什么会这样吗?我怎样才能在这里找到问题呢?

My Api Service Class

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>
}

My 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)
}
}
}

My Repository Class

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)
}
}

视图模型
fun getSearchResult():LiveData<PagingData<ImageItem>> = repo.getImages().cachedIn(viewModelScope)

用于回收器视图的分页适配器

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
}
}
}
}

这是我试图点击的完整url

https://picsum.photos/v2/list?page=1&限制= 20

有没有人能帮助我了解为什么会发生这种情况,或者如何在这里找到问题。提前谢谢你

似乎问题是在我的回收器视图分页适配器内部导致问题的原因。我现在已经解决了这个问题。

似乎问题是在我的回收器视图分页适配器内部导致问题的原因。我正在实例化分页适配器,但在使用回收器视图设置它时,我的代码中有一些错误。因此导致api根本不会被调用。如果有人遇到同样的问题,请检查您的分页适配器,看看它是否与您的recyclerview正确分配。

相关内容

  • 没有找到相关文章

最新更新