public void getSize(SizeReadyCallback cb) { int currentWidth = getViewWidthOrParam(); int currentHeight = getViewHeightOrParam(); if (isSizeValid(currentWidth) && isSizeValid(currentHeight)) { cb.onSizeReady(currentWidth, currentHeight); } else { // We want to notify callbacks in the order they were added and we only expect one or two callbacks to // be added a time, so a List is a reasonable choice. if (!cbs.contains(cb)) { cbs.add(cb); } if (layoutListener == null) { final ViewTreeObserver observer = view.getViewTreeObserver(); layoutListener = new SizeDeterminerLayoutListener(this); observer.addOnPreDrawListener(layoutListener); } } }
private void checkCurrentDimens() { if (cbs.isEmpty()) { return; } int currentWidth = getViewWidthOrParam(); int currentHeight = getViewHeightOrParam(); if (!isSizeValid(currentWidth) || !isSizeValid(currentHeight)) { return; } notifyCbs(currentWidth, currentHeight); // Keep a reference to the layout listener and remove it here // rather than having the observer remove itself because the observer // we add the listener to will be almost immediately merged into // another observer and will therefore never be alive. If we instead // keep a reference to the listener and remove it here, we get the // current view tree observer and should succeed. ViewTreeObserver observer = view.getViewTreeObserver(); if (observer.isAlive()) { observer.removeOnPreDrawListener(layoutListener); } layoutListener = null; }