1 Star 0 Fork 2

chenjim / kotlin-for-android-developers-zh

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
kuo_zhan_han_shu_zhong_de_cao_zuo_fu.md 721 Bytes
一键复制 编辑 原始数据 按行查看 历史

扩展函数中的操作符

我们不需要去扩展我们自己的类,但是我需要去使用扩展函数扩展我们已经存在的类来让第三方的库能提供更多的操作。几个例子,我们可以去像访问List的方式去访问ViewGroup的view:

operator fun ViewGroup.get(position: Int): View = getChildAt(position)

现在真的可以非常简单地从一个ViewGroup中通过position得到一个view:

val container: ViewGroup = find(R.id.container)
val view = container[2]

别忘了去Kotlin for Android developers repository去查看这些代码。

1
https://gitee.com/chenjim/kotlin-for-android-developers-zh.git
git@gitee.com:chenjim/kotlin-for-android-developers-zh.git
chenjim
kotlin-for-android-developers-zh
kotlin-for-android-developers-zh
master

搜索帮助