-
Notifications
You must be signed in to change notification settings - Fork 92
Closed
Description
版本号:2.0.5
Fragment绑定失败?原始代码为:
<!-- activity_main.xml -->
<androidx.fragment.app.FragmentContainerView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="**.main.MainFragment"
android:fitsSystemWindows="true"/>
<!-- MainFragment -->
class MainFragment : Fragment() {
private val binding:FragmentMainBinding by binding()
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.content.text = "123456"
}
}
但是在页面中,MainFragment的布局无法加载出来。
查看源码,发现并没有将View绑定至Fragment的相关代码:
class FragmentBindingProperty<VB : ViewBinding>(private val clazz: Class<VB>) : ReadOnlyProperty<Fragment, VB> {
override fun getValue(thisRef: Fragment, property: KProperty<*>): VB =
requireNotNull(thisRef.view) { "The property of ${property.name} has been destroyed." }
.getBinding(clazz).also { binding ->
if (binding is ViewDataBinding) binding.lifecycleOwner = thisRef.viewLifecycleOwner
}
}
是否和这个有关系?
Metadata
Metadata
Assignees
Labels
No labels