Skip to content

Instantly share code, notes, and snippets.

@DjakaTechnology
Last active January 15, 2020 18:36
Show Gist options
  • Select an option

  • Save DjakaTechnology/ff4f2ac5ba276a48434a84688e8eab86 to your computer and use it in GitHub Desktop.

Select an option

Save DjakaTechnology/ff4f2ac5ba276a48434a84688e8eab86 to your computer and use it in GitHub Desktop.
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