IOC架构设计之ButterKnife源码&原理(二)下篇,kotlin开发windows程序

本文深入探讨了ButterKnife的IOC架构,详细讲解了`bind`和`unbind`方法的实现。`bind`方法主要通过`findViewById`进行视图绑定,并转换为指定类型。`unbind`方法则负责解除绑定,将成员变量和监听置为空。ButterKnife的API提供了一系列`bind`方法用于不同场景的视图绑定,它们最终都会调用`createBinding`方法生成绑定实例。
摘要由CSDN通过智能技术生成

if (requiresCast) {
builder.add(“AsType”);
}
builder.add("(source, $L", bindings.getId().code);
if (fieldBinding.isRequired() || requiresCast) {
builder.add(", $S", asHumanDescription(singletonList(fieldBinding)));
}
if (requiresCast) {
builder.add(", KaTeX parse error: Expected 'EOF', got '}' at position 39: …getRawType()); }̲ builder.add(")…L", builder.build());
return;
}

List requiredViewBindings = bindings.getRequiredBindings();
if (requiredViewBindings.isEmpty()) {
result.addStatement(“view = source.findViewById($L)”, bindings.getId().code);
} else if (!bindings.isBoundToRoot()) {
result.addStatement(“view = $T.findRequiredView(source, $L, $S)”, UTILS,
bindings.getId().code, asHumanDescription(requiredViewBindings));
}

addFieldBindings(result, bindings);
addMethodBindings(result, bindings);
}

主要是调用系统的findViewById 方法,但是你看到了findRequiredViewAsType,findRequiredView方法和castView方法,findRequiredView,findRequiredViewAsType是作者为乐代码的书写方便对findViewById的一层封装,你可以看一下源码,最后都会调用的findRequiredView方法的findViewById方法。

public static View findRequiredView(View source, @IdRes int id, String who) {
View view = source.findViewById(id);
if (view != null) {
return view;
}
String name = getResourceEntryName(source, id);
throw new IllegalStateException(“Required view '”

  • name
  • "’ with ID "
  • id
  • " for "
  • who
  • " was not found. If this view is optional add ‘@Nullable’ (fields) or ‘@Optional’"
  • " (methods) annotation.");
    }

这个castView是什么方法呢?是Class类的方法,直接转换为指定的类型

public static T castView(View view, @IdRes int id, String who, Class cls) {
try {
return cls.cast(view);
} catch (ClassCastException e) {
String name = getResourceEntryName(view, id);
throw new IllegalStateException(“View '”

  • name
  • "’ with ID "
  • id
  • " for "
  • who
  • " was of the wrong type. See cause for more info.", e);
    }
    }

说白了都是调用系统的方法。
好了到这里成员变量的赋值算是完了。
注意一点target.title target就是我们的activity或者view ;也验证了为什么是使用了类似BindView注解不能是private修饰符的另一个原因了。

接下来是方法的监听 private void addMethodBindings(MethodSpec.Builder result, ViewBindings bindings) {}方法,李 main 也是通过循环添加方法借助我们上文提到的
MethodSpec.methodBuilder构造器

for (ListenerMethod method : getListenerMethods(listener)) {
MethodSpec.Builder callbackMethod = MethodSpec.methodBuilder(method.name())
.addAnnotation(Override.class)
.addModifiers(PUBLIC)
.returns(bestGuess(method.returnType()));
String[] parameterTypes = method.parameters();
for (int i = 0, count = parameterTypes.length; i < count; i++) {
callbackMethod.addParameter(bestGuess(parameterTypes[i]), “p” + i);
//…
}

感兴趣的可以根据生成的代码来对照这查看,这里就不多说了。
最后生成的如下所示的代码。

@UiThread
public SimpleActivity_ViewBinding(f

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值