title: How to use ViewPager2 with tabs in Kotlin?
body: <p>Actually in my app i had an activity which had a include of first fragment in it and then from that fragment i was able to press some buttons to navigate to another fragment, now i would change the activity and add a tablayout from which i will be able to navigate between the three fragments i have, but by reading the documentation i'm can't understand on how i can use the ViewPager2 to show my fragment in it by cliking on the tab.</p>
  <p>My activity layout looks like this now:</p>
  <pre><code>&lt;androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
      xmlns:tools=&quot;http://schemas.android.com/tools&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;match_parent&quot;
      tools:context=&quot;.LetturaActivity&quot;&gt;
  
      &lt;com.google.android.material.appbar.AppBarLayout
          android:layout_width=&quot;match_parent&quot;
          app:elevation=&quot;0dp&quot;
          android:layout_height=&quot;wrap_content&quot;
          android:theme=&quot;@style/Theme.VisualStock.AppBarOverlay&quot;&gt;
  
          &lt;androidx.appcompat.widget.Toolbar
              android:id=&quot;@+id/toolbar&quot;
              android:layout_width=&quot;match_parent&quot;
              android:layout_height=&quot;?attr/actionBarSize&quot;
              android:background=&quot;?attr/colorPrimary&quot;
              app:popupTheme=&quot;@style/Theme.VisualStock.PopupOverlay&quot; /&gt;
  
          &lt;com.google.android.material.tabs.TabLayout
              android:layout_width=&quot;match_parent&quot;
              android:layout_height=&quot;wrap_content&quot;
              app:elevation=&quot;8dp&quot;
              app:tabSelectedTextColor=&quot;#ffffff&quot;
              app:tabBackground=&quot;@drawable/tab_selector&quot;
              app:tabIndicatorGravity=&quot;top&quot;
              app:tabIndicatorColor=&quot;#ffffff&quot;
              app:tabMode=&quot;fixed&quot;&gt;
  
              &lt;com.google.android.material.tabs.TabItem
                  android:layout_width=&quot;wrap_content&quot;
                  android:layout_height=&quot;wrap_content&quot;
                  android:text=&quot;@string/riepilogo&quot;
                  /&gt;
  
              &lt;com.google.android.material.tabs.TabItem
                  android:layout_width=&quot;wrap_content&quot;
                  android:layout_height=&quot;wrap_content&quot;
                  android:text=&quot;@string/testata&quot;
                  /&gt;
  
              &lt;com.google.android.material.tabs.TabItem
                  android:layout_width=&quot;wrap_content&quot;
                  android:layout_height=&quot;wrap_content&quot;
                  android:text=&quot;@string/corpo&quot;
                  /&gt;
  
          &lt;/com.google.android.material.tabs.TabLayout&gt;
  
      &lt;/com.google.android.material.appbar.AppBarLayout&gt;
  
      &lt;androidx.viewpager2.widget.ViewPager2
          android:id=&quot;@+id/pager&quot;
          android:layout_width=&quot;match_parent&quot;
          android:layout_height=&quot;match_parent&quot; /&gt;
  
  
      &lt;com.google.android.material.floatingactionbutton.FloatingActionButton
          android:id=&quot;@+id/floatingActionButton&quot;
          android:layout_width=&quot;wrap_content&quot;
          android:layout_height=&quot;wrap_content&quot;
          android:layout_margin=&quot;16dp&quot;
          android:clickable=&quot;true&quot;
          android:contentDescription=&quot;@string/menu_per_lo_scarico_nuovo_documento_o_riepilogo_articoli&quot;
          android:focusable=&quot;true&quot;
          app:fabSize=&quot;normal&quot;
          app:layout_anchor=&quot;@+id/pager&quot;
          app:layout_anchorGravity=&quot;end|bottom&quot;
          app:srcCompat=&quot;@drawable/ic_add&quot;
          app:tint=&quot;@android:color/white&quot; /&gt;
  
  &lt;/androidx.coordinatorlayout.widget.CoordinatorLayout&gt;
  </code></pre>
  <p>And then i have three fragment files like this:</p>
  <pre><code>class LetturaFragment : Fragment() {
  ...
        override fun onCreateView(
              inflater: LayoutInflater, container: ViewGroup?,
              savedInstanceState: Bundle?
      ): View? {
          // Inflate the layout for this fragment
          return inflater.inflate(R.layout.fragment_lettura, container, false)
      }
      override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
          super.onViewCreated(view, savedInstanceState)
  ...
       }
   }
  
  class ArticoliFragment : Fragment() {
      override fun onCreateView(
              inflater: LayoutInflater, container: ViewGroup?,
              savedInstanceState: Bundle?
      ): View? {
          return inflater.inflate(R.layout.fragment_articoli, container, false)
      }
      override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
          super.onViewCreated(view, savedInstanceState)
  ...
  }
  }
  </code></pre>
  <p>and the activity</p>
  <pre><code>class LetturaActivity : AppCompatActivity() {
      override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)
          setContentView(R.layout.activity_lettura)
  }
  </code></pre>
  <p>How can i use my fragments to navigate in the tab and show them in ViewPager2?</p>