fitsSystemWindows effect gone for fragments added via FragmentTransaction

前端 未结 6 1020

I have an Activity with navigation drawer and full-bleed Fragment (with image in the top that must appear behind translucent system bar on Lollipop). While I had an interim

6条回答
  •  有刺的猬
    2020-11-28 02:58

    When you use , the layout returned in your Fragment's onCreateView is directly attached in place of the tag (you'll never actually see a tag if you look at your View hierarchy.

    Therefore in the case, you have

    DrawerLayout
      CoordinatorLayout
        AppBarLayout
        ...
      NavigationView
    

    Similar to how cheesesquare works. This works because, as explained in this blog post, DrawerLayout and CoordinatorLayout both have different rules on how fitsSystemWindows applies to them - they both use it to inset their child Views, but also call dispatchApplyWindowInsets() on each child, allowing them access to the fitsSystemWindows="true" property.

    This is a difference from the default behavior with layouts such as FrameLayout where when you use fitsSystemWindows="true" is consumes all insets, blindly applying padding without informing any child views (that's the 'depth first' part of the blog post).

    So when you replace the tag with a FrameLayout and FragmentTransactions, your view hierarchy becomes:

    DrawerLayout
      FrameLayout
        CoordinatorLayout
          AppBarLayout
          ...
      NavigationView
    

    as the Fragment's view is inserted into the FrameLayout. That View doesn't know anything about passing fitsSystemWindows to child views, so your CoordinatorLayout never gets to see that flag or do its custom behavior.

    Fixing the problem is actually fairly simple: replace your FrameLayout with another CoordinatorLayout. This ensures the fitsSystemWindows="true" gets passed onto the newly inflated CoordinatorLayout from the Fragment.

    Alternate and equally valid solutions would be to make a custom subclass of FrameLayout and override onApplyWindowInsets() to dispatch to each child (in your case just the one) or use the ViewCompat.setOnApplyWindowInsetsListener() method to intercept the call in code and dispatch from there (no subclass required). Less code is usually the easiest to maintain, so I wouldn't necessarily recommend going these routes over the CoordinatorLayout solution unless you feel strongly about it.

提交回复
热议问题