【发布时间】: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 tag、How do I avoid misusing tags? 和 tagging guide。另外,请阅读How to Ask
-
为什么你添加了两次
appcompat?当没有任何工作时,使用alpha和beta版本尤其没有意义。这个问题应该标记为beta-testing。 -
您应该知道 AndroidX 和 Android 支持库(编号版本)之间的区别。使用 AndroidX 时,不要使用任何编号的支持库。在使用 AndroidX 之前,请阅读此 AndroidX documentation,以大致了解如何以及为什么要使用它。您似乎不知道依赖版本在 Android 中是如何工作的。这是good blog,让您快速了解库的版本名称。
标签: android android-gradle-plugin androidx