[英]When is onAttachedToWindow called in the Layouting Process
I'm not sure about the usage of onAttachedToWindow
. 我不确定
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)
[...]可以在第一次onDraw之前的任何时间调用它 - 包括onMeasure之前或之后(int,int)
I what to know: 我知道什么:
when it is called before onMeasure
and when is it called after. 在
onMeasure
之前调用它时以及何时调用它。
The Story behind: 背后的故事:
I am adding OnGlobalLayoutListener
in onAttachedToWindow
and remove it in onDetachedFromWindow
. 我加入
OnGlobalLayoutListener
在onAttachedToWindow
和删除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.
但我担心第一个onGlobalLayout调用会丢失,如果还没有添加Listener。 (because onMeasure usually happens during the layouting)
(因为onMeasure通常在布局期间发生)
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. 文档是正确的,你不应该依赖
onWindowAttach/Detach
与onMeasure
或onLayout
传递同步。
If your View
class is interested in parent hierarchy changes, I'd advice against such a design. 如果您的
View
类对父层次结构更改感兴趣,我会建议不要使用这样的设计。 The Parent UI should notify sub-views of hierarchy changes. 父UI应该通知子视图的层次结构更改。 So,
OnGlobalLayoutListener
better be used by an enclosing UI class. 因此,
OnGlobalLayoutListener
最好由封闭的UI类使用。
Also, View
class has onSizeChanged()
that you can override to detect when it has been measured up. 此外,
View
类具有onSizeChanged()
,您可以覆盖它以检测何时测量它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.