Skip to content

Instantly share code, notes, and snippets.

@Hackforid
Created March 30, 2017 13:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Hackforid/1a86b5fcda14cdd896849ec4c9ccd3eb to your computer and use it in GitHub Desktop.
Save Hackforid/1a86b5fcda14cdd896849ec4c9ccd3eb to your computer and use it in GitHub Desktop.
CompatCollapsingToolbarLayout
public class CompatCollapsingToolbarLayout extends CollapsingToolbarLayout {
private boolean mLayoutReady;
public CompatCollapsingToolbarLayout(Context context) {
this(context, null);
}
public CompatCollapsingToolbarLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CompatCollapsingToolbarLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
if (!mLayoutReady) {
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) {
if ((getWindowSystemUiVisibility() &
(SYSTEM_UI_FLAG_LAYOUT_STABLE|SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN)) ==
(SYSTEM_UI_FLAG_LAYOUT_STABLE|SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN)) {
try {
Field mLastInsets = CollapsingToolbarLayout.class.getDeclaredField("mLastInsets");
mLastInsets.setAccessible(true);
mLastInsets.set(this, null);
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
mLayoutReady = true;
}
}
super.onLayout(changed, left, top, right, bottom);
}
public void refix() {
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) {
if ((getWindowSystemUiVisibility() &
(SYSTEM_UI_FLAG_LAYOUT_STABLE|SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN)) ==
(SYSTEM_UI_FLAG_LAYOUT_STABLE|SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN)) {
try {
Field mLastInsets = CollapsingToolbarLayout.class.getDeclaredField("mLastInsets");
mLastInsets.setAccessible(true);
mLastInsets.set(this, null);
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
mLayoutReady = true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment