ViewModel and Data Binding: why do the views randomly jumble up?


On page 394 I read that:

This is happening because your layout has no way of knowing that you updated SoundViewModel's Sound inside SoundHolder.bind(Sound)

I don’t quite understand why. Following the logic of RecyclerView, every time it needs to recycle a view, onBindViewHolder(), bind(), getItemCount() get called. When bind() is called, we proceed to set the updated sound object to our ViewModel: getViewModel().setSound(sound). We even call executePendingBindings() to force the layout to immediately update itself.

Why then is this logic working when creating views but it breaks once recycling them?


I too have the same question. In fact, on my Android physical device, I do not see anything like the Figure 20.12 titled Déjà vu. The RecyclerView seems to be working fine for me. Have you found a scenario similar to what was mentioned in the book, yet?

@cstewart your input on this would be great. Thanks!


Actually, I found the answer by @cstewart in the below posts on the main page for this chapter. The link is pasted below: