提起View.post()或者View.postDelay(),相信不少童鞋一点都不陌生,它用得最多的有两个功能
1)在子线程中更新UI;
2)获取View的宽高等属性值。
一、在子线程中更新UI
一般我们通过使用View.post()实现在子线程中更新UI的示例大致如下:
1 private Button mStartBtn;
2 @Override
3 protected void onCreate(Bundle savedInstanceState) {
4 super.onCreate(savedInstanceState);
5 setContentView(R.layout.activity_intent_service);
6 mStartBtn = findViewById(R.id.start);
7 new Thread(new Runnable() {
8 @Override
9 public void run() {
10 mStartBtn.post(new Runnable() {
11 @Override
12 public void run() {
13 //处理一些耗时操作
14 mStartBtn.setText("end");
15 }
16 });
17 }
18 }).start();
19 }
第7行开启了一个线程,第10行通过调用post方法,使得在第14行实现了修改自身UI界面的显示。那么,这是如何实现的呢?我们进入post源码看看。
//============================View.java============================= 1 /**
2 * <p>Causes the Runnable to be added to the message queue.
3 * The runnable will be run on the user interface thread.</p>
4 * ......
5 */
6 public boolean post(Runnable action) {
7 final AttachInfo attachInfo = mAttachInfo;
8 if (attachInfo != null) {
9 return attachInfo.mHandler.post(action); //①
10 }
11 // Postpone the runnable until we know on which thread it needs to run.
12 // Assume that the runnable will be successfully placed after attach.
13 getRunQueue().post(action); //②
14 return true;
15 }
第1~5行的注释说,该方法将Runnable添加到消息队列中,该Runnable将在UI线程运行。这就是该方法的作用,添加成功了就会返回true。
上述源码的执行逻辑,关键点在mAttachInfo是否为null,这会导致两种逻辑:
1)mAttachInfo不为null,走代码①的逻辑。
2)mAttachInfo为null,走代码②的逻辑。
当前View尚未attach到Window时,整个View体系还没有加载完,mAttachInfo就会为null,表现在Activity中,就是onResume()方法还没有运行完。反之,mAttachInfo就不会为null。这部分内容后面会详细讲解,这里先知道这个结论。