Q. 어떻게 사용해?
A.
presentation-main-viewpager2
class MainViewPagerAdapter(fragmentActivity: FragmentActivity) : FragmentStateAdapter(fragmentActivity) {
// private lateinit var mainViewPagerAdapter: MainViewPagerAdapter
private val fragments = listOf(HomeFragment(), MyFragment())
override fun getItemCount(): Int {
return fragments.size
}
override fun createFragment(position: Int): Fragment {
return fragments[position]
}
}
presentation-main
//import
import com.google.android.material.tabs.TabLayoutMediator
//onCreate
initLayout()
//뷰페이저초기화
private fun initLayout() {
val viewPager = binding.mainViewPager
val mainViewPagerAdapter = MainViewPagerAdapter(this)
viewPager.adapter = mainViewPagerAdapter
//탭레이아웃 연결
TabLayoutMediator(binding.mainTab, binding.mainViewPager) { tab, position ->
when (position) {
0 -> tab.text = "검색"
1 -> {
tab.text = "보관함"
}
}
}.attach()
}
'나만의 AI > 안드로이드 도와줘' 카테고리의 다른 글
Android Kotlin [Pickmedia 알려줘] (1) | 2024.08.31 |
---|---|
Android Kotlin [Dialog 알려줘] (0) | 2024.08.12 |
Android kotlin [ViewModel 알려줘] (0) | 2024.08.01 |
Android kotlin [Glide 알려줘] (0) | 2024.08.01 |
Android kotlin [parcelize 알려줘] (0) | 2024.07.16 |