【问题标题】:RecyclerView in Android not working and i can't access itAndroid 中的 RecyclerView 无法正常工作,我无法访问它
【发布时间】:2020-09-17 17:10:20
【问题描述】:

当我尝试访问回收视图时遇到问题,我得到了灰屏并且它无法正常工作 这是照片是我的问题 [这是错误] [1] [1]:https://i.stack.imgur.com/8tec9.png

这是我在 xml 中的代码

 <?xml version="1.0" encoding="utf-8"?>
<layout 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">

    <data>
<variable
    name="viewModel"
    type="com.example.moviesnerd.ui.MovieViewModel" />
    </data>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".ui.MainActivity">

    <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyclerView"
        android:scrollbars="vertical"
        android:layout_width="match_parent"
            android:layout_height="0dp"
            app:layout_constraintBottom_toTopOf="@+id/button"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
        />

        <Button
            android:id="@+id/button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="@{()->viewModel.getName()}"
            android:text="Show Movie"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="1.0"
            app:layout_constraintStart_toStartOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

这是我的依赖项

 dependencies {
    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.1'
    testImplementation 'junit:junit:4.13'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
    implementation 'com.jakewharton:butterknife:10.2.3'
    annotationProcessor 'com.jakewharton:butterknife-compiler:10.2.3'

    // ViewModel and LiveData
    implementation "android.arch.lifecycle:extensions:1.1.1"
    implementation 'androidx.recyclerview:recyclerview:1.0.0'

    implementation "androidx.cardview:cardview:1.0.0"


} 

【问题讨论】:

    标签: android xml android-recyclerview dependency-injection


    【解决方案1】:

    只需执行 构建 > 清理项目。我有同样的问题,这是我在 Stackoverflow 中提出的问题:

    RecyclerView doesn't show up in Android Studio Preview

    【讨论】:

      【解决方案2】:

      你添加了两次 RecyclerView 库,删除上面,它会工作

      【讨论】:

        【解决方案3】:

        我不熟悉 androidx.constraintlayout.widget.ConstraintLayout 但你的 RecyclerView android:layout_height="0dp"。
        把它改成 wrap_content 就可以了。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-09-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多