14.findViewById的过去及未来
We have lots of alternatives for this, and you may wonder why do we need another solution. Let’s compare the different solutions based on these criteria: null-safety, compile-time safety, and speed.
Column 1 | ButterKnife | Kotlin Synthetics | Data Binding | findViewById) | View Binding |
---|---|---|---|---|---|
Fast | ❌ * | ✅ | ❌ * | ✅ | ✅ |
Null-safe | ❌ | ❌ | ✅ | ❌ | ✅ |
Compile-time safe | ❌ | ❌ | ✅ | ✅ ** | ✅ |
* ButterKnife and Data Binding solutions are slower because they use an annotation-based approach
** findViewById()
is compile-time safe since API 26 because we don’t need to cast the type of view anymore.
https://juejin.cn/post/6905942568467759111