Last active
October 24, 2020 15:36
-
-
Save mustafayigitt/35beb1e5199575f4281e995892685634 to your computer and use it in GitHub Desktop.
Generic ListAdapter with Kotlin
This file contains 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
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