类型不匹配。要求:(RecyclerView.Adapter<RecyclerView.ViewHolder!>?..RecyclerView.Adapter<*>?) 找到:PageAdapter
Type mismatch. Required: (RecyclerView.Adapter<RecyclerView.ViewHolder!>?..RecyclerView.Adapter<*>?) Found: PageAdapter
我在下面代码的 PageAdapter(supportFragmentManager)
行收到错误 type mismatch:
class PatientDetailsActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_patient_details)
viewPager.adapter = PageAdapter(supportFragmentManager) //error
}
}
这是我的 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())
}
}
}
布局
<?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>
Type mismatch.
Required: (RecyclerView.Adapter<RecyclerView.ViewHolder!>?..RecyclerView.Adapter<*>?)
Found: PageAdapter
确实,viewPager
作为 activity_patient_details
布局中的 id
被错误地设置为 RecyclerView
的 id,这就是错误所说的。
它期望 ReyclerView.Adapter
而找到 PageAdapter
。
因此,您需要检查布局中ViewPager
的id并将其设置为viewPager
。
我相信您在 xml 布局中使用 ViewPager2 来实现 viewpager 功能。由于 ViewPager2 不能与 PagerAdapter 一起使用,它确实需要 RecyclerView 适配器。
您的简单解决方案是使用 ViewPager 而不是 ViewPager2。
如果您想使用 ViewPager2,您可以按照此 link 中的文章了解 RecyclerViewAdapter 的实现。
我在下面代码的 PageAdapter(supportFragmentManager)
行收到错误 type mismatch:
class PatientDetailsActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_patient_details)
viewPager.adapter = PageAdapter(supportFragmentManager) //error
}
}
这是我的 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())
}
}
}
布局
<?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>
Type mismatch.
Required: (RecyclerView.Adapter<RecyclerView.ViewHolder!>?..RecyclerView.Adapter<*>?)
Found: PageAdapter
确实,viewPager
作为 activity_patient_details
布局中的 id
被错误地设置为 RecyclerView
的 id,这就是错误所说的。
它期望 ReyclerView.Adapter
而找到 PageAdapter
。
因此,您需要检查布局中ViewPager
的id并将其设置为viewPager
。
我相信您在 xml 布局中使用 ViewPager2 来实现 viewpager 功能。由于 ViewPager2 不能与 PagerAdapter 一起使用,它确实需要 RecyclerView 适配器。
您的简单解决方案是使用 ViewPager 而不是 ViewPager2。
如果您想使用 ViewPager2,您可以按照此 link 中的文章了解 RecyclerViewAdapter 的实现。