如何在androidx中访问RecyclerView?
嗨,我是Android开发的新手,我正在尝试一个关于MVVM的教程,我在YouTube上找到了这个教程。视频中的示例项目使用AppCompat,但我将我的转换为androidx,因为从我阅读的内容来看,它是要使用的当前(?)版本?我的想法错了吗 无论如何,本教程的一部分使用了RecyclerView,我无法在我的activity_main.xml文件中访问它,说明v7是一个未解析的包如何在androidx中访问RecyclerView?,android,android-gradle-plugin,androidx,Android,Android Gradle Plugin,Androidx,嗨,我是Android开发的新手,我正在尝试一个关于MVVM的教程,我在YouTube上找到了这个教程。视频中的示例项目使用AppCompat,但我将我的转换为androidx,因为从我阅读的内容来看,它是要使用的当前(?)版本?我的想法错了吗 无论如何,本教程的一部分使用了RecyclerView,我无法在我的activity_main.xml文件中访问它,说明v7是一个未解析的包android.support.v7.widget.RecyclerView以红色文本显示v7以后的版本。我知道我
android.support.v7.widget.RecyclerView
以红色文本显示v7以后的版本。我知道我可以将它恢复到旧版本,但我想我正在努力使它工作,因为它希望知道如何使用androidx,对吗
我不知道如何将RecyclerView添加到迁移到androidx的当前项目中
我所尝试的:
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"
}
- 根据文档添加
实现'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"
}
我的活动\u 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:
build.gradle
:…正式文件:
步骤1:
检查并设置gradle.properties中的行:
android.useAndroidX=true
android.enableJetifier=true
步骤2:
改变
到
最后:
更改标签
<view class="android.support.v7.widget.RecyclerView"...>
到
如果您按照上述说明进行操作,并且在布局预览中RecyclerView仍显示为灰色框,请尝试重新生成项目。您可以使用材质设计依赖项
implementation 'com.google.android.material:material:1.2.0-alpha04'
implementation 'com.android.support:multidex:1.0.3'
通常只需右键单击build.gradle(模块:app)>重构>迁移到AndroidX。自动迁移和重命名所有依赖项。此问题通过向build.gradle(模块:App)添加依赖项来解决:
并将此代码添加到首选布局中,以便使用RecyclerView
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
哇,感谢您的快速响应!所以我只是在谷歌上搜索了这些文档,我只是想知道……这些文档是用最新版本更新的吗(比如androidx)?还是我真的那么不擅长搜索?现在最好使用Android Studio中的重构->迁移到AndroidX…
菜单选项来进行转换,因为混合类型(AndroidX和支持在同一项目中使用)将无法生成,请参考:。还需要对support
关键字进行全局搜索,通过引用手动替换缺少的转换。我想知道他们为什么不更改官方文档:对我来说,它只适用于:实现“androidx.recyclerview:recyclerview:1.0.0”请不要用IDE标签标记问题(android studio)仅仅因为您使用了该IDE:这些标记只应该在您对IDE本身有疑问时使用,而不是在其中编写(或想要编写)任何代码时使用。看到了吗,还有。另外,请阅读为什么添加了两次appcompat
?使用alpha
和beta
版本尤其毫无意义,因为这些版本都不起作用。这个问题应该被标记。你应该知道AndroidX和Android支持库(编号版本)之间的区别。使用AndroidX时,不要使用任何编号的支持库。在使用AndroidX之前,请阅读此AndroidX,大致了解如何使用它以及为什么使用它。您似乎不知道依赖版本在Android中是如何工作的。下面是一个示例,让您快速了解库的版本名。
<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"/>
implementation 'com.google.android.material:material:1.2.0-alpha04'
implementation 'com.android.support:multidex:1.0.3'
implementation 'androidx.recyclerview:recyclerview:1.1.0'
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />