Last active
November 10, 2024 14:25
-
-
Save cbeyls/ab6903e103475bd4d51b to your computer and use it in GitHub Desktop.
A fragment container enabling the use of android:fitsSystemWindows in fragment layouts.
This file contains hidden or 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
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); | |
} | |
} |
Awesome work Bruh ...
Just amazing
one problem - onMeasure is triggered 100 times
This class should not be used anymore: use FragmentContainerView
instead.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I rewrote this class recently, to optimize it for API 20+ while keeping compatibility with older Android versions.
On API 20+
onApplyWindowInsets()
will be called. These insets are read-only so we just pass them later as-is to child views without having to do any copy or transformation.On older versions
fitSystemWindows()
will be called, and the class makes two defensive copies of the insets to avoid issues if they are modified by other views.The insets are not passed to child views anymore when they arrive initially, or when a child view is added. Instead they are now dispatched to all child views at once in
onMeasure()
right before measuring. This behavior is similar to theDrawerLayout
implementation.