ViewPager Adapter

Swipe views let you navigate between sibling screens, such as tabs, with a horizontal finger gesture, or swipe. This navigation pattern is also referred to as horizontal paging.

OneViewPager

You can use BaseViewPagerAdapter to create a viewpager for your application. In this OneViewPager, you don't need to create a separate class to create a page swipe, you only need to create a lazy variable.

Example:

Create an activity/fragment, then use TabLayout and ViewPager2 inside the activity/fragment.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".presentation.viewpager.ViewPagerActivity">

    <com.google.android.material.tabs.TabLayout
        android:id="@+id/tabLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabMode="fixed"
        app:tabPaddingStart="0dp"
        app:tabTextAppearance="@android:style/TextAppearance.Widget.TabWidget"
        app:tabTextColor="@color/black"/>

    <androidx.viewpager2.widget.ViewPager2
        android:id="@+id/vpScreen"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>

Then, you can create a lazy variable.

private val viewPager by lazy {
    object : BaseViewPagerAdapter<BaseFragment<*>>(this){
        override fun createFragment(position: Int): Fragment =
            getItem(position).fallback(Fragment())
    }
}

Then, define the TabLayout, ViewPager and you can insert title and the Fragments. For example:

private fun setupViewPager() {
    val titles = listOf("First Screen", "Second Screen")
    binding.vpScreen.apply {
        adapter = viewPager
        isSaveEnabled = false
        isUserInputEnabled = false

        TabLayoutMediator(binding.tabLayout, binding.vpScreen) {tab, position ->
            tab.text = titles[position]
        }.attach()
    }

    viewPager.addAllItems(
        listOf(
            FirstViewPagerFragment(),
            SecondViewPagerFragment()
        )
    )
}

Full Code:

class ViewPagerActivity : BaseActivity<ActivityViewPagerBinding>() {

    private val viewPager by lazy {
        object : BaseViewPagerAdapter<BaseFragment<*>>(this){
            override fun createFragment(position: Int): Fragment =
                getItem(position).fallback(Fragment())
        }
    }

    override fun getViewBinding(): ActivityViewPagerBinding =
        ActivityViewPagerBinding.inflate(layoutInflater)

    override fun initUI() {
        setupViewPager()
    }

    private fun setupViewPager() {
        val titles = listOf("First Screen", "Second Screen")
        binding.vpScreen.apply {
            adapter = viewPager
            isSaveEnabled = false
            isUserInputEnabled = false

            TabLayoutMediator(binding.tabLayout, binding.vpScreen) {tab, position ->
                tab.text = titles[position]
            }.attach()
        }

        viewPager.addAllItems(
            listOf(
                FirstViewPagerFragment(),
                SecondViewPagerFragment()
            )
        )
    }

}

Last updated