반응형
post() gets called after setContentView().

Method setContentView() ends up in calling ViewGroup.addView() of the top view, and addView() call always triggers requestLayout().

In turn, requestLayout() posts a task to the main thread to be executed later.

This task will execute measure and layout on the view hierarchy.

Now if you post another task it will be put into the queue after layout task and, as the result, always executed after measure and layout happen. Thus you will always have valid sizes.

parentView.post(new Runnable() {
  // Post in the parent's message queue to make sure the parent
  // lays out its children before you call getHitRect()
  @Override
  public void run() {
  /// do UI stuff
  }
});

 

Post runnable은 setContentView가 호출되고 나서야 불린다.

즉, setContentView는 상위 뷰의 addView의 호출로 끝이나며 addView는 항상 requestLayout을 호출하게 된다.

 

그리고나서 requestLayout은 나중에 실행될 메인 스레드에 task를 post한다.

 

만약 다른 task를 post한다면 layout task 이후로 queue에 쌓이게 되고 결과적으로 항상 measure와 layout가 일어난 후에 실행된다.

 

따라서 항상 post이후에는 valid한 size를 얻을 수 있는 것이다.

 

 

https://stackoverflow.com/questions/21937224/does-posting-runnable-to-an-ui-thread-guarantee-that-layout-is-finished-when-it

 

Does posting Runnable to an UI thread guarantee that layout is finished when it is run?

First thing! I do know about ViewTreeObserver.onGlobalLayoutListener. What made me ask this question is the following notice on Android developer documentation website: The snippet below does the

stackoverflow.com

 

 

반응형