addOnLayoutChangeListener VS addOnGlobalLayoutListener VS交(Runnable的R) [英] addOnLayoutChangeListener vs addOnGlobalLayoutListener vs post(Runnable r)

查看:2898
本文介绍了addOnLayoutChangeListener VS addOnGlobalLayoutListener VS交(Runnable的R)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题: 假设我们有简单的例子:我们有查看,然后,我们必须表现出对这一观点的一些数据。我们用静态方法 showData(查看视图)可以做到这一点。我想知道确切的时刻,视图布局进行测量,我可以访问的的getWidth() 的getHeight()并确保这是最后的宽度和我的看法高度。

我所知道的:(不知道我是100%正确的):

我知道3种不同的方法来做到这一点。

  • view.addOnLayoutChangeListener - 我们知道什么时候布局发生变化,我们 能得到高度和宽度
  • view.getViewTreeObserver()addOnGlobalLayoutListener - 我的情况 几乎是相同的,但后来我们得到的信息。因为layoutChange 气泡从儿童到根部,并globalLayout从根到儿童 (是吗?)。
  • view.post(Runnable的R) - 是工作,但我不知道为什么可运行是 张贴到UI线程执行视图后测量。

: 什么是要知道这种观点被测量的最佳方法?为什么?

解决方案
  

什么是知道这一观点最好的办法是测量?

最好的方法? OnGlobalLayoutListener View.getMeasuredHeight() View.getMeasuredWidth()

  

为什么?

由于你妈妈知道你比你更了解你自己,所以它能够更好地咨询的ViewGroup 有关其状态的自动谈到孩子卷。

View.post()入队的UI队列的过程。 UI队列= {1,2},你叫这条线= {1,2,可运行}后来经过。所以肯定会1,2后运行

PROBLEM: Suppose we have simple case: we have view and we have to show some data on this view. We use static method showData(View view) to do this. I want to know the exact moment when view layout is measured and I can access getWidth() getHeight() and be sure that this is final width and height of my view.

WHAT I KNOW: (not sure I am 100% right):

I know 3 different approaches to do this

  • view.addOnLayoutChangeListener - we know when layout is changed, we can get height and width
  • view.getViewTreeObserver().addOnGlobalLayoutListener - for my case almost the same, but we get information later. Because layoutChange bubbles from children to root, and globalLayout from root to children (right?).
  • view.post(Runnable r) - is working, but I do not know why runnable is posted to ui thread executes after view is measured.

QUESTION: What is the best approach to know that view is measured? and why?

解决方案

What is the best approach to know that view is measured?

The best Approach? OnGlobalLayoutListener with View.getMeasuredHeight() or View.getMeasuredWidth()

and why?

Because your mom knows you better than you know yourself, so its better to consult the ViewGroup about its state which automatically speaks volumes about the children.

View.post() enqueues process on the ui queue. Ui queue ={1,2}, later after you call that line = {1,2,runnable}. so definitely it will run after 1,2

这篇关于addOnLayoutChangeListener VS addOnGlobalLayoutListener VS交(Runnable的R)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆