Last active
January 15, 2020 18:36
-
-
Save DjakaTechnology/ff4f2ac5ba276a48434a84688e8eab86 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class AdapterDelegateManager<T> { | |
| private val delegates = mutableListOf<AdapterDelegate<T>>() | |
| fun addDelegate(delegate: AdapterDelegate<T>) { | |
| delegates.add(delegate) | |
| } | |
| fun getItemViewType(items: List<T>, position: Int): Int { | |
| delegates.forEachIndexed { index, it -> if (it.isForViewType(items, position)) return index } | |
| throw IllegalStateException("Adapter Delegate for specified type at index: $position not found") | |
| } | |
| fun onBindViewHolder(items: List<T>, position: Int, viewHolder: RecyclerView.ViewHolder) { | |
| delegates[getItemViewType(items, position)].onBindViewHolder(items, position, viewHolder) | |
| } | |
| fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder { | |
| return delegates[viewType].onCreateViewHolder(parent) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment