findViewById源码解读

源码版本:Android 10

将布局文件id通过findViewById方法传递给父类AppCompatActivity,此处源码如下:
 @Override
    public <T extends View> T findViewById(@IdRes int id) {
        return getDelegate().findViewById(id);
    }

关于getDelegate方法在此处就不在介绍,可以去看setContentView源码解读。最终调用的还是AppCompatDelegateImpl中的findViewById方法,该方法源码如下:

@Nullable
    @Override
    public <T extends View> T findViewById(@IdRes int id) {
        ensureSubDecor();
        //mWindow是PhoneWindow的实例对象
        return (T) mWindow.findViewById(id);
    }

mWindow.findViewById(id)最终调用的是Window的findViewById方法,该方法源代码如下:

  @Nullable
    public <T extends View> T findViewById(@IdRes int id) {
    	//getDecorView()将会返回mDecor,它是DecorView(继承至FrameLayout)的实例
        return getDecorView().findViewById(id);
    }

上述findViewById最终调用的是View的findViewById,该方法的源代码如下所示:

   public final <T extends View> T findViewById(@IdRes int id) {
        if (id == NO_ID) {
            return null;
        }
        return findViewTraversal(id);
    }

View的findViewTraversal方法的源码如下所示:

 protected <T extends View> T findViewTraversal(@IdRes int id) {
        if (id == mID) {  //mID在View被创建传来时就已经赋值了 
            return (T) this;
        }
        return null;
    }

DecorView继承关系如下所示:

因为ViewGroup重写了findViewTraversal方法,该方法源码如下所示:
 @Override
    protected <T extends View> T findViewTraversal(@IdRes int id) {
        if (id == mID) {
            return (T) this;
        }
        final View[] where = mChildren;
        final int len = mChildrenCount;
        for (int i = 0; i < len; i++) {
            View v = where[i];
            if ((v.mPrivateFlags & PFLAG_IS_ROOT_NAMESPACE) == 0) {
            //递归调用通过id寻找控件
                v = v.findViewById(id);
                if (v != null) {
                    return (T) v;
                }
            }
        }
        return null;
    }

findViewById的时序图

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值