vicmns vicmns - 1 year ago 438
Android Question

RecyclerView onBindViewHolder with payloads not being called

Ok I'm getting out of ideas on how to tackle this... I'm trying to implement the RecyclerViews Adapter onBindViewHolder with payloads, since I use lazy loading on each adapter items to load a extra information for each card/item on the RV.

So my problem is that, even if I call notifyItemChanged with the payload that I'm using (which is a simple string) when it reaches onBindViewHolder the payload list it's always empty :S.
Of course I googled about it, and all the posts (the awesome quantity of 3 posts btw) from here says that I have to override the method canReuseUpdatedViewHolder on the DefaultItemAnimator to return true in order to the payloads to work. So I went ahead and did that, which totally didn't work.

After some monkey testing (scrolling all the way down before any item has finished loading), I discovered that some times one payload is successfully passed to the onBindViewHolder...
Digging more, I went ahead and stopped setting the extra information that I download from the Lazy Loading WS on my models. Until I found out that the culprit is when I set a huge String value on my model (which is used to render a chart, so expect it to be large ~5mb on the worst case... but generally is ~200k), if I comment out that line of code, the damn payload is passed around... without a fail! Even that my payload is a simple string!

I'm starting believing on witchcraft here...

Other stuff that I tried:
1) Using setHasStableIds(true) on the Adapter (of course using uniques values on getItemId() method)
2) Created my own ItemAnimator that extends from DefaultItemAnimator. Which also I discovered that animateChange is never called...
3) Setting the ItemAnimator to NULL

I think that the RV someway attach a given model to the current ViewHolder, Since it doesn't make sense to me this kind of behaviour that only happens when setting that huge string...

Any advice or resource will help...

Answer Source

Well, it results that my getItemViewType logic was wrong... I was changing some info that was used on getItemViewType when my extra WS finished, so my previous ViewHolder type was different from the newest one to set, so the RV will handle it as a full replace.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download