Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove listener from ViewTreeObserver

Tags:

android

I need to listen for exact one global layouting event to initially set correct scrolling positions. After searching a little why my calls to scrollTo(x,y) 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 GlobalLayoutListener and defer my call to scrollTo().

The problem is, that I only want to do this scrolling once. So I figured I could simply call removeGlobalOnLayoutListener() to stop listening. That resulted in a Exception: IllegalStateException: This ViewTreeObserver is not alive, call getViewTreeObserver() again. 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.

My current iteration of the code looks like this. What could I change to make sure the call to scrollToGridPos() only takes place once? I know I could just add a local field mHasFired to the inner class but that seems like very dirty hack to me ...

final ViewTreeObserver vto = mLayout.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
    public void onGlobalLayout() {
        scrollToGridPos(getCenterPoint(), false);
        if (vto.isAlive()) {
            vto.removeGlobalOnLayoutListener(this);
        }
    }
});
like image 657
Marcus Riemer Avatar asked Oct 04 '14 12:10

Marcus Riemer


1 Answers

You should just be using mLayout.getViewTreeObserver() in onGlobalLayout(), rather than trying to access the old one. Eg.

mLayout.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
    public void onGlobalLayout() {
        scrollToGridPos(getCenterPoint(), false);
        mLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this);        
    }
});
like image 135
Paul Burke Avatar answered Nov 15 '22 21:11

Paul Burke