force menu invalidation when expanding actionView from user interaction to properly layout menu items (#2503)

This commit is contained in:
MCAxiaz
2020-01-16 18:44:22 -08:00
committed by arkon
parent 73fbc81067
commit bed978a26a
4 changed files with 57 additions and 32 deletions
@@ -85,10 +85,11 @@ abstract class BaseController(bundle: Bundle? = null) : RestoreViewOnCreateContr
}
/**
* Workaround for disappearing menu items when collapsing an expandable item like a SearchView.
* Workaround for buggy menu item layout after expanding/collapsing an expandable item like a SearchView.
* This method should be removed when fixed upstream.
* Issue link: https://issuetracker.google.com/issues/37657375
*/
var expandActionViewFromInteraction = false
fun MenuItem.fixExpand(onExpand: ((MenuItem) -> Boolean)? = null, onCollapse: ((MenuItem) -> Boolean)? = null) {
setOnActionExpandListener(object : MenuItem.OnActionExpandListener {
override fun onMenuItemActionExpand(item: MenuItem): Boolean {
@@ -101,6 +102,25 @@ abstract class BaseController(bundle: Bundle? = null) : RestoreViewOnCreateContr
return onCollapse?.invoke(item) ?: true
}
})
if (expandActionViewFromInteraction) {
expandActionViewFromInteraction = false
expandActionView()
}
}
/**
* Workaround for menu items not disappearing when expanding an expandable item like a SearchView.
* [expandActionViewFromInteraction] should be set to true in [onOptionsItemSelected] when the expandable item is selected
* This method should be called as part of [MenuItem.OnActionExpandListener.onMenuItemActionExpand]
*/
fun invalidateMenuOnExpand(): Boolean {
return if (expandActionViewFromInteraction) {
activity?.invalidateOptionsMenu()
false
} else {
true
}
}
}