Created
April 3, 2017 02:07
-
-
Save PaeP3nguin/4e41f7e76be452fe2f78d3c534fb8dd1 to your computer and use it in GitHub Desktop.
FrameLayout that saves and dispatches window insets to all new children
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
package yours.here; | |
import android.content.Context; | |
import android.os.Build; | |
import android.support.annotation.Nullable; | |
import android.support.annotation.RequiresApi; | |
import android.support.v4.view.OnApplyWindowInsetsListener; | |
import android.support.v4.view.ViewCompat; | |
import android.support.v4.view.WindowInsetsCompat; | |
import android.util.AttributeSet; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.FrameLayout; | |
/** | |
* FrameLayout that saves window insets and dispatches them to all new children | |
* Suitable as a fragment container with fragments that use fitsSystemWindows | |
*/ | |
public class InsetFrameLayout extends FrameLayout implements ViewGroup | |
.OnHierarchyChangeListener, OnApplyWindowInsetsListener { | |
private WindowInsetsCompat mInsets; | |
public InsetFrameLayout(Context context) { | |
super(context); | |
init(); | |
} | |
public InsetFrameLayout(Context context, @Nullable AttributeSet attrs) { | |
super(context, attrs); | |
init(); | |
} | |
public InsetFrameLayout(Context context, @Nullable AttributeSet attrs, int | |
defStyleAttr) { | |
super(context, attrs, defStyleAttr); | |
init(); | |
} | |
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) | |
public InsetFrameLayout(Context context, AttributeSet attrs, int defStyleAttr, int | |
defStyleRes) { | |
super(context, attrs, defStyleAttr, defStyleRes); | |
init(); | |
} | |
private void init() { | |
ViewCompat.setOnApplyWindowInsetsListener(this, this); | |
setOnHierarchyChangeListener(this); | |
} | |
@Override | |
public WindowInsetsCompat onApplyWindowInsets(View v, WindowInsetsCompat insets) { | |
mInsets = insets; | |
for (int index = 0; index < getChildCount(); index++) { | |
ViewCompat.dispatchApplyWindowInsets(getChildAt(index), insets); | |
} | |
return insets; | |
} | |
@Override | |
public void onChildViewAdded(View parent, View child) { | |
if (mInsets == null) { | |
return; | |
} | |
ViewCompat.dispatchApplyWindowInsets(child, mInsets); | |
} | |
@Override | |
public void onChildViewRemoved(View parent, View child) { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Saved my life.