-
Notifications
You must be signed in to change notification settings - Fork 314
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Reuse AdapterDelegate<List<T>> in a domain specific way #89
Comments
yea, I feel your pain. I am considering removing (in AdapterDelegates 5) the need of specifying a base class, so essentially AdatperDelegate operate on datasource |
That'd be cool too |
The above does not actually work if you have different item view types. In my first use case it either had X or Y. Having X & Y would crash. Updated version: @Suppress("UNCHECKED_CAST", "PROTECTED_CALL_FROM_PUBLIC_INLINE")
inline fun <Old, reified I : T, T> AdapterDelegate<List<Old>>.wrap(noinline mapper: (I) -> Old): AdapterDelegate<List<T>> {
val listItemAdapterDelegate = this as AbsListItemAdapterDelegate<Old, Old, AdapterDelegateViewHolder<Old>>
return object : AbsListItemAdapterDelegate<I, T, RecyclerView.ViewHolder>() {
override fun isForViewType(item: T, items: MutableList<T>, position: Int): Boolean = item is I
override fun onBindViewHolder(item: I, holder: RecyclerView.ViewHolder, payloads: MutableList<Any>) {
listItemAdapterDelegate.onBindViewHolder(mapper.invoke(item), holder as AdapterDelegateViewHolder<Old>, payloads)
}
override fun onCreateViewHolder(parent: ViewGroup) = listItemAdapterDelegate.onCreateViewHolder(parent)
}
} |
Use case:
I've got a few layouts which I can reuse on different screens. Think of an empty screen. Empty screen consists of an Emoji, text and also subtitle.
I want to reuse code for inflation + binding, so I've created:
Now on my feature screen A I have a RecyclerView that can either have Leaderboard entries or an empty state:
Creating the AdapterDelegate for the leader board is straight forward:
Now, how can I reuse the empty state Adapter Delegate?
This does not work since my factory function returns an
AdapterDelegate<List<EmptyState>>
and I need aAdapterDelegate<List<Entry>>
.This is what I came up with:
Then I can do:
Now as for the
wrapped
function:I feel like this functionality should be provided by the library? Maybe it even is and I'm missing it? Additionally, since the methods are protected my helper function currently lives in
package com.hannesdorfmann.adapterdelegates4
😆Do you have any better ideas?
The text was updated successfully, but these errors were encountered: