视图绑定无权访问片段
View binding does not have access to fragment
我有一个 activity,上面有 ConstraintLayout
和片段:
<androidx.constraintlayout.widget.ConstraintLayout
...attributes....
>
<fragment
android:id="@+id/containerView"
app:navGraph="@navigation/some_navigation"
...other attributes...
/>
...other views...
我在 activity 代码中使用视图绑定:
binding = ActivityMainBinding.inflate(layoutInflater)
这里的问题是我无法访问 binding.containerView
。
在 fragment
的情况下我应该使用 findViewById
吗?
片段标记 (<fragment>
) 不是 view
它充当其他视图的 container
,因此您不能像其他视图一样访问它...
当您尝试使用 binding
访问非视图元素时,您将遇到编译错误
相反,您可以使用,
supportFragmentManager.findFragmentById(R.id.containerView)
我有一个 activity,上面有 ConstraintLayout
和片段:
<androidx.constraintlayout.widget.ConstraintLayout
...attributes....
>
<fragment
android:id="@+id/containerView"
app:navGraph="@navigation/some_navigation"
...other attributes...
/>
...other views...
我在 activity 代码中使用视图绑定:
binding = ActivityMainBinding.inflate(layoutInflater)
这里的问题是我无法访问 binding.containerView
。
在 fragment
的情况下我应该使用 findViewById
吗?
片段标记 (<fragment>
) 不是 view
它充当其他视图的 container
,因此您不能像其他视图一样访问它...
当您尝试使用 binding
相反,您可以使用,
supportFragmentManager.findFragmentById(R.id.containerView)