Android RecyclerView in ConstraintLayout doesn't scroll

后端 未结 4 1448
情话喂你
情话喂你 2020-12-14 07:05

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

相关标签:
4条回答
  • 2020-12-14 07:46

    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.

    0 讨论(0)
  • 2020-12-14 07:51

    Sometimes it happens because of not constraining bottom of recycler view. Which causes recycler view to be of an infinite height that goes beyond the visible screen.

    0 讨论(0)
  • 2020-12-14 07:58

    This worked for me:

    <?xml version="1.0" encoding="utf-8"?>
    <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"
        tools:context=".MainActivity">
    
        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/constraintLayout2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">
    
            ..... other controls ....
    
        </androidx.constraintlayout.widget.ConstraintLayout>
    
        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_marginStart="1dp"
            android:layout_marginEnd="1dp"
            android:layout_marginBottom="1dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/constraintLayout2" />
    </androidx.constraintlayout.widget.ConstraintLayout>
    
    0 讨论(0)
  • 2020-12-14 07:59

    I fixed the same problem by the following XML structure pattarn -

                     <androidx.constraintlayout.widget.ConstraintLayout
                            android:layout_width="match_parent"
                           /*be carefull about the height.It need to be match parent.*/
                            android:layout_height="match_parent">
            
                          Others views are here .....
            
                            <androidx.recyclerview.widget.RecyclerView
                                android:layout_width="match_parent"
                                android:layout_height="0dp" /*Must be 0dp in height*/
                                app:layout_constraintBottom_toBottomOf="parent" 
                                /*Must have a layout_constraintBottom_toBottomOf */
                                *emphasized text*/>
            
                        </androidx.constraintlayout.widget.ConstraintLayout>
    
    0 讨论(0)
提交回复
热议问题