【问题标题】:Replacing data binding from kotlin-android-extensions to kotlin-kapt将数据绑定从 kotlin-android-extensions 替换为 kotlin-kapt
【发布时间】:2021-11-26 17:32:24
【问题描述】:

由于 kotlin-android-extensions 在 kotlin 4.20 中已被弃用,我想将代码移至支持的方法。我试过https://medium.com/back-market-engineering/from-kotlin-synthetics-to-android-viewbinding-the-definitive-guide-c98c6e89fe0b 仍然没有很好地描述数据绑定。

【问题讨论】:

标签: android kotlin data-binding android-viewbinding


【解决方案1】:

我不确定是否真的需要数据绑定。无论如何,如果代码以前可以工作,并且我们想让它在没有 kotlin-android-extensions 的情况下工作,我们必须这样做: 在 gradle 文件代码中替换 apply plugin 'kotlin-android-extensions' to plugins{ id 'kotlin-android' id 'kotlin-kapt'} 在使用import kotlinx.android.synthetic. 的类中

private var _binding: YourLayoutNameBinding? = null //YourLayoutName change to your Layout name
private val binding get() = _binding!!
override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?
): View {
    _binding = YourLayoutNameBinding.inflate(inflater, container, false)//YourLayoutName change to your Layout name
    return binding.root
}


override fun onDestroyView() {
    super.onDestroyView()
    _binding = null
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-19
    • 2015-09-05
    • 1970-01-01
    • 1970-01-01
    • 2018-02-07
    • 2017-12-16
    • 1970-01-01
    相关资源
    最近更新 更多