Skip to content

Instantly share code, notes, and snippets.

@arildojr7
Last active July 9, 2019 12:43
Show Gist options
  • Save arildojr7/9e80067f00c886e841a824e84290e4ba to your computer and use it in GitHub Desktop.
Save arildojr7/9e80067f00c886e841a824e84290e4ba to your computer and use it in GitHub Desktop.
interface OnClickMenu {
fun onClickMenu(menu: MenuAccountEnum)
}
class MenuAccountAdapter(
var menuItems: List<MenuAccountEnum>, private val clickListener: (MenuAccountEnum) -> Unit
) : RecyclerView.Adapter<MenuAccountAdapter.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = DataBindingUtil.inflate<ItemMenuAccountBinding>(
LayoutInflater.from(parent.context),
R.layout.item_menu_account,
parent,
false
)
return ViewHolder(view, clickListener)
}
override fun getItemCount() = menuItems.size
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.bind(menuItems[position])
}
class ViewHolder(private val binding: ItemMenuAccountBinding, val clickListener: (MenuAccountEnum) -> Unit) :
RecyclerView.ViewHolder(binding.root), OnClickMenu {
override fun onClickMenu(menu: MenuAccountEnum) {
clickListener(menu)
}
fun bind(menu: MenuAccountEnum) {
binding.menu = menu
binding.onClick = this
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment