Search code examples
androidandroid-recyclerviewandroid-databindingandroid-paging-3

Android Paging 3.0 load method not working


i am trying to implement the android paging 3.0 library and i am having an issue with the .load method not working, at first i thought the adapter submitData or layoutManager was not working cause i did get response from LoadResult.Page, but now when looking further it kind of stopped working at all

here's my code

1- Fragment

private fun initAdapter() {
    searchAdapter = SearchItemsAdapter {
        //item clicked
    }
    val gridLayoutManager = GridLayoutManager(requireContext(), 2)
    binding?.layoutManager = gridLayoutManager
    binding?.searchAdapter = searchAdapter
}
private fun startSearch() {
    searchJob?.cancel()
    searchJob = viewLifecycleOwner.lifecycleScope.launch {
        searchViewModel?.getSearchResults("phone")?.collectLatest {
            searchAdapter?.submitData(it)
        }
    }
}

2- ViewModel

fun getSearchResults(query: String): Flow<PagingData<ItemModel>> {
    val lastResult = currentSearchResult
    if (query == currentQueryValue && lastResult != null) {
        return lastResult
    }
    currentQueryValue = query
    val newResult: Flow<PagingData<ItemModel>> = searchRepository.getSearchResults(query)
        .cachedIn(viewModelScope)
    currentSearchResult = newResult
    return newResult
}

3- SearchRepository

fun getSearchResults(query: String): Flow<PagingData<ItemModel>> {
    return Pager(
        config = PagingConfig(
            pageSize = 10
        ),
        pagingSourceFactory = { SearchPagingSource(client, query) }
    ).flow
}

4- PagingSource

override suspend fun load(params: LoadParams<Int>): LoadResult<Int, ItemModel> {
    val position: Int = params.key ?: 1
    return try {
        val response: SearchResponse = service.getSearchResult(query, position)
        val items = response.metadata.results
        val nextKey = if (items.isEmpty()) {
            null
        } else {
            position + 1
        }
        LoadResult.Page(
            data = items,
            prevKey = if (position == 1) null else position - 1,
            nextKey = nextKey
        )
    } catch (exception: IOException) {
        return LoadResult.Error(exception)
    } catch (exception: HttpException) {
        return LoadResult.Error(exception)
    }
}

5- Adapter

override fun onBindViewHolder(holder: SearchItemsViewHolder, position: Int) {
    val item = getItem(position)
    if (item != null) {
        holder.binding.item = item
        holder.itemView.setOnClickListener {
            itemClickCallback(item)
        }
    }
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SearchItemsViewHolder {
    val layoutInflater = LayoutInflater.from(parent.context)
    val binding = SearchItemLayoutBinding.inflate(layoutInflater, parent, false)
    return SearchItemsViewHolder(binding)
}

companion object {
    private val diffCallBack = object : DiffUtil.ItemCallback<ItemModel>() {
        override fun areItemsTheSame(oldItem: ItemModel, newItem: ItemModel): Boolean =
            oldItem.id == newItem.id

        override fun areContentsTheSame(oldItem: ItemModel, newItem: ItemModel): Boolean =
            oldItem == newItem
    }
}

Solution

  • i found the issue that i had

    1- i was not calling the startSearch method from the onViewCreated method of the fragment, that didn't help with the API calling

    2- the recyclerView in the XML had wrap_content instead of 0dp as height