Kotlin ViewPager2:类'ScreenSlidePagerAdapter'不是抽象的,并且没有实现基类成员

时间:2019-09-29 12:17:01

标签: java android android-studio android-fragments kotlin

嗨, 我正在学习如何制作Android应用,并尝试添加Viewpager ...我一直在关注此教程:https://developer.android.com/training/animation/screen-slide-2

但是我似乎收到了错误消息:Class 'ScreenSlidePagerAdapter' is not abstract and does not implement abstract base class member @NonNull public abstract fun createFragment(p0: Int): Fragment defined in androidx.viewpager2.adapter.FragmentStateAdapter

我的代码与ScreenSlidePagerActivity类的教程几乎完全相同,所以不确定是什么问题!

package com.main.treasurex.ui.home


import android.os.Bundle
import androidx.fragment.app.FragmentActivity
import androidx.fragment.app.Fragment
import androidx.viewpager2.widget.ViewPager2
import com.main.treasurex.R
import androidx.viewpager2.adapter.FragmentStateAdapter



/**
 * The number of pages (wizard steps) to show in this demo.
 */
private const val NUM_PAGES = 5

class SlideFragmentActive : FragmentActivity() {

    /**
     * The pager widget, which handles animation and allows swiping horizontally to access previous
     * and next wizard steps.
     */
    private lateinit var mPager: ViewPager2

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.fragment_sample)

        // Instantiate a ViewPager and a PagerAdapter.
        mPager = findViewById(R.id.pager)

        // The pager adapter, which provides the pages to the view pager widget.
        val pagerAdapter = ScreenSlidePagerAdapter(this)
        mPager.adapter = pagerAdapter
    }

    override fun onBackPressed() {
        if (mPager.currentItem == 0) {
            // If the user is currently looking at the first step, allow the system to handle the
            // Back button. This calls finish() on this activity and pops the back stack.
            super.onBackPressed()
        } else {
            // Otherwise, select the previous step.
            mPager.currentItem = mPager.currentItem - 1
        }
    }

    /**
     * A simple pager adapter that represents 5 ScreenSlidePageFragment objects, in
     * sequence.
     */
    private inner class ScreenSlidePagerAdapter(fa: FragmentActivity) : FragmentStateAdapter(fa) {

        override fun getItemCount(): Int = NUM_PAGES

        override fun getItem(position: Int): Fragment = SlideFragment()
    }
}


依赖项是

dependencies {
//noinspection GradleCompatible
implementation "androidx.viewpager2:viewpager2:1.0.0-beta04"

implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.core:core-ktx:1.0.2'
implementation 'com.google.android.material:material:1.0.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.navigation:navigation-fragment:2.0.0'
implementation 'androidx.navigation:navigation-ui:2.0.0'
implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0'
implementation 'androidx.navigation:navigation-fragment-ktx:2.0.0'
implementation 'androidx.navigation:navigation-ui-ktx:2.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'

}

谢谢您对高级的所有帮助! ED

1 个答案:

答案 0 :(得分:0)

/**
     * A simple pager adapter that represents 5 ScreenSlidePageFragment objects, in
     * sequence.
     */
    private inner class ScreenSlidePagerAdapter(fa: FragmentActivity) : FragmentStateAdapter(fa) {

        override fun createFragment(position: Int): Fragment {
            return ScreenSlidePageFragment()
        }

        override fun getItemCount(): Int = NUM_PAGES

        override fun getItem(position: Int): Fragment = SlideFragment()
    }