简体   繁体   English

何时在Layouting Process中调用onAttachedToWindow

[英]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 . 我加入OnGlobalLayoutListeneronAttachedToWindow和删除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/DetachonMeasureonLayout传递同步。

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.

相关问题 onAttachedToWindow()没有被调用 - onAttachedToWindow() is not being called Android是否可以在调用onAttachedToWindow()时隐藏状态栏? - Android Can we hide status bar when onAttachedToWindow() is called? Android 视图 - onAttachedToWindow 和 onDetachedFromWindow - 它们在活动生命周期中何时被调用? - Android view - onAttachedToWindow and onDetachedFromWindow - when are they called in the activity lifecycle? 谁在何时调用Activity onAttachedToWindow方法 - Who did call Activity onAttachedToWindow method and when 当我使用onAttachedToWindow时无法全屏活动 - Unable to full screen activity when i use onAttachedToWindow RxKotlin - 调用dispose时进程终止 - RxKotlin - Process gets terminated when dispose is called 尝试启动进程时不调用 ProcessBuilder - ProcessBuilder is not called when trying to start a process 即使强制关闭应用程序进程,也保证可以调用onPause()吗? - Is onPause() guaranteed to be called, even when force closing the app process? 防止在进程被杀死然后重新启动时调用 onCreateDialog - Prevent onCreateDialog being called when process is killed then relaunched 当Android系统杀死进程以声明内存时会调用onSaveInstanceState吗? - Is onSaveInstanceState called when the android system kills the process to claim memory?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM