Closed maxrave-dev closed 1 day ago
@maxrave-dev I'm fairly sure you're facing this issue: https://github.com/material-components/material-components-android/issues/1310 and the workaround is to clear collapsing toolbar insets via ViewCompat.setOnApplyWindowInsetsListener(collapsingToolbarLayout, null)
@mateuszkwiecinski hi, this fixes the problem but creates another. When setting the listener to null, on api >31 or >32 (i dont fully remember), the scrim color set in Collapsing Toolbar is transparent although i set a different color, programmatically cant change it neither. Api <=30 doesnt seem to face this problem and show scrim color correctly. Any suggestion?? Ive been stuck on this problem for so long and looking on te internet doesnt seem to help☹ EDIT: IF SOMEONE COULD HELP ME I WOULD APPRECIATE IT SO MUCH
I managed to solve a similar issue:
https://github.com/chrisbanes/insetter/assets/14194998/cddbd19f-d74c-4e13-8a7e-2a298ac87a59
Notice the toolbar not being correctly inset due to CollapsingToolbarLayout
consumption of insets.
Next is after forcefully removing OnApplyWindowInsetsListener
from CollapsingToolbarLayout
i.e.
ViewCompat.setOnApplyWindowInsetsListener(collapsingToolbarLayout, null)
https://github.com/chrisbanes/insetter/assets/14194998/4700e1c1-c9fe-4a2f-a046-b2545c3ce5a0
As seen now CollapsingToolbarLayout
fails to handle scrim of the Toolbar
correctly.
And the workaround for this problem is by updating the CollapsingToolbarLayout
's scrimVisibleHeightTrigger
by applying a custom OnApplyWindowInsetsListener
to the CollapsingToolbarLayout
:
class CustomOnApplyWindowInsetsListener(
private val toolbar: Toolbar
) : OnApplyWindowInsetsListener {
private var lastInsets: WindowInsetsCompat? = null
override fun onApplyWindowInsets(v: View, insets: WindowInsetsCompat): WindowInsetsCompat {
if (v !is CollapsingToolbarLayout) return insets
var newInsets: WindowInsetsCompat? = null
if (ViewCompat.getFitsSystemWindows(v)) {
// If we're set to fit system windows, keep the insets
newInsets = insets
}
// If our insets have changed, keep them and invalidate the scroll ranges...
if (lastInsets != newInsets) {
lastInsets = newInsets
v.requestLayout()
}
toolbar.doOnLayout { toolbar ->
v.scrimVisibleHeightTrigger =
insets.getInsets(WindowInsetsCompat.Type.systemBars()).top +
toolbar.bottom
}
return insets // Unlike the original handling, insets aren't consumed (in order to let Toolbar apply margin based on insets)
}
}
And the result will look like this:
https://github.com/chrisbanes/insetter/assets/14194998/445c4d65-8dd5-4fe9-8197-4a89a4558105
Inside my XML
In my fragment
onCreateView
:My result:
Toolbar still be overlap How to fix it? Thanks for make a best library