我正在开发一个需要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)
}
}
}
关于我应该做什么的任何想法?