Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save manavtamboli/eb780294aa2cc28c33550f995a3ae6eb to your computer and use it in GitHub Desktop.

Select an option

Save manavtamboli/eb780294aa2cc28c33550f995a3ae6eb to your computer and use it in GitHub Desktop.
// Make the class abstract
abstract class AdapterX <T>(
@LayoutRes private val layoutResId : Int,
diff : DiffUtil.ItemCallback<T>
) : ListAdapter<T, AdapterX.ViewHolder>(diff){
// An Extension function for ViewHolder with the List Item as Parameter
// Make it abstract so that it can be overridden
abstract fun ViewHolder.onBind(item : T)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent.context).inflate(layoutResId, parent, false)
return ViewHolder(view)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val item = getItem(position)
// Calling the extension method
holder.onBind(item)
}
class ViewHolder(view : View) : RecyclerView.ViewHolder(view)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment