未解决的引用:TabLayoutMediator

9

我正在开发一款Android应用程序,需要将 TabLayout 绑定到 ViewPager2。根据这里的指导并遵循这里的代码,我需要使用 TabLayoutMediator。但是在导入时,我面对着 Unresolved reference 错误。我知道这个错误指的是什么,但我找不到任何导致该错误的依赖关系和其他问题。如第二个链接所述,这个类包含在 androidx.viewpager2:viewpager2:1.0.0-beta03 中,这是一个比链接中提到的更新版本。我还尝试将版本降级到链接中提到的版本,但也没有成功。以下是我的 build.gradle 文件:

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.2"
    defaultConfig {
        applicationId "com.example.myapp"
        minSdkVersion 19
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    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 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    implementation 'androidx.legacy:legacy-support-core-utils:1.0.0'
    implementation 'com.google.android.material:material:1.0.0'
    implementation 'androidx.viewpager2:viewpager2:1.0.0-beta03'
}

这是我的MainActivity文件:

package com.example.myapp

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.viewpager2.widget.ViewPager2
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator


class MainActivity : AppCompatActivity() {

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

        val viewpager : ViewPager2 = findViewById(R.id.view_pager)
        val demoAdapter = DemoViewPagerAdapter(this)
        viewpager.adapter = demoAdapter

        val tabs : TabLayout = findViewById(R.id.tab_layout)
        TabLayoutMediator(tabs, viewpager) { tab, position ->
            tab.text = demoAdapter.getPageTitle(position)
        }
    }
}

你有什么想法?我应该做什么?


1
由于你正在使用 Kotlin,因此可以导入 import kotlinx.android.synthetic.main.activity_main.* 来避免使用 findViewById() - Skizo-ozᴉʞS ツ
@Skizo-ozᴉʞS 是的,我已经尝试过清理和重建项目了。但是仍然没有改变。 - Athena
你能否将你的依赖项更改为以下内容: implementation "com.google.android.material:material:1.1.0-alpha07" implementation "androidx.viewpager2:viewpager2:1.0.0-alpha05" - Skizo-ozᴉʞS ツ
@Skizo-ozᴉʞS 是的,它起作用了!但是这两个依赖项的新版本如何导致问题呢?! - Athena
5
因为你没有使用更新的依赖项,你的 Gradle 文件中使用的是 'com.google.android.material:material:1.0.0'(请注意版本号为 1.0.0)。我猜测 TablayoutMediator 是在 1.1.0 版本中添加的。 - ansh sachdeva
显示剩余2条评论
1个回答

25
我建议你阅读这篇Styling Android ViewPager tutorial,它解释了如何处理你正在寻找的内容。看起来,你没有使用相同的导入方式,也许可以改用以下导入方式:
implementation "com.google.android.material:material:1.1.0-alpha07"
implementation "androidx.viewpager2:viewpager2:1.0.0-alpha05"

但是这两个依赖的更新版本怎么可能会导致问题?!

您需要查看这些库的变更日志以了解发生了什么。尽管您之前一直使用的是Beta版本。

这是Github代码库


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接