Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
402 views
in Technique[技术] by (71.8m points)

android - Remove listener from ViewTreeObserver

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);
        }
    }
});
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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);        
    }
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...