简体   繁体   中英

How to know whether the layout process is finished?

I need to call requestLayout() in my custome view, but I noticed

This should not be called while the view hierarchy is currently in a layout pass ({@link # isInLayout() }.

So I deciede to use this code:

if(isInLayout()) {
    // request layout later
} else {
    requestLayout();
}

But the question is that I don't know how to request layout later, can I use addOnLayoutChangeListener ?

just like this:

addOnLayoutChangeListener(new OnLayoutChangeListener() {
    @Override
    public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
        requestLayout();
    }
});

If you want to request a new layout after the layout pass that is in progress completes, take a look at ViewTreeObserver.OnPreDrawListener and the PreDraw() method.

onPreDraw

boolean onPreDraw ()

Callback method to be invoked when the view tree is about to be drawn. At this point, all views in the tree have been measured and given a frame. Clients can use this to adjust their scroll bounds or even to request a new layout before drawing occurs.

There are other methods that are part of the ViewTreeObserver.OnPreDrawListener interface that may also be what you are looking for.


You can also look at post() that is part of View . (See documentation here) . I believe that the Runnable that you post will be executed after the layout is completed on the view. That may be more of what you are looking for. Also take a look at the accepted answer to this Stack Overflow question .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM