Fixed menu inflating unnecessarily on every click

This commit is contained in:
Viktor De Pasquale 2020-02-01 14:51:20 +01:00 committed by John Wu
parent dd9129017f
commit f8bea66313

View File

@ -470,18 +470,19 @@ interface OnPopupMenuItemClickListener {
@BindingAdapter("popupMenu", "popupMenuOnClickListener", requireAll = false)
fun View.setPopupMenu(popupMenu: Int, listener: OnPopupMenuItemClickListener) {
tag = tag as? PopupMenu ?: let {
val menu = tag as? PopupMenu ?: let {
val themeWrapper = ContextThemeWrapper(context, R.style.Foundation_PopupMenu)
PopupMenu(themeWrapper, this)
}
setOnClickListener {
(tag as PopupMenu).apply {
menuInflater.inflate(popupMenu, menu)
tag = menu.apply {
this.menu.clear()
menuInflater.inflate(popupMenu, this.menu)
setOnMenuItemClickListener {
listener.onMenuItemClick(it.itemId)
true
}
show()
}
}
setOnClickListener {
(tag as PopupMenu).show()
}
}