I am developing an android application which needs a TabLayout
bind to a ViewPager2
. Reading this and following the code in here, I need to use TabLayoutMediator
. But when importing it, I face Unresolved reference
error. I know what this error refers to but I cannot find any problem in dependencies and stuff which causes the error. As mentioned in the second link, this class is contained in androidx.viewpager2:viewpager2:1.0.0-beta03
which is a newer version of what the link talks about. I also tried to downgrade the version to the one mentioned in the link but it did not work as well. Here is my build.gradle
file:
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'
}
And This is my MainActivity
file:
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)
}
}
}
Any ideas on what I should do?
I recommend you to read this Styling Android ViewPager tutorial
it explains how to work with what you are looking for, as I can see you are not using the same imports to work with, may you use these instead :
implementation "com.google.android.material:material:1.1.0-alpha07"
implementation "androidx.viewpager2:viewpager2:1.0.0-alpha05"
but how is it possible that the newer versions of these two dependencies cause the problem?!
You have to check the change logs for these library to see what happens, you were using the Beta
versions though.
This is the Github repository
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With