0

I'm using the following code to create a recycler view. The recyclerview has got a grid layout manager with an item count of up to 2 per row.

 <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:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white"
        android:clickable="true"
        android:focusableInTouchMode="true"
        android:focusable="true">

    <ImageView
        android:id="@+id/backPress"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_marginTop="20dp"
        android:layout_marginEnd="16dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/hello"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Title!"
        android:textSize="30sp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        />

    <TextView
        android:id="@+id/textGoing"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Subtitle"
        android:textSize="@dimen/text_average"
        android:textStyle="bold"
        app:layout_constraintTop_toBottomOf="@+id/hello" />
    <com.google.android.material.textfield.TextInputLayout
        android:layout_margin="@dimen/dimen_20"
        app:layout_constraintTop_toBottomOf="@+id/textGoing"
        android:id="@+id/anchor_input"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:boxBackgroundColor="@android:color/white"
        android:background="@android:color/transparent" >

        <EditText
            android:id="@+id/anchor_edit_txt"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            />
    </com.google.android.material.textfield.TextInputLayout>

    <TextView
        app:layout_constraintTop_toBottomOf="@+id/anchor_input"
        app:layout_constraintStart_toStartOf="@+id/anchor_input"
        android:id="@+id/anchor_hint"
        android:layout_below="@+id/anchor_input"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />


    <Button
        android:id="@+id/add_cat_btn"
        app:layout_constraintTop_toBottomOf="@+id/anchor_hint"
        app:layout_constraintEnd_toEndOf="parent"
        android:layout_below="@+id/anchor_hint"
        android:layout_marginTop="@dimen/dimen_20"
        android:text="START"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"/>

    <TextView
        android:id="@+id/category_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="@+id/add_cat_btn"
        android:text="Description"/>
    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/category_rv"
        app:layout_constraintTop_toBottomOf="@+id/category_title"
        app:layout_constraintStart_toStartOf="parent"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
        tools:itemCount="4"
        android:layout_margin="@dimen/dimen_20"/>

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/move_next"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:fabSize="normal"
        app:tint="@android:color/white"
        app:layout_constraintEnd_toEndOf="@+id/category_rv"
        app:layout_constraintBottom_toBottomOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

Unfortunately, the recycler view that I have nested within the ConstraintLayout is not scrolling at all. What am I missing? Does constraint layout support the same?

user1241241
  • 664
  • 5
  • 20
  • 2
    Does this answer your question? [Android RecyclerView in ConstraintLayout doesn't scroll](https://stackoverflow.com/questions/45397199/android-recyclerview-in-constraintlayout-doesnt-scroll) – Yoleth Mar 23 '21 at 13:58
  • no.. i tried it.. It wasn't working – user1241241 Mar 23 '21 at 14:01
  • That question linked above really looks like the problem: your RecyclerView's height is `wrap_content`. What exactly did you try changing? – Ryan M Mar 24 '21 at 02:18

2 Answers2

1

The error comes from

tools:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
tools:itemCount="4"

replace it by

app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
app:spanCount="4"
Yoleth
  • 1,269
  • 7
  • 15
1

To solve RecyclerView scrolling issue with ConstraintLayout parent change your RecyclerView height from wrap_content to 0dp and add top and bottom constraints like below:

<androidx.recyclerview.widget.RecyclerView
   android:id="@+id/category_rv"
   android:layout_width="0dp"
   android:layout_height="0dp"
   app:layout_constraintStart_toStartOf="parent"
   app:layout_constraintEnd_toEndOf="parent"
   app:layout_constraintTop_toBottomOf="@+id/category_title"
   app:layout_constraintBottom_toBottomOf="parent"/>
MariosP
  • 8,300
  • 1
  • 9
  • 30