不幸的是,我匆忙提出这个问题。我自己找到了答案,但也许有人会派上用场。答案在 Kotlin 字节码中。
这是_$_findCachedViewById
使用 Kotlin Android Extensions 插件为活动生成的:
public View _$_findCachedViewById(int var1) {
if (this._$_findViewCache == null) {
this._$_findViewCache = new HashMap();
}
View var2 = (View)this._$_findViewCache.get(var1);
if (var2 == null) {
var2 = this.findViewById(var1); // this is an Activity reference
this._$_findViewCache.put(var1, var2);
}
return var2;
}
这是为该实现接口_$_findCachedViewById
生成的:ViewHolder
LayoutContainer
public View _$_findCachedViewById(int var1) {
if (this._$_findViewCache == null) {
this._$_findViewCache = new HashMap();
}
View var2 = (View)this._$_findViewCache.get(var1);
if (var2 == null) {
View var10000 = this.getContainerView();
if (var10000 == null) {
return null;
}
var2 = var10000.findViewById(var1); // var2 is a сontainerView reference
this._$_findViewCache.put(var1, var2);
}
return var2;
}
如果扩展插件创建缓存Activity
或者Fragment
它使用它们的引用来通过 id 查找视图。使用ViewHolder
该实现LayoutContainer
接口,它使用containerView
参考来查找视图。但是,ViewHolder
如果没有LayoutContainer
Kotlin 扩展插件,现在不会使用哪个参考来查找视图。