由于kotlin-android-extensions在kotlin 4.20中被弃用,我想将代码移到支持的方法中。我尝试过https://medium.com/back-market-engineering/from-kotlin-synthetics-to-android-viewbinding-the-definitive-guide-c98c6e89fe0b,但数据绑定并没有得到很好的描述。
发布于 2021-10-06 14:10:24
我不确定是否真的需要数据绑定。无论如何,如果代码以前可以工作,并且我们想让它在没有kotlin-android-extension的情况下工作,我们必须这样做:在gradle文件中替换使用import kotlinx.android.synthetic.的类中的apply plugin 'kotlin-android-extensions' to plugins{ id 'kotlin-android' id 'kotlin-kapt'}代码,以
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
}https://stackoverflow.com/questions/69467360
复制相似问题