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?
clean/rebuild project
? If it did not work just invalidate cache and restart again it should work. – RestorationKotlin
you can import thisimport kotlinx.android.synthetic.main.activity_main.*
so you can avoid thefindViewById()
thingy – Restorationimplementation "com.google.android.material:material:1.1.0-alpha07"
implementation "androidx.viewpager2:viewpager2:1.0.0-alpha05"
– Restoration'com.google.android.material:material:1.0.0'
(notice the version1.0.0
).TablayoutMediator
is added in1.1.0
i guess – Denson