[英]Can I infer type of a property in the delegate?
I have this code. 我有这个代码。 It will
findViewById
with a delegate. 它将找到带有委托的
findViewById
。
val backgroundImage: ImageView by lazy { view<ImageView>(R.id.item_component_section_background) }
fun <T: View> view(id : Int) : T {
val view : View = findViewById(id) ?: throw IllegalArgumentException("Given ID could not be found in current layout!")
@Suppress("UNCHECKED_CAST")
return view as T
}
Is there any way to remove the view<ImageView>
for something like view
in the lazy-block? 有什么办法可以删除lazy-block中的
view
之类的view<ImageView>
吗? Can I get or infer the type of the property in the function view()
? 我可以在函数
view()
获取或推断属性的类型吗?
You can have 你可以有
val backgroundImage by lazy { view<ImageView>(R.id.imageView) }
or 要么
val backgroundImage by lazy<ImageView> { view(R.id.imageView) }
Note that in both cases the type of backgroundImage
is inferred from the right hand side expression type. 请注意,在这两种情况下,
backgroundImage
的类型都是从右侧表达式类型推断出来的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.