Skip to content

Instantly share code, notes, and snippets.

@tonnylitao
Last active June 17, 2020 09:48
Show Gist options
  • Save tonnylitao/bcbe6ebb864014c833d0e4cfc1bd1eab to your computer and use it in GitHub Desktop.
Save tonnylitao/bcbe6ebb864014c833d0e4cfc1bd1eab to your computer and use it in GitHub Desktop.
override fun getItemViewType(position: Int)
= requireNotNull(
getItem(position)?.layoutId ?: placeholderId
){
"item at $position is null"
}
override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int)
= ViewHolder(
DataBindingUtil.inflate(
LayoutInflater.from(parent.context),
viewType, parent, false
)
).also { viewHolder ->
if (viewType != placeholderId
&& listener != null) {
viewHolder.itemView.setOnClickListener {
val position = viewHolder.absoluteAdapterPosition
val item = getItem(position)
?: return@setOnClickListener
listener.invoke(
this@RecyclerAdapter,
position,
item)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment