Skip to content

Instantly share code, notes, and snippets.

@japgolly
Created April 16, 2013 09:37
Show Gist options
  • Save japgolly/5394669 to your computer and use it in GitHub Desktop.
Save japgolly/5394669 to your computer and use it in GitHub Desktop.
onGlobalLayout()
protected static final int IGNORE_BACKGROUND_WIDTH_CHANGES = 1 << 1;
protected static final int IGNORE_BACKGROUND_HEIGHT_CHANGES = 1 << 2;
private View globalLayoutView;
protected int globalLayoutWidth = -1, globalLayoutHeight = -1;
private int globalLayoutFlags = 0;
protected void addGlobalLayoutListener(View globalLayoutView) {
addGlobalLayoutListener(globalLayoutView, 0);
}
protected void addGlobalLayoutListener(View globalLayoutView, int flags) {
this.globalLayoutView = globalLayoutView;
this.globalLayoutFlags = flags;
globalLayoutView.getViewTreeObserver().addOnGlobalLayoutListener(this);
}
@Override
public void onGlobalLayout() {
// logLifecycle("onGlobalLayout");
final int w = globalLayoutView.getWidth();
final int h = globalLayoutView.getHeight();
if (((globalLayoutFlags & IGNORE_BACKGROUND_WIDTH_CHANGES) == 0 && w != globalLayoutWidth)
|| ((globalLayoutFlags & IGNORE_BACKGROUND_HEIGHT_CHANGES) == 0 && h != globalLayoutHeight)) {
globalLayoutWidth = w;
globalLayoutHeight = h;
// logLifecycle("onGlobalLayout2");
onGlobalLayout2();
}
}
protected void onGlobalLayout2() {
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addGlobalLayoutListener(incdecContainer, IGNORE_BACKGROUND_HEIGHT_CHANGES);
}
@Override
protected void onGlobalLayout2() {
// Distribute horizontal space evenly between incdec containers
final int hspace = globalLayoutWidth - (incdecContL.getWidth() << 1);
final int sideGaps = hspace / 3;
final int midGap = hspace - (sideGaps << 1);
((RelativeLayout.LayoutParams) incdecContL.getLayoutParams()).leftMargin = sideGaps;
((RelativeLayout.LayoutParams) incdecContR.getLayoutParams()).leftMargin = midGap;
// Sync h/v space between incdec views
final int paddingV = Math.min(incdecMaxPaddingV, midGap);
for (DurationIncDecView v : incDecViews) {
((LinearLayout.LayoutParams) v.getLayoutParams()).bottomMargin = paddingV;
}
incdecContainer.requestLayout();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment