gpt4 book ai didi

android - Layouting Process什么时候调用onAttachedToWindow

转载 作者:太空宇宙 更新时间:2023-11-03 13:52:13 25 4
gpt4 key购买 nike

我不确定 onAttachedToWindow 的用法。

我的问题基本上是关于 documentation 的.其中提到:

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

我知道什么:

什么时候在 onMeasure 之前调用,什么时候在之后调用。

背后的故事:

我在 onAttachedToWindow 中添加 OnGlobalLayoutListener 并在 onDetachedFromWindow 中删除它。因为在将 View 添加到窗口时处理布局对我来说是某种逻辑。但我担心如果尚未添加监听器,第一个 onGlobalLayout 调用会丢失。 (因为onMeasure一般发生在布局的时候)

如果有人有更好的方法来解决我的问题,请随时给我提示。

最佳答案

文档是正确的,你不应该依赖于 onWindowAttach/DetachonMeasureonLayout pass 同步。

如果您的 View 类对父层次结构的更改感兴趣,我建议不要采用这种设计。父 UI 应通知 subview 层次结构更改。因此,OnGlobalLayoutListener 最好由封闭的 UI 类使用。

此外,View 类有 onSizeChanged()您可以覆盖它以检测它何时被测量。

关于android - Layouting Process什么时候调用onAttachedToWindow,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33758969/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com