There are two effects that I have the deepest experience with: 1. Everyone knows that calling view.getHeight() in the onCreate() method will return 0, but you can
to get his height. 2. Use the View.postDelayed() method to prevent multiple unnecessary click events from being triggered by multiple clicks on the view within a period of time:
The post contains a Runnable, which means that the work of the Runnable is executed in the UI thread. The UI can be updated directly in the Runnable. However, it should be noted that the Runnable here is not a new thread. Do not perform too complex and time-consuming work to avoid Blocking causes ANR.
Android APP automatically starts a UIThread, the main thread, after startup. This thread is mainly used to distribute user operation events and interface drawing events. Therefore, in order to maintain a better user experience, time-consuming operations will be executed in other threads. After the execution is completed, the results will be returned to the main thread and displayed. However, the UI cannot be directly manipulated from other threads. This requires a mechanism to transfer information between threads, namely Handler, message queue and Looper.
The Android platform mainly accesses the main thread from other threads in the following ways: Activity.runOnUiThread(Runnable) View.post(Runnable) View.postDelayed(Runnable, long) Handler
For the running process behind View.post, you can refer to the following information: A kind of implementation and principle of DelayLoad (Part 2) After reading this analysis, you will understand why View is called in onCreate Only .post can get the height of a View
There are two effects that I have the deepest experience with:
1. Everyone knows that calling view.getHeight() in the onCreate() method will return 0, but you can
to get his height.
2. Use the View.postDelayed() method to prevent multiple unnecessary click events from being triggered by multiple clicks on the view within a period of time:
The post contains a Runnable, which means that the work of the Runnable is executed in the UI thread. The UI can be updated directly in the Runnable. However, it should be noted that the Runnable here is not a new thread. Do not perform too complex and time-consuming work to avoid Blocking causes ANR.
View
has aHandler
by default, which belongs tomainLooper
.View.post
is equivalent to thisHandler.post
Android APP automatically starts a UIThread, the main thread, after startup. This thread is mainly used to distribute user operation events and interface drawing events. Therefore, in order to maintain a better user experience, time-consuming operations will be executed in other threads. After the execution is completed, the results will be returned to the main thread and displayed. However, the UI cannot be directly manipulated from other threads. This requires a mechanism to transfer information between threads, namely Handler, message queue and Looper.
The Android platform mainly accesses the main thread from other threads in the following ways:
Activity.runOnUiThread(Runnable)
View.post(Runnable)
View.postDelayed(Runnable, long)
Handler
For the running process behind View.post, you can refer to the following information:
A kind of implementation and principle of DelayLoad (Part 2)
After reading this analysis, you will understand why View is called in onCreate Only .post can get the height of a View
View.post is used to put the Runnable into the queue of the UI thread for execution