Search code examples
androidandroid-paging-3

Footer not showing with Paging 3


I'm following the Codelab of Paging 3.

The paging is working fine, but trying to add a Footer does not seem to work.

My code is exactly like the Codelab one when it comes to LoadStateAdapter used

class ListLoadStateAdapter(
    private val retry: () -> Unit,
) : LoadStateAdapter<ListLoadStateAdapter.ListLoadStateViewHolder>() {

    override fun onBindViewHolder(holder: ListLoadStateViewHolder, loadState: LoadState) {
        holder.bind(loadState)
    }

    override fun onCreateViewHolder(
        parent: ViewGroup,
        loadState: LoadState,
    ) = ListLoadStateViewHolder.create(parent, retry)

    class ListLoadStateViewHolder(
        private val binding: ComponentPagedListFooterBinding,
        retry: () -> Unit,
    ) : RecyclerView.ViewHolder(binding.root) {

        init {
            binding.buttonRetry.setOnClickListener { retry.invoke() }
        }

        fun bind(loadState: LoadState) {
            if (loadState is LoadState.Error) {
                binding.textViewPlaceholderError.text = loadState.error.localizedMessage
            }

            binding.progressBar.isVisible = loadState is LoadState.Loading
            binding.buttonRetry.isVisible = loadState is LoadState.Error
            binding.textViewPlaceholderError.isVisible = loadState is LoadState.Error

//            binding.root.isVisible = loadState is LoadState.Loading || loadState is LoadState.Error
        }

        companion object {
            fun create(parent: ViewGroup, retry: () -> Unit): ListLoadStateViewHolder {
                val binding = ComponentPagedListFooterBinding.inflate(
                    LayoutInflater.from(parent.context),
                    parent,
                    false,
                )

                return ListLoadStateViewHolder(binding, retry)
            }
        }
    }
}

And this is how I add the footer

adapter = [email protected] {
                withLoadStateFooter(ListLoadStateAdapter { retry() })

                addLoadStateListener {
                    viewModel.handlePagingState(it, this)
                }
            }

handlePagingState is just to follow the state and bind it to the Page state (Loading, Error, Empty, etc). Removing it changed nothing anyways.

The ListLoadStateAdapter.onCreateViewHolder() doesn't even get called, neither the constructor for ListLoadStateViewHolder.

What am I doing wrong? Is there something I missed? Or maybe a bug somewhere?


Solution

  • My issue was that I was not setting the ConcatAdapter returned by withLoadStateFooter

    recyclerView.adapter = adapter.withLoadStateFooter(
        footer = ListLoadStateAdapter { retry() }
    )