Unresolved reference: TabLayoutMediator [duplicate]
Asked Answered
Q

1

9

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?

Quartile answered 22/8, 2019 at 7:13 Comment(7)
Have you did clean/rebuild project? If it did not work just invalidate cache and restart again it should work.Restoration
Also since you are using Kotlin you can import this import kotlinx.android.synthetic.main.activity_main.* so you can avoid the findViewById() thingyRestoration
@Skizo-ozᴉʞS Yes I have tried cleaning and rebuilding the project. Nothing changed.Quartile
Can you just change your dependencies to these : implementation "com.google.android.material:material:1.1.0-alpha07" implementation "androidx.viewpager2:viewpager2:1.0.0-alpha05"Restoration
@Skizo-ozᴉʞS Yeah it worked! but how is it possible that the newer versions of these two dependencies cause the problem?!Quartile
because you were not using newer dependency. your gradle had 'com.google.android.material:material:1.0.0' (notice the version 1.0.0). TablayoutMediator is added in 1.1.0 i guessDenson
@anshsachdeva usinf latest 1.3.0-alpha02 but can not include tablayout mediatorSoke
A
25

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

Accordion answered 22/8, 2019 at 7:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.