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
Related articles
- android studio - how should i write this part of intent in countdowntimer?
- android studio - i would like to know the reason why cannot be converted appears in jsonobject (string) and how to deal with it
- java - no reference site for android studio sqlite room update
- How to develop NDK under Android Studio
- xml - android studio "class never used"
- java - android studio error
- java - if you do not set up android studio one by one, a build error will occur
- Android studio code to draw 10 randomly-sized five-pointed stars at random positions
- android studio - i want to describe the detailed position of the component placed in framelayout in xml
- android studio error
- i don't know how to open the resources window in android studio 36
- macos (osx) - when i try to install android studio on mac, the sdk is installed and i can't install
- Android Studio how to view the source code and debug method steps
- android emulator - build error in android studio
- drawing app android studio
- Android studio cannot add bmob sdk dependency problem and solution
- Detailed explanation of the Android Studio implementation of the user login interface demo (xml implementation)
- how to search for a place name using android studio google map
- android studio - you cannot substitute the one selected by spinner
Related questions
- android studio - i would like to know the reason why cannot be converted appears in jsonobject (string) and how to deal with it
- android studio - null-related error, the application crashes and cannot be resolved
- android studio how to update within the widget's onreceive ()
- android studio - how to change the layout_weight value set in the xml file from the kotlin file
- unable to change the xml layout of an android fragment
- i want to add the characters entered in edittext as listview information by pressing the android button
- i made an android app all that remains is the billing system
- android studio - viewpager and viewadapter cannot be associated
- android studio - in the database sample, i want to prevent the application from crashing even if i click the menu list
- android studio - show message if one of the two edittexts is empty
What is required
RecyclerView.Adapter
So I think it doesn't matter if you use it or not.ViewPager2
ThenFragmentPagerAdapter
notFragmentStateAdapter
I came to use.(Remarks:
FragmentStateAdapter
IsRecyclerView.Adapter
Is a subclass ofSo
FragmentStateAdapter
InheritedMyAdapter
Should I reimplement.The arguments passed to MyAdapter also change. Pass the Activity itself.
Please read the official ViewPager to ViewPager2 migration guide as well.
Migrate from ViewPager to ViewPager2