如何在安卓Lollipop中在XML中添加RecyclerView

4

我已经更新了我的Android SDK,使用的是Android 5.0(API 21); 我还更新了平台工具和构建工具。

在将RecycleView添加到布局XML文件后,我遇到了The following classes could not be found: - android.support.v7.widget.RecyclerView (Fix Build Path, Edit XML, Create Class)错误。

我的布局XML文件如下所示:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.androidlintro.MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

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

</RelativeLayout>

有人能告诉我出了什么问题吗?即使我已经下载了完整的软件包(新的SDK、构建工具、平台工具等),我还是会遇到这个错误。

注意:我正在使用Eclipse IDE。

3个回答

0

请确保在您的项目中包含了Android支持库。

在Eclipse中:右键单击项目 -> 属性 -> Android

然后将Android支持库添加到构建路径中。

接着清理项目并进行构建。


是的,它已经被添加了。appcompact_v7项目默认作为库添加。 - DCoder
我已经清理了我的项目,但在清理appcompact_v7时出现了错误“清理所选项目遇到问题”。 - DCoder
你是将它作为JAR文件添加还是将其作为工作区中包含的项目添加?请参阅http://developer.android.com/tools/support-library/setup.html#获取更多信息。 - Robin Eisenberg
现在我正在按照您提供的链接进行操作。在第4步中,我们需要导入appcompact_v7库。我已经导入了它,但是项目出现了错误。 - DCoder
res->valuesv21->style_base.xml文件中的位置出现了错误:"error: Error retrieving parent for item: No resource found that matches the given name 'android:Widget.Material.ActionButton'",该错误位于<style name="Base.Widget.AppCompat.ActionButton" parent="android:Widget.Material.ActionButton">标签处。 - DCoder

0

ECLIPSE

如果您的问题没有解决,或者是为了未来的读者,请看这里的答案:

首先从Android SDK Manager下载Android Support Library。

然后前往此位置并从此处复制.aar文件。

X:\android-sdk\extras\android\m2repository\com\android\support\recyclerview-v7\21.0.0

然后将其重命名为.zip文件,然后解压缩它,找到classes.jar文件,将其重命名为一些适当的名称,如'RecyclerView_v7.jar',然后将该.jar文件复制到您的项目库中。将其添加到构建路径中,然后重新启动Eclipse,以便可以实例化它(不是必需的,但我必须这样做)。


0

针对Android Studio:

  1. 选择:文件 -> 项目结构... -> 依赖项

  2. 点击右上角的“+”并选择“1个库依赖项”

  3. 搜索“com.android.support:recyclerview”

  4. 您将看到库的当前版本。选择它并单击“确定”

  5. Gradle将同步,然后您就准备好了。


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