Reputation: 11
Making tabs with TabLayout and ViewPager2 and fragment. I am getting type mismatch on adapter. I am new to KOTLIN and Android if any suitable tutorial link is there which has implementation of tablayout with viewpager2
class ProfileActivity:AppCompatActivity() {
lateinit var toolbar: Toolbar
lateinit var tabLayout: TabLayout
lateinit var viewpager2: ViewPager2
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_profile)
// Toolbar
toolbar=findViewById(R.id.toolbar)
setSupportActionBar(toolbar)
supportActionBar?.apply {
title="Profile"
// show back button on toolbar
// on back button press, it will navigate to parent activity
// mentioned in AndroidManifest.xml
setDisplayHomeAsUpEnabled(true)
setDisplayShowHomeEnabled(true)
}
// Tab layout - add tab items
tabLayout=findViewById(R.id.profileTabLayout)
tabLayout.addTab(tabLayout.newTab().setText("Personal"))
tabLayout.addTab(tabLayout.newTab().setText("Education"))
tabLayout.addTab(tabLayout.newTab().setText("Interests"))
tabLayout.tabGravity=TabLayout.GRAVITY_FILL
//Viewpager2
viewpager2=findViewById(R.id.profileViewPager)
//Adapter for view pager
val adapter1=MyTabAdapter(this,supportFragmentManager,tabLayout.tabCount)
viewpager2.adapter= adapter1
viewpager2.addOnPageChangeListener(TabLayout.TabLayoutOnPageChangeListener(tabLayout))
tabLayout.addOnTabSelectedListener(
object : TabLayout.OnTabSelectedListener {
override fun onTabSelected(tab: TabLayout.Tab) {
viewpager2.currentItem = tab.position
}
override fun onTabUnselected(tab: TabLayout.Tab) {
}
override fun onTabReselected(tab: TabLayout.Tab) {
}
}
)
}
}
Here is Code of MyAdapter Probably i am confused with FragmentStatePagerAdapter as it shows deprecated if there is any easy method to implement then i can try that also.
class MyTabAdapter(profileActivity: ProfileActivity, supportFragmentManager: FragmentManager, private var tabCount: Int) {
fun getItem(position: Int): Fragment? {
when (position) {
0 -> {
// val homeFragment: HomeFragment = HomeFragment()
// val personalFragment:PersonalFragment=PersonalFragment()
return PersonalFragment()
}
1 -> {
return EducationFragment()
}
2 -> {
// val movieFragment = MovieFragment()
return InterestsFragment()
}
else -> return null
}
}
fun getCount(): Int {
return tabCount
}
}
**Here is one of the fragment . I have three fragment in total.** I
class PersonalFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_personal, container, false)
}
}
I have referred the https://www.javatpoint.com/kotlin-android-tablayout-with-viewpager for implementation
I
Upvotes: 1
Views: 1248
Reputation: 1170
There are two problems that I can see:
ViewPager
but the snippet of codes that you attached showed that you were using ViewPager2
.MyTabAdapter
extends nothing, it has to extend some kind of base adapter.In your case, I suppose you wanted to use ViewPager2
to page through some fragments. You need to use FragmentStateAdapter
.
Modify MyTabAdapter
to be liked this
class MyTabAdapter(profileActivity: ProfileActivity, private val tabCount: Int) : FragmentStateAdapter(profileActivity) {
override fun createFragment(position: Int): Fragment? {
when (position) {
0 -> {
// val homeFragment: HomeFragment = HomeFragment()
// val personalFragment:PersonalFragment=PersonalFragment()
return PersonalFragment()
}
1 -> {
return EducationFragment()
}
2 -> {
// val movieFragment = MovieFragment()
return InterestsFragment()
}
else -> return null
}
}
override fun getItemCount(): Int {
return tabCount
}
}
Check here for detailed document of migrating from ViewPager
to ViewPager2
Upvotes: 1