Last active
November 14, 2019 11:38
-
-
Save Zhuinden/c643f03a023a9cbe83fff6c75c948d3b to your computer and use it in GitHub Desktop.
implementing FragmentPagerAdapter in a Fragment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyFragmentPagerAdapter( | |
private val context: Context, | |
fragmentManager: FragmentManager | |
) : FragmentPagerAdapter(fragmentManager) { | |
override fun getCount() = 2 | |
override fun getItem(position: Int) = when(position) { | |
0 -> FirstFragment() | |
1 -> SecondFragment() | |
else -> throw IllegalStateException("Unexpected position $position") | |
} | |
override fun getPageTitle(position: Int): CharSequence = when(position) { | |
0 -> context.getString(R.string.first) | |
1 -> context.getString(R.string.second) | |
else -> throw IllegalStateException("Unexpected position $position") | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import kotlin.synthetic... | |
class ParentFragment: Fragment() { | |
override fun onCreateView(...) = ... | |
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | |
super.onViewCreated(view, savedInstanceState) | |
viewPager.adapter = MyFragmentPagerAdapter(requireContext(), childFragmentManager) | |
tabLayout.setupWithViewPager(viewPager) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment