1

I'm creating a RecyclerView with a grid layout using GridLayoutManager. Everything seems fine until i try to set the layout to the RecyclerView. I have used this code/layout files before so i know it works, however after migrating to Androidx i am suddenly experiencing issues.

Code (in Activity):

RecyclerView recyclerView = findViewById(R.id.rv_checks);
    recyclerView.setHasFixedSize(true);
    recyclerView.setNestedScrollingEnabled(false);
    recyclerView.setFocusable(false);

    GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 3);
    recyclerView.setLayoutManager(gridLayoutManager);

RecyclerView:

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/rv_checks"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/action_container"
    android:layout_marginTop="5dp"
    android:clipToPadding="false"
    android:padding="20dp"/>

build.gradle:

android {
compileSdkVersion 28
defaultConfig {
    applicationId "com.example.app"
    minSdkVersion 21
    targetSdkVersion 28
    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 'androidx.appcompat:appcompat:1.0.0-beta01'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.2'

    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.0-alpha4'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha4'

    implementation 'androidx.recyclerview:recyclerview:1.0.0'

}
apply plugin: 'com.google.gms.google-services'

Error:

error: incompatible types: GridLayoutManager cannot be converted to LayoutManager
Sha-1
  • 187
  • 4
  • 12

2 Answers2

1

I've just fixed this issue, the problem in this instance was around how the GridLayoutManager was being declared (solution below). This however wasn't a permanent fix as the issue was with migrating to AndroidX. A solution for fixing those issues can be found here which might (as it did in my case) throw up a library clash which can be fixed like this.

Code (in Activity):

RecyclerView recyclerView = findViewById(R.id.rv_checks);
    recyclerView.setHasFixedSize(true);
    recyclerView.setNestedScrollingEnabled(false);
    recyclerView.setFocusable(false);

    GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 3);
    recyclerView.setLayoutManager(gridLayoutManager);

Temporary solution:

androidx.recyclerview.widget.GridLayoutManager gridLayout = new androidx.recyclerview.widget.GridLayoutManager(this, 3);

gradle.properties:

android.useAndroidX=true
android.enableJetifier=true
Sha-1
  • 187
  • 4
  • 12
-1

If your migration to AndroidX tries to use this. https://developer.android.com/reference/androidx/leanback/widget/VerticalGridView.html

It is like a new recycler view for androidX

Vova
  • 956
  • 8
  • 22