8000
We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
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
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 中调用扩展函数时,编译器将会调用自动生成的静态函数并且传入当前的对象.
The text was updated successfully, but these errors were encountered:
No branches or pull requests
Uh oh!
There was an error while loading. Please reload this page.
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 中调用扩展函数时,编译器将会调用自动生成的静态函数并且传入当前的对象.
The text was updated successfully, but these errors were encountered: