title: Implementing Viewpager2 in a fragment with Recycler view
body: <p>I'm trying to implement viewpager2 in a fragment which shows a list of notes with Recycler View. I tried different solutions but I'm unable to figure it out.</p>
  <p>Here is my fragment_notes_list.xml :</p>
  <pre><code>&lt;androidx.recyclerview.widget.RecyclerView
  xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  android:id=&quot;@+id/notes_recycler_view&quot;
  android:layout_width=&quot;match_parent&quot;
  android:layout_height=&quot;match_parent&quot;/&gt;
  </code></pre>
  <p>list_item_notes.xml used to display recycler view viewholder:</p>
  <pre><code>&lt;androidx.cardview.widget.CardView
      xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      xmlns:card_view=&quot;http://schemas.android.com/apk/res-auto&quot;
      android:id=&quot;@+id/card_pertanyaan&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;96dp&quot;
      android:layout_marginLeft=&quot;16dp&quot;
      android:layout_marginTop=&quot;12dp&quot;
      android:layout_marginRight=&quot;16dp&quot;
      android:clickable=&quot;true&quot;
      android:foreground=&quot;?selectableItemBackground&quot;
      card_view:cardBackgroundColor=&quot;#595959&quot;
      card_view:cardCornerRadius=&quot;8dp&quot;
      card_view:contentPadding=&quot;10dp&quot;&gt;
  
      &lt;androidx.constraintlayout.widget.ConstraintLayout 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:id=&quot;@+id/linearLayout&quot;
          android:layout_width=&quot;match_parent&quot;
          android:layout_height=&quot;wrap_content&quot;&gt;
  
          &lt;TextView
              android:id=&quot;@+id/notes_title&quot;
              android:layout_width=&quot;0dp&quot;
              android:layout_height=&quot;wrap_content&quot;
              android:layout_marginStart=&quot;8dp&quot;
              android:layout_marginTop=&quot;16dp&quot;
              android:layout_marginEnd=&quot;8dp&quot;
              android:text=&quot;Title&quot;
              android:textColor=&quot;@color/white&quot;
              app:layout_constraintBottom_toTopOf=&quot;@+id/notes_body&quot;
              app:layout_constraintEnd_toEndOf=&quot;parent&quot;
              app:layout_constraintHorizontal_bias=&quot;0.0&quot;
              app:layout_constraintStart_toStartOf=&quot;parent&quot;
              app:layout_constraintTop_toTopOf=&quot;parent&quot;
              app:layout_constraintVertical_chainStyle=&quot;packed&quot; /&gt;
  
          &lt;TextView
              android:id=&quot;@+id/notes_body&quot;
              android:layout_width=&quot;0dp&quot;
              android:layout_height=&quot;wrap_content&quot;
              android:layout_marginStart=&quot;8dp&quot;
              android:layout_marginTop=&quot;8dp&quot;
              android:layout_marginEnd=&quot;8dp&quot;
              android:textColor=&quot;@color/white&quot;
              app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
              app:layout_constraintEnd_toEndOf=&quot;parent&quot;
              app:layout_constraintStart_toStartOf=&quot;parent&quot;
              app:layout_constraintTop_toBottomOf=&quot;@+id/notes_title&quot; /&gt;
  
      &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
  &lt;/androidx.cardview.widget.CardView&gt;
  </code></pre>
  <p>Relevant snippets from fragment class NotesListFragment.kt :</p>
  <pre><code>override fun onCreateView(
          inflater: LayoutInflater,
          container: ViewGroup?,
          savedInstanceState: Bundle?
      ): View? {
          val view = inflater.inflate(R.layout.fragment_notes_list, container, false)
  
          notesRecyclerView =
              view.findViewById(R.id.notes_recycler_view) as RecyclerView
          registerForContextMenu(notesRecyclerView)
          notesRecyclerView.layoutManager = LinearLayoutManager(context)
          notesRecyclerView.adapter = adapter
          return view
      }
  
  private inner class ScreenSlidePagerAdapter(fa: Fragment) : FragmentStateAdapter(fa) {
  
          override fun getItemCount(): Int = NUM_PAGES
  
          override fun createFragment(position: Int): Fragment {
              return if (position == 0) {
                  NotesListFragment()
         
  
   } else {
              CalenderFragment()
          }
      }
  }
  
  
  
  override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
          super.onViewCreated(view, savedInstanceState)
          notesListViewModel.notesListLiveData.observe(
              viewLifecycleOwner,
              Observer { notes -&gt;
                  notes?.let {
                      Log.i(TAG, &quot;got notes&quot;)
                      updateUI(notes)
                  }
              })
      }
  
  private inner class NotesAdapter(var notes: List&lt;Notes&gt;) : RecyclerView.Adapter&lt;NotesHolder&gt;() {
  
          override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): NotesHolder {
              val view = layoutInflater.inflate(R.layout.list_item_notes, parent, false)
  
             /* viewPager = view.findViewById(R.id.pager)
  
              val pagerAdapter = ScreenSlidePagerAdapter(this@NotesListFragment)
              viewPager.adapter = pagerAdapter */
  
              return NotesHolder(view)
          }
  
          override fun getItemCount() = notes.size
  
          override fun onBindViewHolder(holder: NotesHolder, position: Int) {
              val note = notes[position]
              holder.bind(note)
       
  
  
  
   }
      }
  </code></pre>
  <p>I'm confused should I add ViewPager2 with FrameLayout in list_item_notes.xml?
  But then how can I implement ScreenSliderPagerAdapter for it?</p>