Skip to content

Instantly share code, notes, and snippets.

@mustafayigitt
Last active October 24, 2020 15:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mustafayigitt/35beb1e5199575f4281e995892685634 to your computer and use it in GitHub Desktop.
Save mustafayigitt/35beb1e5199575f4281e995892685634 to your computer and use it in GitHub Desktop.
Generic ListAdapter with Kotlin
abstract class GenericListAdapter<T : Any>(
@IdRes val layoutId: Int,
inline val bind: (item: T, holder: BaseViewHolder, itemCount: Int) -> Unit
) : ListAdapter<T, BaseViewHolder>(BaseItemCallback<T>()) {
override fun onBindViewHolder(holder: BaseViewHolder, position: Int) {
bind(getItem(position), holder, itemCount)
}
override fun getItemViewType(position: Int) = layoutId
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder {
val root = LayoutInflater.from(parent.context).inflate(
viewType, parent, false
)
return BaseViewHolder(root as ViewGroup)
}
override fun getItemCount() = currentList.size
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment