【问题标题】:Is it possible to use view binding in a static method?是否可以在静态方法中使用视图绑定?
【发布时间】:2021-12-27 09:01:20
【问题描述】:

我的问题是,是否可以在静态方法中使用视图绑定:

class SomeFragment : FragmentBinding<SomeFragmentBinding>() {

 companion object {
   fun someRandomFunction() {
    // Use view binding here
    binding.textView.text = "Test"
   }
 }
}

【问题讨论】:

    标签: android static android-viewbinding


    【解决方案1】:

    bindingSomeFragment 类的非静态属性,其中 companion object 是静态的。静态方法不能访问非静态属性,因为类的每个实例都有自己的非静态属性实例。

    因此,您无法访问bindingcompanion object 中的任何其他非静态属性。

    【讨论】:

      猜你喜欢
      • 2012-04-26
      • 2010-12-21
      • 1970-01-01
      • 2012-07-05
      • 1970-01-01
      • 1970-01-01
      • 2016-04-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多