Skip to content

Instantly share code, notes, and snippets.

@cbeyls
Last active February 5, 2020 10:58
Show Gist options
  • Star 34 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save cbeyls/ab6903e103475bd4d51b to your computer and use it in GitHub Desktop.
Save cbeyls/ab6903e103475bd4d51b to your computer and use it in GitHub Desktop.
A fragment container enabling the use of android:fitsSystemWindows in fragment layouts.
package be.digitalia.common.widgets;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Rect;
import android.os.Build;
import android.support.v4.util.ObjectsCompat;
import android.util.AttributeSet;
import android.view.View;
import android.view.WindowInsets;
import android.widget.FrameLayout;
/**
* A FrameLayout which memorizes the window insets and propagates them to child views before they are measured.
* You can use this layout as a fragment container in place of a standard FrameLayout to
* propagate window insets to attached fragments.
*
* @author Christophe Beyls
*/
public class WindowInsetsFrameLayout extends FrameLayout {
private Object mLastInsets;
public WindowInsetsFrameLayout(Context context) {
super(context);
}
public WindowInsetsFrameLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public WindowInsetsFrameLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@TargetApi(Build.VERSION_CODES.KITKAT_WATCH)
@Override
public WindowInsets onApplyWindowInsets(WindowInsets insets) {
if (!ObjectsCompat.equals(mLastInsets, insets)) {
mLastInsets = insets;
requestLayout();
}
return insets.consumeSystemWindowInsets();
}
@SuppressWarnings("deprecation")
@Override
protected boolean fitSystemWindows(Rect insets) {
if (!ObjectsCompat.equals(mLastInsets, insets)) {
if (mLastInsets == null) {
mLastInsets = new Rect(insets);
} else {
((Rect) mLastInsets).set(insets);
}
requestLayout();
}
return true;
}
@SuppressLint("DrawAllocation")
@SuppressWarnings("deprecation")
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (mLastInsets != null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
final WindowInsets wi = (WindowInsets) mLastInsets;
final int childCount = getChildCount();
for (int i = 0; i < childCount; i++) {
final View child = getChildAt(i);
if (child.getVisibility() != GONE) {
child.dispatchApplyWindowInsets(wi);
}
}
} else {
super.fitSystemWindows(new Rect((Rect) mLastInsets));
}
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
@afmjoaa
Copy link

afmjoaa commented Aug 10, 2018

Awesome work Bruh ...

@YuriPopiv
Copy link

Just amazing
one problem - onMeasure is triggered 100 times

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment