I have a recyclerView inside a constraint layout and I cannot make it scroll, the list just continues below the screen without scroll possibility. If I turn the layout into relative layout the scroll works fine.
how can I get it to scroll?
the the following XML shows my layout, the recycler view is in the bottom. the layout has an image and description at the top of the screen. this screen setup takes 30% of the screen. followed by and a separator and the recycler view that should take the rest of the screen and that cannot scroll
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/gradient_top">
<android.support.v7.widget.AppCompatImageView
android:id="@+id/imageViewLock"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginBottom="10dp"
android:layout_marginTop="16dp"
app:layout_constraintBottom_toTopOf="@+id/textViewPermissionsTitle"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_phone_lock"/>
<TextView
android:id="@+id/textViewPermissionsTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:gravity="center"
android:paddingLeft="24dp"
android:paddingRight="24dp"
android:text="@string/allow_permissions"
android:textColor="@color/white"
android:textSize="@dimen/description_text_size"
app:layout_constraintBottom_toTopOf="@+id/guideline1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
<android.support.constraint.Guideline
android:id="@+id/guideline1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.3"/>
<View
android:id="@+id/viewSeparator"
android:layout_width="match_parent"
android:layout_height="0.7dp"
android:layout_marginTop="10dp"
android:background="@color/bright_blue"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="@+id/guideline1"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerViewPermissions"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbarSize="1dp"
android:scrollbarThumbVertical="@color/white"
android:scrollbars="vertical"
app:layout_constraintTop_toBottomOf="@+id/viewSeparator" />
</android.support.constraint.ConstraintLayout>
For a RecyclerView
to scroll, one of two things must be true:
RecyclerView
has a smaller height than all of its itemsRecyclerView
is inside a scrolling parentConstraintLayout
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.
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.