简体   繁体   中英

When is onAttachedToWindow called in the Layouting Process

I'm not sure about the usage of onAttachedToWindow .

My question is basically about the documentation . Which mentions:

[...] it may be called any time before the first onDraw -- including before or after onMeasure(int, int)

I what to know:

when it is called before onMeasure and when is it called after.

The Story behind:

I am adding OnGlobalLayoutListener in onAttachedToWindow and remove it in onDetachedFromWindow . Because is somehow logic to me to handle the layouting when the view is added to the window. But I am concerned that the first onGlobalLayout calls get lost, if the Listener is not added yet. (because onMeasure usually happens during the layouting)

If someone got a better approach for my problem, feel free to give me a hint.

Docs are correct, and you should not rely on onWindowAttach/Detach being in sync with onMeasure or onLayout pass.

If your View class is interested in parent hierarchy changes, I'd advice against such a design. The Parent UI should notify sub-views of hierarchy changes. So, OnGlobalLayoutListener better be used by an enclosing UI class.

Also, View class has onSizeChanged() that you can override to detect when it has been measured up.

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