如何在androidx中访问RecyclerView?

kob*_*owo 7 android android-gradle-plugin androidx

嗨,我是Android开发的新手,我正在尝试在YouTube上找到的有关MVVM的教程。视频中的示例项目使用AppCompat,但我将自己的代码转换为androidx,因为从我读取的内容中可以使用current(?)版本?我是否对这种想法感到误解?

无论如何,本教程的一部分利用了RecyclerView,我无法在我的activity_main.xml文件中访问它,表明v7是一个未解决的程序包。android.support.v7.widget.RecyclerView从v7开始以红色文本显示。我知道我可以将其还原为旧版本,但我想我正在努力使这项工作取得成功,因为它有望了解如何正确使用androidx?

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

我尝试过的

  • 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"
}
Run Code Online (Sandbox Code Playgroud)

我的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>
Run Code Online (Sandbox Code Playgroud)

小智 22

RecyclerView 也已迁移到AndroidX:

  1. 更新build.gradle
implementation 'androidx.recyclerview:recyclerview:1.0.0'
Run Code Online (Sandbox Code Playgroud)
  1. 更改布局文件:
<androidx.recyclerview.widget.RecyclerView> ... </androidx.recyclerview.widget.RecyclerView
Run Code Online (Sandbox Code Playgroud)

  • 我想知道为什么他们不更改官方文档:https://developer.android.com/guide/topics/ui/layout/recyclerview#add-support-library (2认同)

Wil*_*ran 5

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

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

android.useAndroidX=true
android.enableJetifier=true
Run Code Online (Sandbox Code Playgroud)

第二步:改变

implementation 'com.android.support:recyclerview-v7:28.0.0'
Run Code Online (Sandbox Code Playgroud)

implementation 'androidx.recyclerview:recyclerview:1.0.0' //Update to latest version
Run Code Online (Sandbox Code Playgroud)

最后:更改标签

 <view class="android.support.v7.widget.RecyclerView"...>
Run Code Online (Sandbox Code Playgroud)

<androidx.recyclerview.widget.RecyclerView
     android:id="@+id/recycler_view"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     tools:listitem="@layout/todo_item"/>
Run Code Online (Sandbox Code Playgroud)