8000 Kotlin · Issue #31 · yunshuipiao/Potato · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Kotlin #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
yunshuipiao opened this issue May 22, 2019 · 0 comments
Open

Kotlin #31

yunshuipiao opened this issue May 22, 2019 · 0 comments
Labels

Comments

@yunshuipiao
Copy link
Owner
yunshuipiao commented May 22, 2019

Kotlin

知识点

为什么不需要 findViewbyid

Kotlin 会自动生成类似 findViewById() 的方法:findCachedViewById(),在这个方法里面创建一个 HashMap 缓存每次查找到的 View,避免每次调用 View 的属性或方法时都会重新调用findCachedViewById()进行查找。具体查找流程:在findCachedViewById()中,会先通过缓存 HashMap 的 get 方法来获取控件, get() 中传入的 key 即控件 ID,由于第一次 get 的值为 null ,因此会调用findViewById() ,并把控件 ID作为 key 和 找到的控件 View 作为 value put 进缓存 Map 中。第二次再使用该控件 ID 的时候,直接可以从 Map 中获取。

在 fragment 中,只有在 onViewCreated 方法之后,才能找到控件,如此使用。

扩展函数的原理

自动生成一个当前对象作为参数的静态函数,当在 Kotlin 中调用扩展函数时,编译器将会调用自动生成的静态函数并且传入当前的对象.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant
0