carousell / MonoAdapter

General Adapter for single ViewType cases.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Wish: can you also support unbinder in the constructor?

tcw165 opened this issue · comments

You may treat onViewRecycled or onViewDetachedFromWindow callbacks as the unbinder.

It's been a while not reading the RecyclerView implementation for me. In my vague memory, I think onViewDetachedFromWindow is a better fit as the LayoutManager detaches the item view from the RecyclerView when the item view moves out of the visible window. Then LayoutManager re-attaches the item view back when it is about to be visible.

For onViewRecycled, I remember it's getting called a lot less frequently cause it occurs only when the view is less likely to be reused and the item view will also be relocated to another less-frequently used pool.

class MonoAdapter<V : ViewBinding, T>(
    private val viewProvider: (ViewGroup) -> V,
    private val binder: V.(T) -> Unit,
    diffCheck: DiffUtil.ItemCallback<T> = DefaultDiffCheck()
) : ListAdapter<T, MonoAdapter.ViewHolder<V>>(diffCheck) {

Can't think of a use case for MonoAdapter now, but to fix this concern we can have a setter function for outside to add callback and release resource accordingly if needed.
Thanks for the feedback and it will be our first feature request. 🎉
https://github.com/carousell/MonoAdapter/pull/4/files