tabSelectionEvents
Create a Flow of tab selection events on the TabLayout instance where the value emitted is one of the 3 event types: TabLayoutSelectionEvent.TabSelected, TabLayoutSelectionEvent.TabReselected, TabLayoutSelectionEvent.TabUnselected
Note: if a a tab is already selected, TabLayoutSelectionEvent.TabSelected will be emitted immediately upon collection.
Note: Created flow keeps a strong reference to the TabLayout instance until the coroutine that launched the flow collector is cancelled.
Example of usage:
// observe all 3 types of events
tabLayout.tabSelectionEvents()
.onEach { event ->
when(event) {
is TabLayoutSelectionEvent.TabSelected -> {
// handle selected event
}
is TabLayoutSelectionEvent.TabReselected -> {
// handle reselected event
}
is TabLayoutSelectionEvent.TabUnselected -> {
// handle unselected event
}
}
}
.launchIn(uiScope)
Content copied to clipboard
// only observe 1 type of events tabLayout.tabSelectionEvents() .filterIsInstance() .onEach { event -> // handle selected event } .launchIn(uiScope)
Content copied to clipboard