0

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)

theapache64
  • 10,926
  • 9
  • 65
  • 108
  • Is it really infinitely or just until all data is laid out (categories x movies)? – Pawel May 11 '20 at 09:20
  • @Pawel I just noticed that, once I start scrolling, and walked through every data (all categories and all movies), It's getting stopped. – theapache64 May 11 '20 at 09:24
  • Looked at your layout files, don't use wrap content height on main recycler view because it messes up recycling behavior. Use 0dp and constraint it to parents bottom. – Pawel May 11 '20 at 09:39
  • Oh my godd...!!! That fixed everything. I didn't know the bottom constraint has this much importance. You should create an answer to this question. I'll accept it. (y) @Pawel – theapache64 May 11 '20 at 10:27

1 Answers1

1

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

theapache64
  • 10,926
  • 9
  • 65
  • 108