Sma*_*mer 8 android kotlin android-jetpack android-viewbinding
我一直在阅读https://developer.android.com/topic/libraries/view-binding并正在考虑他们如何在片段中定义视图绑定属性。
这是示例代码:
private var _binding: ResultProfileBinding? = null
// This property is only valid between onCreateView and
// onDestroyView.
private val binding get() = _binding!!
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
_binding = ResultProfileBinding.inflate(inflater, container, false)
val view = binding.root
return view
}
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
Run Code Online (Sandbox Code Playgroud)
可为空也是如此_binding,因为对视图的引用将在 中被销毁onDestroyView()。然而,它们用作binding支持属性,它检索_binding并使其不可为 null(无论 Kotlin 中调用的强制展开的等效项是什么)。
问题是为什么我们应该拥有_binding以及binding应该使用哪一个?感觉就像如果您试图使其可为_binding空,那么为什么要使其本质上不可为空binding并在视图被销毁时冒险访问视图呢?
| 归档时间: |
|
| 查看次数: |
3962 次 |
| 最近记录: |