Skip to content

Instantly share code, notes, and snippets.

@ntngel1
Last active April 7, 2020 08:57
Show Gist options
  • Save ntngel1/0b852f557979e225d19eb626899eebad to your computer and use it in GitHub Desktop.
Save ntngel1/0b852f557979e225d19eb626899eebad to your computer and use it in GitHub Desktop.
class LaunchDetailsFragmentAdapter(
private val flightNumber: Int,
fm: FragmentManager,
lifecycle: Lifecycle
) : FragmentStateAdapter(fm, lifecycle) {
override fun createFragment(position: Int): Fragment = when (position) {
RESOURCES_POSITION -> LaunchDetailsResourcesFragment.newInstance(flightNumber)
IMAGES_POSITION -> LaunchDetailsImagesFragment.newInstance(flightNumber)
else -> throw IllegalStateException("No such fragment for position = $position")
}
override fun getItemCount() = 2
companion object {
const val RESOURCES_POSITION = 0
const val IMAGES_POSITION = 1
fun getTabLayoutMediator(tabLayout: TabLayout, pager: ViewPager2) =
TabLayoutMediator(tabLayout, pager) { tab, position ->
val titleIdRes = when (position) {
RESOURCES_POSITION -> R.string.resources
IMAGES_POSITION -> R.string.images
else -> throw IllegalStateException("No such title for position = $position")
}
pager.setCurrentItem(tab.position, true)
tab.text = tabLayout.context.str(titleIdRes)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment