findViewById()方法分析
首先,看我们在Activity中调用的findViewById方法:
@Nullable
public View findViewById(@IdRes int id) {
return getWindow().findViewById();
}
调用了Window的findViewById()方法,获取结果。
那么下面,我们就来看看Window的findViewById()方法吧,内容如下:
@Nullable
public View findViewById(@IdRes int id) {
return getDecorView().findViewById(id);
}
这个方法中,调用getDecorView()函数,获得最顶层的View(其实一般获取的应该是ViewGroup)。然后,再调用顶层View(ViewGroup)的findViewById()方法。
View(ViewGroup)的findViewById()方法,内容如下:
@Nullable
public final View findViewById(@IdRes int id) {
if (id < 0) {
return null;
}
return findViewTraversal(id);
}
这里判断id是不是小于0。如果是直接方法null,否则将调用findViewTraversal()方法。
在前面说过,定层视图可能是ViewGroup,也可能是View。下面来分别讨论:
顶层视图为View,直接调用下面方法:
protected View findViewTraversal(@IdRes int id) {
if (id == mID) {
return this;
}
return null;
}
顶层视图为View的话,直接判断此View是不是我们要找的视图,是则返回,否则,返回null。
顶层视图为GroupView的话,调用下面方法:
protected View findViewTraversal(@IdRes int id) {
//id符合条件,则返回View
if (id == mID) {
return this;
}
final View[] where = mChildren;
final int len = mChildrenCount;
//循环,查找符合条件的View
for (int i = 0; i < len; i++) {
View v = where[i];
if ((v.mPrivateFlags & PFLAG_IS_ROOT_NAMESPACE) == 0) {
//如果v为View,直接判断。
//如果v为GroupView,递归查找GroupView中的各个ChildrenView
v = v.findViewById(id);
if (v != null) {
return v;
}
}
}
return null;
}
循环递归查找匹配View。找到则返回View内容;否则返回null。
总结
从源代码中可知,在Activity中直接调用findViewById()方法,其实就是调用最顶层View(groupView)对象的findViewById()方法。通过调用顶层视图的findViewById方法,其实就是查找顶层视图及其子视图中,是否存在id符合条件的View。