Hey guys, I was trying to use on-demand feature of...
# android
a
Hey guys, I was trying to use on-demand feature of
ViewStub
and
OnInflateListener
to listen once the original layout is inflated. This is my
ViewStub
layout `fragment_navigation_viewstub`:
Copy code
<?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`:
Copy code
<?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:
Copy code
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 initialized
🧵 3
r
Sorry, deleted my comments yet again. I think the explanation here is that you need to explicitly call
inflate()
on the
ViewStub
to have an
OnInflateListener
be invoked. From what I understand that's how
ViewStub
works.
Copy code
ViewStub stub = findViewById(R.id.stub);
View inflated = stub.inflate();
a
Thanks for your response. But I am using viewbinding
r
Even with view binding you will if I understood correctly still have to call
inflate()
on the stub. View binding does not inflate the stub specifically and so the listener is not called.
a
But I already called the
inflate
as you can see in the class
Copy code
private val binding by viewBinding { FragmentNavigationViewstubBinding.inflate(it) }
r
Yes, but you are inflating the
ConstraintLayout
with that call and not the
ViewStub
Your layout is called
fragment_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
.
a
I already inflated
fragment_navigation_viewstub
layout not sure how can i inflate again the
ViewStub
🤔
r
Hey, good morning, @Ali! Ah, well assuming that what I'm saying is correct, this is what you would do:
Copy code
binding.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!
a
It means we need two
inflate
call inside this class?
r
That's right! That's what
ViewStub
is for! Lazy inflation.
That is, after reading the documentation several times because I didn't understand it either! 😆
a
I also found another way. We can also make setVisibility true to be called the
OnInflateListener
as per the developer doc
r
Yeah, that should work too! Let me know if this fixes the issue!
a
yeah I fixed my issue that way for now
r
That's awesome! Happy to hear it and good luck with your project 😄!
a
thanks man! Cheers