-
-
Save SimonVT/ad0290346ec6d25ffbb2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CatchSystemInsets extends FrameLayout { | |
private static final boolean CATCH_INSETS = Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP; | |
WindowInsets insets; | |
public CatchSystemInsets(Context context) { | |
super(context); | |
init(); | |
} | |
public CatchSystemInsets(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
init(); | |
} | |
public CatchSystemInsets(Context context, AttributeSet attrs, int defStyleAttr) { | |
super(context, attrs, defStyleAttr); | |
init(); | |
} | |
public CatchSystemInsets(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { | |
super(context, attrs, defStyleAttr, defStyleRes); | |
init(); | |
} | |
private void init() { | |
if (CATCH_INSETS) { | |
setFitsSystemWindows(true); | |
setSystemUiVisibility(SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN); | |
} | |
} | |
@Override public WindowInsets onApplyWindowInsets(WindowInsets insets) { | |
if (CATCH_INSETS) { | |
this.insets = new WindowInsets(insets); | |
return insets.consumeSystemWindowInsets(); | |
} | |
return insets; | |
} | |
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { | |
if (CATCH_INSETS) { | |
if (insets != null) { | |
for (int i = 0, childCount = getChildCount(); i < childCount; i++) { | |
View child = getChildAt(i); | |
child.dispatchApplyWindowInsets(insets); | |
} | |
} | |
} | |
super.onMeasure(widthMeasureSpec, heightMeasureSpec); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment