如何将RecyclerView添加到项目中。

15

尝试导入android.support.v7.widget.RecyclerView,但失败了,还添加了

compile 'com.android.support:recyclerview-v7:23.1.1'

将其添加到build.gradle中,但这也没有帮助


1
请发布您的 build.gradle 文件和错误信息。 - Giorgio Antonioli
1
发布(post)和布局(layout) - xAqweRx
6个回答

36

您还可以从Android Studio菜单中添加库/依赖项。

点击 "Build" -> "编辑库和依赖项"。 然后点击右侧的 "+" 按钮。

搜索任何库。

例如搜索 "recycler"。

然后从列表中选择 "com.android.support:recyclerview-v7:x.x.x",完成。


2
这是最好的答案,但仍不完美。搜索会产生各种各样的库,但没有你需要的回收站;它可以显示最新版本,而不是你需要的版本;既然它似乎非常有用,那么完善这个答案可能是值得的。 - Witold Kaczurba

5
  1. 打开 build.gradle 文件,添加 RecyclerView 依赖:com.android.support:recyclerview-v7:23.1.1,并重新构建项目。

build.gradle

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'
compile 'com.android.support:recyclerview-v7:23.1.1'}
  1. 使用最新版本的构建工具,Android Studio为每个活动创建两个布局文件。对于主要活动,它创建了activity_main.xml(包含CoordinatorLayout和AppBarLayout)和content_main.xml(用于实际内容)。打开content_main.xml并查看recycler view小部件。

布局

<android.support.v7.widget.RecyclerView
    android:id="@+id/recycler_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:scrollbars="vertical" />

</RelativeLayout>

1
构建 Gradle:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'
compile 'com.android.support:recyclerview-v7:23.1.1'
}

XML文件:
<android.support.v7.widget.RecyclerView
    android:id="@+id/recycler_view"
    android:scrollbars="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

1
更新此依赖项到最新版本。
compile 'com.android.support:recyclerview-v7:25.3.1'
compile 'com.android.support:design:25.3.1'

0

只是放置这个

implementation'com.android.support:design:25.3.1'

也会起作用。


0
 1.Open build.gradle and add recycler view dependency. 

    implementation 'androidx.recyclerview:recyclerview:1.2.1' 

2. left corner of this file sync now option and click on it.

3. in xml file you can add below code:

<androidx.recyclerview.widget.RecyclerView
 android:id="@+id/listing"
 android:layout_width="match_parent"
 android:layout_height="match_parent" />

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接