ViewPager2 in android is the best-known way to create slide between screen like UX, in our recent article about ViewPager2 we have shared a little knowledge on how you can create ViewPager2 in android with kotlin, Since its an updated version of the old ViewPager in android it uses the famous RecyclerView Adapter to create and show the pages in a carousel to slide between screens. In this tutorial, we are going to give you an example of how to use fragments in viewpager2 with tab layout. If you want to know more about the android fragment you follow the below link
Android Fragment Basics In Kotlin
ViewPager2 With Fragments:
ViewPager2 is a well know UI widget in android for creating carousels, we learned a bit about them in the previous article on How to create a viewpager2 in android with kotlin, whereas we only discussed a single layout screen to show an array of data, But ViewPager2 is not limited to that the use cases and it has a lot of capabilities and our requirements also change to compete with modern applications and to give the user a better experience we need to do more than just using a single layout to show UI, So let's say you have three different screens each with its own design and functionality, and if you wanted to give the user quick navigation between those three screens and a swipe to navigate through, then you definitely need to use ViewPager2, we know that fragments are like "sub-activity" we can use multiple fragments in a single activity each has its own set of functionality.
So to put things in perspective, let's look at an example, So we are on earth and here we have three-time frames namely Morning, Evening, and Night I accept we have more than that but let's not complicate things just keep it simple for now, so consider Earth is an activity which can hold three fragments which are the time frames which we mentioned earlier and each time frame has its own set of characteristics and functions, simple as it is and this is what we are going to achieve through our code, we create the earth and add three-time frames. To get a better understanding of what we are going to do in this example to create viewpager2 with fragments, look at the sample output at the end of this article
Create ViewPager2 In Activity With TabLayout:
We start off by creating our android activity, if you want to recap on android activity peek a quick look at this article, Now that you have your activity we add the two widgets a ViewPager2 and TabLayout
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity">
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</androidx.viewpager2.widget.ViewPager2>
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
This is our constraint layout where we made a simple design with only two mentioned widgets, if you are familiar with ViewPager you might add the tab layout inside the ViewPager widget but notice that in ViewPager2 you cannot add any views inside as that might crash your application by throwing an Exception stating ViewPager2 should not have any child, so we need to add both the widgets separately.
MainActivity.kt
val tabTitles = arrayOf("Morning","Evening","Night")
val viewPager = findViewById<ViewPager2>(R.id.viewPager)
viewPager.adapter = ViewPagerFragmentAdapter(this,tabTitles)
val tabLayout = findViewById<TabLayout>(R.id.tabLayout)
TabLayoutMediator(tabLayout,viewPager){tab, position ->
tab.text = tabTitles[position]
}.attach()
Now move on to your activity, in here we add an array as mentioned before we add three time frames as we are going to have three fragments on the earth(our activity).
How To Add TabLayout To ViewPager2 In Kotlin:
Android TabLayout with ViewPager2 |
Add FragmentStateAdapter To ViewPager2 :
class ViewPagerFragmentAdapter(appCompatActivity: AppCompatActivity, private val tabTitles:Array<String>) :
FragmentStateAdapter(appCompatActivity) {
override fun getItemCount(): Int = tabTitles.size
override fun createFragment(position: Int): Fragment {
return when(position){
0 ->{
MorningFragment().let {
val bundle = Bundle()
bundle.putString("PAGE_TITLE",tabTitles[position])
it.arguments = bundle
return it
}
}
1 -> {
EveningFragment().apply {
val bundle = Bundle()
bundle.putString("PAGE_TITLE",tabTitles[position])
arguments = bundle
}
}
else -> {
NightFragment().apply {
val bundle = Bundle()
bundle.putString("PAGE_TITLE",tabTitles[position])
arguments = bundle
}
}
}
}
}
Android ViewPager2 Fragment With TabLayout |
0 Comments
Have doubts? Shoot your thoughts....