【问题标题】:How to access RecyclerView in androidx?如何在 androidx 中访问 RecyclerView?
【发布时间】:2019-09-04 17:15:17
【问题描述】:

您好,我是 Android 开发新手,我正在尝试我在 YouTube 上找到的关于 MVVM 的教程。视频中的示例项目使用 AppCompat,但我将我的项目转换为 androidx,因为从我读到的当前(?)版本要使用?我的想法是不是错了?

无论如何,本教程的一部分使用了 RecyclerView,我无法在我的 activity_main.xml 文件中访问它,说明 v7 是一个未解决的包。 android.support.v7.widget.RecyclerView 在 v7 及更高版本中显示为红色文本。我知道我可以将它恢复到旧版本,但我想我正在努力完成这项工作,因为它希望知道如何使用 androidx 对吗?

我不知道如何将 RecyclerView 添加到我当前项目迁移到 androidx 的项目中。

我的尝试:

  • 根据文档添加implementation 'com.android.support:recyclerview-v7:28.0.0'
  • 缓存无效并重新启动

我的依赖:

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.1.0-alpha04'
    implementation 'androidx.appcompat:appcompat:1.0.0-beta01'
    implementation 'androidx.cardview:cardview:1.0.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.0-alpha4'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha4'

    //RecyclerView
    implementation 'com.android.support:recyclerview-v7:28.0.0'


    // Lifecycle components
    implementation "androidx.lifecycle:lifecycle-extensions:2.1.0-alpha04"
    annotationProcessor "androidx.lifecycle:lifecycle-compiler:2.1.0-alpha04"

    // Room Components
    implementation "androidx.room:room-runtime:2.1.0-alpha06"
    annotationProcessor "androidx.room:room-compiler:2.1.0-alpha06"
}

我的activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <view class="android.support.v7.widget.RecyclerView"
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:listitem="@layout/todo_item"/>

</androidx.constraintlayout.widget.ConstraintLayout>

【问题讨论】:

  • 请不要仅仅因为您使用该 IDE 而使用 IDE 标签 (android-studio) 来标记问题:这些标签仅应在您对 IDE 本身有疑问时使用,而不是您编写的任何代码(或想写)在里面。请参阅 when is it appropriate to remove an IDE tagHow do I avoid misusing tags?tagging guide。另外,请阅读How to Ask
  • 为什么你添加了两次appcompat?当没有任何工作时,使用alphabeta 版本尤其没有意义。这个问题应该标记为beta-testing
  • 您应该知道 AndroidX 和 Android 支持库(编号版本)之间的区别。使用 AndroidX 时,不要使用任何编号的支持库。在使用 AndroidX 之前,请阅读此 AndroidX documentation,以大致了解如何以及为什么要使用它。您似乎不知道依赖版本在 Android 中是如何工作的。这是good blog,让您快速了解库的版本名称。

标签: android android-gradle-plugin androidx


【解决方案1】:

通过在 build.gradle(Module:App) 中添加 Dependency 解决了这个问题:

implementation 'androidx.recyclerview:recyclerview:1.1.0'

并将此代码添加到您的首选布局中以使用 RecyclerView

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

这里是文档的链接:https://developer.android.com/guide/topics/ui/layout/recyclerview

【讨论】:

    【解决方案2】:

    通常只需右键单击 build.gradle (Module: app) > Refactor > Migrate to AndroidX。自动迁移和重命名所有依赖项。

    【讨论】:

      【解决方案3】:

      你可以使用materials design Dependencies

      implementation 'com.google.android.material:material:1.2.0-alpha04'
      implementation 'com.android.support:multidex:1.0.3'
      

      【讨论】:

        【解决方案4】:

        RecyclerView也已迁移到 AndroidX:

        1. 更新build.gradle:
        implementation 'androidx.recyclerview:recyclerview:1.1.0'
        
        1. 更改布局文件:
        <androidx.recyclerview.widget.RecyclerView>...</androidx.recyclerview.widget.RecyclerView
        

        【讨论】:

        • 哇,感谢您的快速响应!所以我只是用谷歌搜索了文档,我只是想知道......文档是否更新为最新版本(如 androidx)?还是我真的不擅长搜索?
        • 现在最好在 Android Studio 中使用 Refactor -> Migrate to AndroidX... 菜单选项进行转换,因为混合类型(AndroidX 和支持在同一项目中使用)将无法构建,参考:@987654321 @ .并且还需要全局搜索support关键字,通过引用developer.android.com/jetpack/androidx/migrate手动替换缺失的转换
        • 我想知道他们为什么不更改官方文档:developer.android.com/guide/topics/ui/layout/…
        • 对我来说它只适用于: implementation 'androidx.recyclerview:recyclerview:1.0.0'
        【解决方案5】:

        如果您按照上述说明操作,并且 RecyclerView 在布局预览中仍然显示为灰色框,请尝试重新构建您的项目。

        【讨论】:

          【解决方案6】:

          官方文档:https://developer.android.com/jetpack/androidx/migrate

          第 1 步: 检查并设置 gradle.properties 中的行:

          android.useAndroidX=true
          android.enableJetifier=true
          

          第 2 步: 改变

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

          implementation 'androidx.recyclerview:recyclerview:1.0.0' //Update to latest version
          

          最后: 更改标签

           <view class="android.support.v7.widget.RecyclerView"...>
          

          <androidx.recyclerview.widget.RecyclerView
               android:id="@+id/recycler_view"
               android:layout_width="match_parent"
               android:layout_height="match_parent"
               tools:listitem="@layout/todo_item"/>
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2019-03-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-02-04
            相关资源
            最近更新 更多