android - 两个 RecyclerView 依赖项有什么区别?

标签 android android-recyclerview android-support-library android-support-design

我正在使用以下 RecyclerView:

<androidx.recyclerview.widget.RecyclerView ... />

build.gradle 中有以下依赖:

implementation 'androidx.recyclerview:recyclerview:1.0.0'

在android studio的Design模式下点击RecyclerView前面的下载按钮后下载。 我在 Oreo 上运行我的应用程序,它运行良好。

但是当我在 KitKat 上运行应用程序时,应用程序被杀死,所以我注释掉 RecyclerView 及其相关的源代码并再次运行应用程序,这次应用程序在 KitKat 上正常运行,所以我意识到问题出在 KitKat 版本的 RecyclerView 上。

现在我做了一些研究,发现我必须使用:

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

和以下 xml 标记:

<android.support.v7.widget.RecyclerView ... />

现在我的问题是

之间有什么区别
<androidx.recyclerview.widget.RecyclerView ... />
implementation 'androidx.recyclerview:recyclerview:1.0.0'

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

哪一个是为了什么目的而创建的,应该在什么情况下使用哪一个?为什么 android studio 下载 KitKat 不支持的那个,而不是下载几乎以前版本的 android 支持的那个?谢谢!!!

最佳答案

What is the difference between two RecyclerView dependencies?

RecyclerView Support LibraryRecyclerView AndroidX library 具有相同的目的。主要区别在于包名。 AndroidX 主要是一个重新打包和重新命名的支持库,主要目的是简化 Android 开发过程。所有的支持库能力在AndroidX中仍然存在,但是现在最新的能力和开发都转移到AndroidX上了。但这并不意味着支持库已被弃用。请参阅 Support Library 中的以下注释文档 :

Note: With the release of Android 9.0 (API level 28) there is a new version of the support library called AndroidX which is part of Jetpack. The AndroidX library contains the existing support library and also includes the latest Jetpack components.

You can continue to use the support library. Historical artifacts (those versioned 27 and earlier, and packaged as android.support.*) will remain available on Google Maven. However, all new library development will occur in the AndroidX library.

We recommend using the AndroidX libraries in all new projects. You should also consider migrating existing projects to AndroidX as well.

因此,您应该为您最近的项目迁移到 AndroidX。


But when i run the app on KitKat, the application is killed, so I comment out the RecyclerView and its related source code and run the application again and this time application runs properly on KitKat, so i realize that the problem is with the RecyclerView for the KitKat version.

这可能是您代码中的错误,或者最不可能是 AndroidX RecyclerView 中的错误。没有您的错误日志,我们无法找到问题所在。因此,您需要在问题中提供错误日志。


Which one is created for which purpose and which one should be used in which situation?

对于新项目,您应该迁移到 AndroidX。但是对于紧密依赖于需要支持库作为依赖项的库的遗留项目,您需要执行以下操作之一:

  1. 将库升级到 AndroidX(这很乏味,可能不值得付出努力)
  2. 继续使用支持库。
  3. 在你的 gradle.properties 中使用 android.enableJetifier,参见 Migrating to AndroidX

关于android - 两个 RecyclerView 依赖项有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57261877/

相关文章:

android - 向左/向右滑动 RecyclerView 的部分行

java - 在 Marshmallow(6.0+) 设备上的 onCreateViewHolder 方法中膨胀 RecyclerView 项目的布局时抛出 "android.view.InflateException"

android - 非法反射访问操作

android - 播放计费库连接和查询sku详情

android - 下拉android时SwipeRefreshLayout加载器不会启动

Android DialogFragment 不会关闭

android - 如何在 android 选项卡布局中提供溢出分页?

java - 如何预填充我的 Room 数据库?

Android:bindService 不会连接到远程 (AIDL) 服务,我不知道为什么

android - 回收者 View 的项目触摸监听器