Android/Kotlin: A FragmentStatePagerAdapter (in TabLayout) example

As a note to self, here’s an example of implementing a FragmentStatePagerAdapter Android class in Kotlin:

import java.util.ArrayList

class TabAdapter internal constructor(fm: FragmentManager) : FragmentStatePagerAdapter(fm) {

    private val mFragmentList = ArrayList<Fragment>()
    private val mFragmentTitleList = ArrayList<String>()

    override fun getItem(position: Int): Fragment {
        return mFragmentList[position]

    fun addFragment(fragment: Fragment, title: String) {

     * If you want to only show icons, return null from this method.
     * @param position
     * @return
    override fun getPageTitle(position: Int): CharSequence? {
        return mFragmentTitleList[position]

    override fun getCount(): Int {
        return mFragmentList.size

I use this class in my TabLayout Example project (a TabLayout/Fragment/ViewPager example). This Kotlin code is based on this original Java example.

Add new comment

The content of this field is kept private and will not be shown publicly.

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.