Skip to content

Instantly share code, notes, and snippets.

@addeeandra
Last active July 28, 2019 17:13
Show Gist options
  • Save addeeandra/07294a6fe62748ab40252a4a99ea2ad8 to your computer and use it in GitHub Desktop.
Save addeeandra/07294a6fe62748ab40252a4a99ea2ad8 to your computer and use it in GitHub Desktop.
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter
import androidx.viewpager.widget.ViewPager
import com.google.android.material.tabs.TabLayout
open class AnyFragmentPagerAdapter(
fm: FragmentManager,
private val mPages: List<AnyPagerableFragment>
) : FragmentPagerAdapter(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
override fun getItem(position: Int): Fragment {
return mPages[position]
}
override fun getCount(): Int {
return mPages.size
}
override fun getPageTitle(position: Int): CharSequence? {
return mPages[position].title
}
fun setup(viewPager: ViewPager, tabLayout: TabLayout) {
viewPager.adapter = this
tabLayout.setupWithViewPager(viewPager, true)
}
abstract class AnyPagerableFragment : Fragment() {
abstract val title: String
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment