Android RecyclerView in ConstraintLayout doesn't scroll
For a RecyclerView
to scroll, one of two things must be true:
- The
RecyclerView
has a smaller height than all of its items - The
RecyclerView
is inside a scrolling parent
ConstraintLayout
is not a scrolling parent, so we have to make sure that the RecyclerView
is "too small", which will cause it to let the user scroll its children.
The easiest way is to just give the RecyclerView
a defined height, with something like this:
android:layout_height="200dp"
Of course, this only works if you know ahead of time exactly how big you want your RecyclerView
to be, and this is not usually the case.
A better solution is to use constraints to define the height of your RecyclerView
. The first step is to give it a height of 0dp
, which can be thought of as "match constraints". In other words, you're telling the system to make the RecyclerView
as tall as it needs to be in order to satisfy its top and bottom constrains.
Next, you must define top and bottom constraints. The simplest would be to constrain the RecyclerView
's top to the parent's top and its bottom to the parent's bottom. That might look like this:
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
Or, you could position the RecyclerView
relative to some other views. That might look like this:
android:layout_height="0dp"
app:layout_constraintTop_toBottomOf="@+id/viewAboveMe"
app:layout_constraintBottom_toTopOf="@+id/viewBelowMe"
As long as you combine a height of 0dp
with both top and bottom constraints (and as long as your RecyclerView
is actually smaller than its contents), this will allow your RecyclerView
to scroll as desired.
Original
Your RecyclerView
is using wrap_content
for its height. If you want it to scroll, you must provide a fixed height, or, inside a ConstraintLayout
, use 0dp
for its height and give it an app:layout_constraintBottom_xxx
attribute.