Home>

【Purpose】
I want to write a program that uses ViewPager2 to switch images. In the reference book I'm using, I selected ViewPager, but in my Android studio, there is only ViewPager2, and if I select that, even if I write the code according to the reference book, it does not compile.

[Code]

package com.example.myslideshow
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentPagerAdapter
import kotlinx.android.synthetic.main.activity_main. *
class MainActivity: AppCompatActivity () {
    class MyAdapter (fm: androidx.fragment.app.FragmentManager): FragmentPagerAdapter (fm) {
        private val resources = listOf (
            R.drawable.slide00, R.drawable.slide01,
            R.drawable.slide02, R.drawable.slide03,
            R.drawable.slide04, R.drawable.slide05,
            R.drawable.slide06, R.drawable.slide07,
            R.drawable.slide08, R.drawable.slide09
        )
        // Number of views to display
        override fun getCount (): Int {
            return resources.size
        }
        // Receives the page number as an argument, creates an instance of ImageFragment and returns it
        override fun getItem (position: Int): Fragment {
            return ImageFragment.newInstance (resources [position])
        }
    }
    override fun onCreate (savedInstanceState: Bundle?) {
        super.onCreate (savedInstanceState)
        setContentView (R.layout.activity_main)
        pager.adapter = MyAdapter (supportFragmentManager)
    }
}

【error】
This is where the compile error comes from

pager.adapter = MyAdapter (supportFragmentManager)

error contents:

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

I don't use RecyclerView ...
But this articleViewPager vs ViewPager2Is it because i am using ViewPager2 that you find RecyclerView and say "Yeah"? I'm guessing. But I don't know the solution

  • Answer # 1

    What is requiredRecyclerView.AdapterSo I think it doesn't matter if you use it or not.

    ViewPager2ThenFragmentPagerAdapternotFragmentStateAdapterI came to use.
    (Remarks:FragmentStateAdapterIsRecyclerView.AdapterIs a subclass of

    SoFragmentStateAdapterInheritedMyAdapterShould I reimplement.

      class MyAdapter (fa: FragmentActivity): FragmentStateAdapter (fa) {
            private val resources = listOf (
                    R.drawable.slide00, R.drawable.slide01,
                    R.drawable.slide02, R.drawable.slide03,
                    R.drawable.slide04, R.drawable.slide05,
                    R.drawable.slide06, R.drawable.slide07,
                    R.drawable.slide08, R.drawable.slide09
            )
            // Number of views to display
            override fun getItemCount (): Int {
                return resources.size
            }
            // Receives the page number as an argument, creates an instance of ImageFragment and returns it
            override fun createFragment (position: Int): Fragment {
                return ImageFragment.newInstance (resources [position])
            }
        }

    The arguments passed to MyAdapter also change. Pass the Activity itself.

    pager.adapter = MyAdapter (this)

    Please read the official ViewPager to ViewPager2 migration guide as well.

    Migrate from ViewPager to ViewPager2