我没有导入 Recycler View AndroidX 库,但我正在使用它。为什么以及如何运作?

I did not Import the Recycler View AndroidX Library, yet, I am using it. Why and How is it working?

build.gradle(app)

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.example.todolistapp"
        minSdkVersion 16
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"

    implementation 'androidx.appcompat:appcompat:1.0.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'

    // Room
    def room_version = "2.0.0"
    implementation "androidx.room:room-runtime:$room_version"
    kapt "androidx.room:room-compiler:$room_version"

    // ViewModel and LiveData
    def lifecycle_version = "2.0.0"
    implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"
    kapt "androidx.lifecycle:lifecycle-compiler:$lifecycle_version"

    // For Floating Action Button
    implementation 'com.google.android.material:material:1.0.0'
}

从我的依赖可以看出,我没有导入回收视图Androidx库。

androidx.recyclerview:recyclerview:1.0.0

但如下所示,我可以在我的布局 (activity_main.xml) 和 MainActivity 代码中轻松使用它。

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerViewTasks"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clipToPadding="false"
        android:paddingBottom="80dp"/>

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="16dp"
        android:tint="@android:color/white"
        app:srcCompat="@android:drawable/ic_input_add"/>
</FrameLayout>

MainActivity.kt

import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity(), TaskAdapter.TaskViewCliskListener {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        recyclerViewTasks.layoutManager = LinearLayoutManager(this)
}

造成这种行为的原因是什么?

根据 dependencies of the com.google.android.material:material:1.0.0:

androidx.recyclerview:recyclerview:1.0.0

这意味着 Material 库已经对 RecyclerView 产生了传递依赖,您不需要自己手动包含它。

I did not Import the Recycler View AndroidX Library, yet i am using. Why and How is it working?

因为你添加了com.google.android.material:material:1.0.0

的依赖

如果您已经添加了 com.google.android.material:material:1.0.0,则无需添加 androidx.recyclerview:recyclerview:1.0.0

我试过了 tested.The com.google.android.material:material:1.0.0 也包括 RecyclerView。可能是为了让 material 的设计看起来和 MaterialButton

一样

com.google.android.material:material:1.0.0 包含 RecyclerView 组件所以你不需要在外部添加它。

如果您将 com.google.android.material:material:1.0.0 添加到您的应用程序依赖项中,则无需将 androidx.recyclerview:recyclerview:1.0.0 添加到您的依赖项中,因为它已包含在 material 库中.

更多信息https://developer.android.com/jetpack/androidx/migrate/class-mappings

dependencies{

implementation 'com.google.android.material:material:1.0.0'

}

依赖项{

implementation 'com.android.support:design:your version'

}

如果您已经添加了

com.google.android.material:material:1.0.0 要么 com.android.support:设计:版本

在 android 的早期版本中,recyclerview 进入设计库,但在 Androidx 的版本中,它在 material 库中。

在您的依赖项中,不需要 recyclerview 依赖项。它已经添加到 material/design 库

Check Here

详情Android图书馆

如果您的问题是使用与 material 依赖项中的版本不同的版本。

每次我手动添加 androidx.recyclerview:recyclerview 规范时,我都尝试使用 recyclerview 的 alpha 版本,代码在构建时运行良好。 但是应用程序一直崩溃,因为它找不到我在运行时从 recyclerview 的 alpha 版本使用的新 类。

我尝试使用 material decency 的最新 alpha 版本,但它也依赖于 recyclerview 上的最新稳定版本..所以它没有帮助。

为了解决这个问题,我从 material 中排除了 recyclerview 体面,并像这样自己添加了 recyclerview 体面。

implementation("com.google.android.material:material:1.1.0") {
    exclude(group = "androidx.recyclerview", module = "recyclerview")
}
implementation("androidx.recyclerview:recyclerview:1.2.0-alpha04")