如何在androidx中访问RecyclerView?

How to access RecyclerView in androidx?

大家好,我是 Android 开发的新手,我正在试用在 YouTube 上找到的有关 MVVM 的教程。视频中的示例项目使用 AppCompat,但我将我的项目转换为 androidx,因为从我读到的内容来看,它的当前(?)版本要使用?我是不是误会了这个想法?

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

我现在的项目迁移到androidx后,不知道如何将RecyclerView添加到项目中。

我试过的:

我的依赖项:

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>

RecyclerView也迁移到了 AndroidX:

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

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

第一步: 检查并设置 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"/>

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

你可以使用materials design Dependencies

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

通常只需右键单击 build.gradle(模块:应用程序)> 重构 > 迁移到 AndroidX。自动迁移和重命名所有依赖项。

此问题已通过向 build.gradle(Module:App) 添加依赖关系解决:

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" />

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