Search code examples
androidkotlinandroid-viewpager

Type mismatch. Required: (RecyclerView.Adapter<RecyclerView.ViewHolder!>?..RecyclerView.Adapter<*>?) Found: PageAdapter


I'm getting an error type mismatch in the line PageAdapter(supportFragmentManager) in the code below:

class PatientDetailsActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_patient_details)    
        viewPager.adapter = PageAdapter(supportFragmentManager) //error   
    }

}

Here's my adapter.kt

 class PageAdapter(fm: FragmentManager) : FragmentPagerAdapter(fm) {
    override fun getCount(): Int {
        return 4
    }

    override fun getItem(position: Int): Fragment {
        when(position){
            0 -> return(Fragment1())
            1 -> return(Fragment2())
            2 -> return(Fragment3())
            3 -> return(Fragment4())
        }
    }
}

layout

    <?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="com.test.sample.PatientDetailsActivity">


  <com.google.android.material.tabs.TabLayout
      android:id="@+id/tabLayout"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_marginStart="1dp"
      android:layout_marginEnd="1dp"
      android:layout_marginBottom="1dp"
      app:layout_constraintBottom_toTopOf="@+id/viewPager"
      app:layout_constraintEnd_toEndOf="parent"
      app:layout_constraintStart_toStartOf="parent"
      app:layout_constraintTop_toTopOf="parent">

    <com.google.android.material.tabs.TabItem
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Monday" />

    <com.google.android.material.tabs.TabItem
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Tuesday" />

    <com.google.android.material.tabs.TabItem
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Wednesday" />
  </com.google.android.material.tabs.TabLayout>

  <androidx.viewpager2.widget.ViewPager2
      android:id="@+id/viewPager"
      android:layout_width="0dp"
      android:layout_height="0dp"
      android:layout_marginStart="1dp"
      android:layout_marginEnd="1dp"
      android:layout_marginBottom="1dp"
      app:layout_constraintBottom_toBottomOf="parent"
      app:layout_constraintEnd_toEndOf="parent"
      app:layout_constraintStart_toStartOf="parent"
      app:layout_constraintTop_toBottomOf="@+id/tabLayout" />
</androidx.constraintlayout.widget.ConstraintLayout>

Solution

  • I believe you are using ViewPager2 in your xml layout for viewpager functionality. Since ViewPager2 doesn't work with PagerAdapter, it does require a RecyclerView Adapter.

    Simple solution for you will be to use ViewPager instead of ViewPager2.

    And if you want to use ViewPager2 you can follow article from this link for the RecyclerViewAdapter implementation.

    View Pager 2