title: Remove listener from ViewTreeObserver
body: <p>I need to listen for exact one global layouting event to initially set correct scrolling positions. After searching a little why my calls to <code>scrollTo(x,y)</code> seem to be ignored I discovered these can only be called in an meaningful way once the whole layout is known. So I am registering a <code>GlobalLayoutListener</code> and defer my call to <code>scrollTo()</code>.</p>
  
  <p>The problem is, that I only want to do this scrolling once. So I figured I could simply call <code>removeGlobalOnLayoutListener()</code> to stop listening. That resulted in a Exception: <code>IllegalStateException: This ViewTreeObserver is not alive, call getViewTreeObserver() again</code>. So I thought I am fine, if the Observer is not alive it won't fire any events. But unfortunately it does: My view is scrolled every time the layout somehow changes.</p>
  
  <p>My current iteration of the code looks like this. What could I change to make sure the call to <code>scrollToGridPos()</code> only takes place once? I know I could just add a local field <code>mHasFired</code>  to the inner class but that seems like very dirty hack to me ...</p>
  
  <pre><code>final ViewTreeObserver vto = mLayout.getViewTreeObserver();
  vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
      public void onGlobalLayout() {
          scrollToGridPos(getCenterPoint(), false);
          if (vto.isAlive()) {
              vto.removeGlobalOnLayoutListener(this);
          }
      }
  });
  </code></pre>