chrisbanes / insetter

Insetter is a library to help apps handle WindowInsets more easily
https://chrisbanes.github.io/insetter
Apache License 2.0
1.13k stars 42 forks source link

Not compatible with CollapsingToolbarLayout #41

Closed percula closed 4 years ago

percula commented 4 years ago

When using this library with a CollapsingToolbarLayout, the insets on the bottom (the top works) are not set or are overwritten. When I comment out the CollapsingToolbarLayout, the problem goes away. Please see the sample project I created: https://github.com/percula/CollapsingToolbarInsets

In the following screenshot, the FAB and the padding of the text should be above the nav bar. Screenshot_1576528495

chrisbanes commented 4 years ago

Yeah, this will happen with CoordinatorLayout. It is very opinionated about inset handling (which is one of the reasons why I wrote Insetter), which makes custom inset handling hard.

You can turn all of that off by setting android:fitsSystemWindows=“false” on the CoordinatorLayout. Then Insetter should work like on any other view.

percula commented 4 years ago

Thanks, but unfortunately that doesn't fix it. I set android:fitsSystemWindows="false" and nothing changed 😟. I updated my sample with this.

My theory is that the OnApplyWindowInsetsListener that the CollapsingToolbarLayout adds is overwriting the work that Insetter is doing. I'm looking through the source now to find out why/how.

percula commented 4 years ago

Ok, I found a solution.

Turns out the CollapsingToolbarLayout is preventing the OnApplyWindowInsetsListener for Views outside the AppBarLayout from ever being notified. Listeners within the AppBarLayout were being notified as expected. This looks like a bug within CollapsingToolbarLayout, probably in CollapsingToolbarLayout.onWindowInsetChanged() line 295 where it consumes all the window insets instead of returning them.

As a work-around, I modified your applySystemWindows method to apply the listener to the rootView:

fun applySystemWindowsWithCollapsingToolbar(
        view: View,
        applyLeft: Boolean,
        applyTop: Boolean,
        applyRight: Boolean,
        applyBottom: Boolean
) {
    view.rootView.doOnApplyWindowInsets { _, insets, padding ->
        val left = if (applyLeft) insets.systemWindowInsetLeft else 0
        val top = if (applyTop) insets.systemWindowInsetTop else 0
        val right = if (applyRight) insets.systemWindowInsetRight else 0
        val bottom = if (applyBottom) insets.systemWindowInsetBottom else 0

        view.setPadding(
                padding.paddings.left + left,
                padding.paddings.top + top,
                padding.paddings.right + right,
                padding.paddings.bottom + bottom
        )
    }
}

However, this breaks the listeners within the AppBarLayout. So for now, I have two sets of binding adapters, one from Insetter and a custom set (above) just for this edge case.