Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save samuel22gj/2f67e8797ac9a39ab2e923d26b1e2487 to your computer and use it in GitHub Desktop.
Save samuel22gj/2f67e8797ac9a39ab2e923d26b1e2487 to your computer and use it in GitHub Desktop.
/**
* @see <a href="https://medium.com/androiddevelopers/windows-insets-fragment-transitions-9024b239a436">Windows Insets + Fragment Transitions</a>
*/
public class DispatchWindowInsetsToAllChildrenListener implements OnApplyWindowInsetsListener {
@Override
public WindowInsetsCompat onApplyWindowInsets(View view, WindowInsetsCompat windowInsetsCompat) {
boolean consumed = false;
if (view instanceof ViewGroup) {
ViewGroup viewGroup = (ViewGroup) view;
// Dispatch the insets to the child
int childCount = viewGroup.getChildCount();
for (int i = 0; i < childCount; i++) {
View child = viewGroup.getChildAt(i);
WindowInsetsCompat childResult = ViewCompat.dispatchApplyWindowInsets(child, windowInsetsCompat);
// If the child consumed the insets, record it
if (childResult.isConsumed()) {
consumed = true;
}
}
}
// If any of the children consumed the insets, return an appropriate value
return consumed ? windowInsetsCompat.consumeSystemWindowInsets() : windowInsetsCompat;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment