Home » Android » How to use Kotlin Android Extensions with ViewStub properly?

How to use Kotlin Android Extensions with ViewStub properly?

Posted by: admin May 14, 2020 Leave a comment

Questions:

Does the extensions have a magic to call inflated view? As far as I see, I should break the harmony of code and call findViewById.

The intent was to inflate layout_ongoingView layout at sometime, and make hidden, and visible again based on scenario.

<ViewStub
    android:id="@+id/viewStubOngoing"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inflatedId="@+id/ongoingView"
    android:layout="@layout/layout_ongoingView" />

And codes

override fun hideOngoingPanel() {
    if (viewStubOngoing !is ViewStub) {
        findViewById(R.id.ongoingView).visibility = View.GONE
    }
}

override fun showOngoingPanel() {
    if (viewStubOngoing !is ViewStub) {
        findViewById(R.id.ongoingView).visibility = View.VISIBLE
    } else {
        viewStubOngoing.inflate()
    }
}
How to&Answers: