As you know, if we want to implement multiple types in RecyclerView
I use this approach intensively: http://frogermcs.github.io/inject-everything-viewholder-and-dagger-2-example/ In short:
onCreateViewHolder
to injected factories.onBind
on similar on base view holder so that you can call it with retrieved data in onBindViewHolder
.getItemViewType
(by either instanceOf
or comparing field value).Why?
It cleanly separates every view holder from the rest of app.
If you use autofactory
from google, you can easily inject dependencies required for every view holder. If you need to notify parent of some event, just create new interface, implement it in parent view (activity) and expose it in dagger. (pro tip: instead of initialising factories from their providers, simply specify that each required item's factory depends on factory that autofactory gives you and dagger will provide that for you).
We use it for +15 view holders and adapter has only to grow by ~3 lines for each new added (getItemViewType
checks).