Thursday, 28 January 2021

How to listen for multiple GlobalLayout events

I am trying to listen for GlobalLayout using

int c=0;
view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        c++; //without removing the listener c will grow for ever even though there is not GlobalLayout events
    }
});

but it's called endlessly.


I know I should remove the listener like this: view.getViewTreeObserver().removeOnGlobalLayoutListener(this);

But I want to keep the listener alive for the next GlobalLayout events.

Currently I am just trying to listen for view position changing using this question I tried onPreDraw but it is the same.


Is it possible to listen for several GlobalLayout events?



Thanks in advance.



from How to listen for multiple GlobalLayout events

No comments:

Post a Comment