Search code examples
androidandroid-recyclerviewnestedrecyclerview

Nested RecyclerView : onBindViewHolder getting called infinitely


Let's make this real simple. I've two model classes, Category and Movie.

data class Category(
    val id: Long,
    val name: String,
    val movies: List<Movie>
)

data class Movie(
    val name : String
)

Each category has multiple movies. So to render this data, I am using nested RecyclerViews.

  • The problem

When I turn the device to landscape mode, the onBindViewHolder of MoviesAdapter (nested recyclerview's adapter) getting called infinitely. (Note, the same code works perfectly fine in portrait mode )

  • What I tried

I've changed the layout_width of nested recyclerview's item to 200dp from 120dp, and that's fixed the infinite call in a device, but on a larger screen I had put higher value to fix it.

I know this not an ideal solution, so I tried 'wrap_content` but it didn't work either. (same infinite call issue)

  • Questions

    1. Why the onBindViewHolder getting called infinitely ?
    2. What's the perfect way to fix this issue ?

As I don't want to flood the question feed with the code, I've hosted the reproducible version here (code minimized for the sake of readability)


Solution

  • I've fixed the issue by setting bottom constraint to main RecyclerView as @Pawel suggested in the comments.