Skip to main content

How to dynamically update ViewPager2 fragments in Kotlin Android

How to dynamically update ViewPager2 fragments in Kotlin Android.

Here is a step-by-step tutorial on how to dynamically update ViewPager2 fragments in Kotlin Android:

Step 1: Set up the project

First, create a new Android project in your preferred IDE. Make sure you have the necessary dependencies for ViewPager2 added to your project. You can add the following dependencies to your app-level build.gradle file:

implementation 'androidx.viewpager2:viewpager2:1.0.0'

Step 2: Create the ViewPager2 layout

In your activity layout file (activity_main.xml), add a ViewPager2 element. This will be the container for your fragments. Here's an example:

android:layout_height="match_parent" />

Step 3: Create the Fragment classes

Create the fragment classes that you want to display in the ViewPager2. Each fragment should extend the Fragment class. Here's an example fragment class:

class MyFragment : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_my, container, false)

Make sure to create a corresponding layout file for each fragment.

Step 4: Create the FragmentAdapter

Next, create a FragmentAdapter class that extends the FragmentStateAdapter class. This adapter will be responsible for managing the fragments in the ViewPager2. Here's an example adapter class:

class MyFragmentAdapter(fragmentManager: FragmentManager, lifecycle: Lifecycle) : FragmentStateAdapter(fragmentManager, lifecycle) {
private val fragments: MutableList<Fragment> = ArrayList()

fun setFragments(newFragments: List<Fragment>) {

override fun getItemCount(): Int {
return fragments.size

override fun createFragment(position: Int): Fragment {
return fragments[position]

In this example, the setFragments method is used to update the list of fragments. The getItemCount method returns the number of fragments, and the createFragment method creates a new instance of the requested fragment.

Step 5: Initialize the ViewPager2 and Adapter

In your activity class, initialize the ViewPager2 and the adapter. Set the adapter to the ViewPager2 instance. Here's an example:

class MainActivity : AppCompatActivity() {
private lateinit var viewPager: ViewPager2
private lateinit var fragmentAdapter: MyFragmentAdapter

override fun onCreate(savedInstanceState: Bundle?) {

viewPager = findViewById(
fragmentAdapter = MyFragmentAdapter(supportFragmentManager, lifecycle)
viewPager.adapter = fragmentAdapter

// Add initial fragments
val initialFragments = listOf(

In this example, we set the adapter to the ViewPager2 instance and add some initial fragments using the setFragments method.

Step 6: Dynamically update the fragments

To dynamically update the fragments in the ViewPager2, simply call the setFragments method on the adapter with the updated list of fragments. Here's an example:

val updatedFragments = listOf(

In this example, we update the fragments in the ViewPager2 with a new list of fragments.

That's it! You now have a dynamically updating ViewPager2 with fragments in Kotlin Android.