Ali
03/30/2022, 9:30 AMViewStub and OnInflateListener to listen once the original layout is inflated.
This is my ViewStub layout `fragment_navigation_viewstub`:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="<http://schemas.android.com/apk/res/android>"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ViewStub
android:id="@+id/fragmentNavigationViewStub"
android:layout="@layout/fragment_navigation"
android:layout_width="match_parent"
android:inflatedId="@+id/items_list"
android:layout_height="match_parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
and this is my original layout `fragment_navigation`:
<?xml version="1.0" encoding="utf-8"?>
<androidx.recyclerview.widget.RecyclerView
xmlns:android="<http://schemas.android.com/apk/res/android>"
android:id="@+id/items_list"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
and my listeneer inside the fragment:
private val binding by viewBinding { FragmentNavigationViewstubBinding.inflate(it) }
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding.fragmentNavigationViewStub.setOnInflateListener { _, view ->
fragmentNavigation = FragmentNavigationBinding.bind(view)
}
}
But I got this exception and looks like setOnInflateListener is not called
kotlin.UninitializedPropertyAccessException: lateinit property fragmentNavigation has not been initializedRonald Wolvers
03/30/2022, 8:38 PMinflate() on the ViewStub to have an OnInflateListener be invoked. From what I understand that's how ViewStub works.Ronald Wolvers
03/30/2022, 8:40 PMRonald Wolvers
03/30/2022, 8:42 PMViewStub stub = findViewById(R.id.stub);
View inflated = stub.inflate();Ali
03/30/2022, 9:39 PMRonald Wolvers
03/30/2022, 9:59 PMinflate() on the stub. View binding does not inflate the stub specifically and so the listener is not called.Ali
03/31/2022, 9:16 AMinflate as you can see in the class
private val binding by viewBinding { FragmentNavigationViewstubBinding.inflate(it) }Ronald Wolvers
03/31/2022, 9:31 AMConstraintLayout with that call and not the ViewStubRonald Wolvers
03/31/2022, 9:32 AMfragment_navigation_viewstub and so the call to FragmentNavigationViewstubBinding.inflate(it) is inflating the layout (ConstraintLayout) and not the ViewStub unless you specifically call inflate() on the ViewStub.Ali
04/01/2022, 8:00 AMfragment_navigation_viewstub layout not sure how can i inflate again the ViewStub 🤔Ronald Wolvers
04/01/2022, 8:07 AMbinding.fragmentNavigationViewStub.inflate()
If you call that after setting the listener, I'm quite sure your OnInflateListener will trigger. The reason is that the ViewStub needs to be inflated separately. Let me know if that works!Ali
04/01/2022, 8:46 AMinflate call inside this class?Ronald Wolvers
04/01/2022, 8:56 AMViewStub is for! Lazy inflation.Ronald Wolvers
04/01/2022, 8:57 AMAli
04/01/2022, 8:58 AMOnInflateListener as per the developer docRonald Wolvers
04/01/2022, 9:57 AMAli
04/01/2022, 11:17 AMRonald Wolvers
04/01/2022, 12:21 PMAli
04/01/2022, 12:25 PM