簡體   English   中英

寬度和高度為 match_parent 的 Recyclerview 返回固定大小錯誤

[英]Recyclerview with match_parent for width and height returns fixed size error

生成 APK/Signed Bundle 時,lintVitalRelease 返回此報告:

<issue
    id="InvalidSetHasFixedSize"
    severity="Fatal"
    message="When using `setHasFixedSize() in an `RecyclerView`, `wrap_content` cannot be used as \&#xA;a value for `size` in the scrolling direction."
    category="Correctness"
    priority="5"
    summary="When using `setHasFixedSize() in an `RecyclerView`, `wrap_content` cannot be used as \&#xA;a value for `size` in the scrolling direction."
    explanation="When a RecyclerView uses `setHasFixedSize(...)` you cannot use `wrap_content` for  size in the scrolling direction."
    errorLine1="        recyclerView.setHasFixedSize(true);"
    errorLine2="        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~">
    <location
        file="/home/Documents/MyProject/app/src/main/java/com/myproject/MainActivity.java"
        line="97"
        column="9"/>
</issue>

這顯然不是真的,因為我的布局文件如下所示:

<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:id="@+id/parent_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
    android:id="@+id/refresh_layout"
    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"
        android:background="@color/white"
        android:clipToPadding="false"
        android:overScrollMode="never"
        android:paddingTop="8dp"
        android:scrollbars="none"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />

</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

lint 是錯誤的還是我搞砸了?

在我將 kotlin 與我的 java 項目集成后,這成為了一個問題!

您在代碼中的錯誤可能是這樣的

<android.support.v7.widget.RecyclerView
    android:id="@+id/my_recycler_view"
    android:scrollbars="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

在這種情況下,我們將使用 my_recycler_view.setHasFixedSize (true)

<android.support.v7.widget.RecyclerView
        android:id="@+id/my_recycler_view"
        android:scrollbars="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

而在這另一個我的 recyclerview.setHasFixedSize (false) 中,如果我們也使用 wrap_content 作為寬度,這適用

問題出在回收站項目布局上。 它不能設置為包裝內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM