(#) When using `setHasFixedSize()` in an `RecyclerView`, `wrap_content` cannot be used as a value for `size` in the scrolling direction.
!!! ERROR: When using `setHasFixedSize()` in an `RecyclerView`, `wrap_content` cannot be used as a value for `size` in the scrolling direction.
This is an error, and is also enforced at build time when
supported by the build system. For Android this means it will
run during release builds.
Id
: `InvalidSetHasFixedSize`
Summary
: When using `setHasFixedSize()` in an `RecyclerView`, `wrap_content` cannot be used as a value for `size` in the scrolling direction.
Severity
: Fatal
Category
: Correctness
Platform
: Android
Vendor
: Android Open Source Project
Identifier
: androidx.recyclerview
Feedback
: https://issuetracker.google.com/issues/new?component=460887
Min
: Lint 8.0 and 8.1
Compiled
: Lint 8.0 and 8.1
Artifact
: [androidx.recyclerview:recyclerview](androidx_recyclerview_recyclerview.md.html)
Since
: 1.2.0
Affects
: Kotlin and Java files and resource files
Editing
: This check can *not* run live in the IDE editor
Implementation
: [Source Code](https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:/recyclerview/recyclerview-lint/src/main/java/androidx/recyclerview/lint/InvalidSetHasFixedSizeDetector.kt)
Tests
: [Source Code](https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:/recyclerview/recyclerview-lint/src/test/java/androidx/recyclerview/lint/InvalidSetHasFixedSizeTest.kt)
Copyright Year
: 2020
When a RecyclerView uses `setHasFixedSize(...)` you cannot use
`wrap_content` for size in the scrolling direction.
(##) Example
Here is an example of lint warnings produced by this check:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~text
com/example/Example.kt:10:Error: When using `setHasFixedSize() in an
RecyclerView, wrap_content cannot be used as a value for size in the
scrolling direction. [InvalidSetHasFixedSize]
recyclerView?.setHasFixedSize(true)
-----------------------------------
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Here are the relevant source files:
`layout/recycler_view.xml`:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~xml linenumbers
<?xml version="1.0" encoding="utf-8"?>
<androidx.recyclerview.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/my_recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
`com/example/R.kt`:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~kotlin linenumbers
package com.example
object R {
object id {
const val my_recycler_view = 0
}
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
`com/example/Example.kt`:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~kotlin linenumbers
package com.example
import android.view.View
import androidx.recyclerview.widget.RecyclerView
class Example {
fun main() {
val view: View = TODO()
val recyclerView = view.findViewById