Hyeyeon blog

[Android] TabLayout과 ViewPager 연결하기 본문

개발/Android

[Android] TabLayout과 ViewPager 연결하기

Hyeyeon.P 2019. 10. 30. 00:42
반응형
1. FragmentStatePagerAdapter에 getPageTitle() 추가
class FragmentPagerAdapater (fm: FragmentManager) : FragmentStatePagerAdapter(fm) {
    var mTabTitles = mutableListOf()
    ...
    override fun getPageTitle(pos: Int): CharSequence? {
        return if(mTabTitles.isEmpty()) super.getPageTitle(pos) else mTabTitles[pos]
    }
}
2. adapter의 mTabTitles 셋팅
val mAdapter = FragmentPagerAdapater((context as FragmentActivity).supportFragmentManager).apply {
	this.mTabTitles = mutableListOf("tab1", "tab2", "tab3"))
}
mAdapter.addFragment(Fragment())
...
mBinding,viewPager.adapter = mAdapter
3. viewPager과 tabLayout연결
mBinding.tabLayout.setupWithViewPager(mBinding.viewPager)
728x90
Comments