Java 无法导入:Android Studio 中的 android.support.v7.widget.RecyclerView

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46446143/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 02:21:41  来源:igfitidea点击:

Can't import: android.support.v7.widget.RecyclerView in Android Studio

javaandroid

提问by Soffy

I'm following my teachers tutorial and therefore writing exactly the same code that he has in his example. So I just created a new class to learn RecyclerViewbut I can't import RecyclerViewOn mouse-over, it just says "Cannot resolve symbol RecyclerView". I use Android Studio 2.3.3.Am I missing something obvious?

我正在学习我的老师教程,因此编写了与他在示例中完全相同的代码。所以我刚刚创建了一个新类来学习,RecyclerView但我无法RecyclerView在鼠标悬停时导入,它只是说“无法解析符号 RecyclerView”。我使用Android Studio 2.3.3.我是否遗漏了一些明显的东西?

import android.support.v7.widget.RecyclerView;

public class CustomAdapter extends RecyclerView.Adapter<ComposedAdapter.Holder> {
//stuff 
}

Gradle:

摇篮:

apply plugin: 'com.android.application'android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
    applicationId "sofialarsson.customrecyclerview"
    minSdkVersion 19
    targetSdkVersion 25
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
 }
}
dependencies {
 compile fileTree(dir: 'libs', include: ['*.jar'])
 androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
    exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
}

采纳答案by akhilesh0707

You need to add dependencies in build.gradle

您需要在其中添加依赖项 build.gradle

Use this update gradlefile

使用此更新gradle文件

apply plugin: 'com.android.application'android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
    applicationId "sofialarsson.customrecyclerview"
    minSdkVersion 19
    targetSdkVersion 25
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
 }
}
dependencies {
 compile fileTree(dir: 'libs', include: ['*.jar'])
 androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
    exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support.constraint:constraint-layout:1.0.2'

compile "com.android.support:appcompat-v7:25.0.0"
compile "com.android.support:recyclerview-v7:25.0.0"

testCompile 'junit:junit:4.12'
}

回答by Nilesh Rathod

Just add compile 'com.android.support:recyclerview-v7:25.3.1'dependencies in build.gradleapp file like below code

只需像下面的代码一样compile 'com.android.support:recyclerview-v7:25.3.1'build.gradle应用程序文件中添加依赖项

dependencies {
    compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'com.android.support:recyclerview-v7:25.3.1'
}

回答by Vishal Vaishnav

Add below dependency in build.gradle;

在 build.gradle 中添加以下依赖项;

compile 'com.android.support:design:xx.x.x'

回答by Ali Khan

Just add these two dependencies and you are good to go.

只需添加这两个依赖项即可。

compile 'com.android.support:design:25.3.1'
compile 'com.android.support:recyclerview-v7:25.3.1'

Make sure always always use same versions everywhere like you use here 25.3.1. Also update your buildToolsVersion="25.0.0". Thanks!!!

确保始终在任何地方始终使用相同的版本,就像您在此处使用的 25.3.1 一样。还要更新您的buildToolsVersion="25.0.0". 谢谢!!!

回答by Lipika

Add below dependency in build.gradle

在 build.gradle 中添加以下依赖项

implementation 'androidx.recyclerview:recyclerview:1.1.0-beta02'

实现 'androidx.recyclerview:recyclerview:1.1.0-beta02'

Link Referencefor latest releases

最新版本的链接参考

回答by Foster

If you are using androidx artifacts, add the following to your app level build.gradle

如果您使用的是 androidx 工件,请将以下内容添加到您的应用级别 build.gradle

dependencies {
    implementation 'androidx.recyclerview:recyclerview:1.1.0'
    implementation 'androidx.appcompat:appcompat:1.1.0'
}

回答by Janos Vinceller

to give you an up-to-date and comprehensive answer, you have two choices.

为了给你一个最新的、全面的答案,你有两个选择。

Pre Android 9.0 (pre API 28), you have to use the old Support Library:

在 Android 9.0 (pre API 28) 之前,您必须使用旧的支持库:

dependencies {
    compile "com.android.support:appcompat-v7:25.0.0"
    compile "com.android.support:recyclerview-v7:25.0.0"
}

For these dependencies, you have to use the version number of your Build Tools Version.

对于这些依赖项,您必须使用构建工具版本的版本号。

In Android 9.0 (API 28+) and above, you have to use the new Support Library (Noted here https://developer.android.com/topic/libraries/support-library/features#v7):

在 Android 9.0 (API 28+) 及更高版本中,您必须使用新的支持库(此处注明https://developer.android.com/topic/libraries/support-library/features#v7):

dependencies {
    implementation "androidx.appcompat:appcompat:1.1.0"
    implementation "androidx.recyclerview:recyclerview:1.1.0"
}

For these dependencies, you have to use the version numbers from the following pages:

对于这些依赖项,您必须使用以下页面中的版本号: