ViewPager2
class MainActivity : AppCompatActivity() {
private val binding by lazy { ActivityMainBinding.inflate(layoutInflater) }
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(binding.root)
val adapter = PagerAdapter(this)
binding.pager.adapter = adapter
// MDC の TabLayout と組み合わせるときは TabLayoutMediator を使う
// TabLayoutMediator の attach は ViewPager2 に adapter をセットした後に行う
TabLayoutMediator(binding.tabLayout, binding.pager) { tab, position ->
tab.text = adapter.getTitle(position)
}.attach()
}
}
private class PagerAdapter(
fragmentActivity: FragmentActivity
) : FragmentStateAdapter(fragmentActivity) {
override fun getItemCount(): Int {
return ...
}
override fun createFragment(position: Int): Fragment {
return PageFragment.newInstance(...)
}
fun getTitle(position: Int): String {
return ...
}
}
0 件のコメント:
コメントを投稿