【问题标题】:How should I set up RecyclerView version to match my project?我应该如何设置 RecyclerView 版本以匹配我的项目?
【发布时间】:2019-10-06 18:11:45
【问题描述】:

我尝试运行/编写 RecyclerView 示例已经有一段时间了,但没有成功。我想这与依赖项中使用的 RecyclerView 版本和我的项目使用的 Android 版本有关。 目前,我正在尝试运行此示例here

没有提到compileSdkVersion、minSdkVersion等是什么。 无论如何,这是我的 gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.2"
    defaultConfig {
        applicationId "com.example.deluxe.example"
        minSdkVersion 24
        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 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    implementation 'com.android.support:cardview-v7:28.0.0'
    implementation 'com.android.support:recyclerview-v7:28.0.0-rc01'
    implementation 'com.squareup.picasso:picasso:2.5.2'
}

同样在此示例中,适配器导入 android.support.v7.widget.RecyclerView,对此我收到错误:无法解析符号 v7。所以,Android 建议导入 androidx.recyclerview.widget.RecyclerView;

问题:我的项目没有在 MainActivity 中加载 RecyclerView - 它引发异常: 错误膨胀类 android.support.v7.widget.RecyclerView 尽管hereherehere有类似的问题,但我无法解决我的问题。

我是 Android Studio 的新手,谁能建议我应该改变什么来运行这个 Recycler/CardView 示例?我已经尝试了几个示例,但无法运行其中的任何一个——最近的和旧的。 我正在尝试使用 Android Nougat(7.0) 和更高版本运行,但这不是强制性的。 我的 Android Studio 版本是 3.5.1

【问题讨论】:

    标签: android android-recyclerview


    【解决方案1】:

    因为你用的是Androidx(好),那么你需要导入相应的artifacts:

    implementation 'androidx.recyclerview:recyclerview:1.1.0-beta04'(或任何最新版本)

    然后在您引用 RecyclerView 的所有地方,(提示:使用 Android Studio“查找”并搜索术语 recyclerview 以查找和评估所有引用和提到它的地方),并确保您引用新工件,例如:

    import androidx.recyclerview.widget.RecyclerView 在代码中和

    <androidx.recyclerview.widget.RecyclerView ... 在 XML 中。

    当你在它的时候,删除

        implementation 'com.android.support:recyclerview-v7:28.0.0-rc01'
    

    如果可以,请替换:

        implementation 'com.android.support:cardview-v7:28.0.0'
    

    与:

        implementation "com.google.android.material:material:1.1.0-alpha10"
    

    并使用:

     <com.google.android.material.card.MaterialCardView 
    

    在您的 XML 中。

    【讨论】:

    • 谢谢,我做了相应的更改,至少我能够看到布局。虽然无法运行该应用程序。我遇到了不同的错误,我认为这些错误与另一个问题有关。
    猜你喜欢
    • 2012-02-24
    • 2014-08-09
    • 1970-01-01
    • 2016-08-28
    • 1970-01-01
    • 2020-04-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多