RecyclerView学习中遇到的问题与解决
我下的Android studio是3.4.1,所正在学习的第一行代码第二版的AS版本是2.2,其中版本的差异可以说是很大的了。我在学习的过程因此也遇到了一些问题。
一、关于添加相应的依赖库
根据官网内容,应在build.grade中添加如下:
dependencies {
implementation 'com.android.support:recyclerview-v7:28.0.0'//配置时会出现下划线,但是是正确的,可以成功添加
}
备注:新建的项目只要需要用到RecyclerView控件,就需要在app/build.grade当中添加一次依赖库。
二、运行后软件始终无响应
在activity_main.xml的编写
原本代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
在官网上查询这么写是没问题的。但是运行后软件始终无法响应。
在翻看其它部分的代码后觉得,还是这个布局代码最可能有一些问题才导致软件无法响应。
大量尝试后:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
这样写就可以正常运行了