kotlin 1.2.50
我正在做一个聪明的 Actor ,只是想知道这是否是正确的方法。如
params.bottomMargin
由代码编辑器突出显示为 Smart cast to android.support.v7.widget.RecyclerView.LayoutParams
val child = parent.getChildAt(i)
val params = child.layoutParams
if(params is RecyclerView.LayoutParams) {
val dividerTop = child.bottom + params.bottomMargin
val dividerBottom = dividerTop + drawable.intrinsicHeight
drawable.setBounds(dividerLeft, dividerTop, dividerRight, dividerBottom)
drawable.draw(c)
}
只是想知道我是否可以用上面的代码做更多的事情来转换
params to RecyclerView.LayouParams
提前谢谢了,
最佳答案
这是智能类型转换的正确用法。如果你想要一个替代方案,你也可以使用 when
声明或安全转换,例如以下示例:
when (params) {
is RecyclerView.LayoutParams -> { /* smart cast here */ }
else -> { /* optional other cases */ }
}
// casts to RecyclerView.LayoutParams? and then invokes the body of let if not null (when params is a RecyclerView.LayoutParams)
(params as? RecyclerView.LayoutParams)?.let {
/* smart cast here */
}
关于kotlin - 用于安全施法的智能施法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52170112/