Skip to content

Instantly share code, notes, and snippets.

@weverb2
Created March 30, 2020 16:04
Show Gist options
  • Save weverb2/744607a56e4484dc5b296c8ffc6f539c to your computer and use it in GitHub Desktop.
Save weverb2/744607a56e4484dc5b296c8ffc6f539c to your computer and use it in GitHub Desktop.
Tab Selected Inline Functions
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayout.OnTabSelectedListener
import com.google.android.material.tabs.TabLayout.Tab
inline fun TabLayout.doOnTabReselected(
crossinline action: (tab: Tab?) -> Unit
) = addOnTabSelectedListener(onTabReselected = action)
inline fun TabLayout.doOnTabUnselected(
crossinline action: (tab: Tab?) -> Unit
) = addOnTabSelectedListener(onTabUnselected = action)
inline fun TabLayout.doOnTabSelected(
crossinline action: (tab: Tab?) -> Unit
) = addOnTabSelectedListener(onTabSelected = action)
inline fun TabLayout.addOnTabSelectedListener(
crossinline onTabReselected: (tab: Tab?) -> Unit = {},
crossinline onTabUnselected: (tab: Tab?) -> Unit = {},
crossinline onTabSelected: (tab: Tab?) -> Unit = {}
): OnTabSelectedListener {
val tabSelectedListener = object : OnTabSelectedListener {
override fun onTabReselected(tab: Tab?) {
onTabReselected(tab)
}
override fun onTabUnselected(tab: Tab?) {
onTabUnselected(tab)
}
override fun onTabSelected(tab: Tab?) {
onTabSelected(tab)
}
}
addOnTabSelectedListener(tabSelectedListener)
return tabSelectedListener
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment