【问题标题】:Android RecyclerView in ConstraintLayout doesn't scrollConstraintLayout中的Android RecyclerView不滚动
【发布时间】:2018-01-05 22:08:43
【问题描述】:

我在约束布局中有一个 recyclerView,我无法让它滚动,列表只是在屏幕下方继续,没有滚动的可能性。如果我将布局转换为相对布局,则滚动效果很好。

我怎样才能让它滚动?

以下 XML 显示了我的布局,回收站视图位于底部。布局在屏幕顶部有图像和描述。此屏幕设置占屏幕的 30%。后跟一个分隔符和回收器视图,它应该占据屏幕的其余部分并且不能滚动

<?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>

【问题讨论】:

    标签: android android-recyclerview android-scrollview android-constraintlayout


    【解决方案1】:

    要滚动 RecyclerView,必须满足以下两项之一:

    • RecyclerView 的高度小于其所有项目的高度
    • RecyclerView 位于滚动父级中

    ConstraintLayout 不是滚动父级,因此我们必须确保RecyclerView“太小”,这将导致它让用户滚动其子级。

    最简单的方法是给 RecyclerView 一个定义的高度,如下所示:

    android:layout_height="200dp"
    

    当然,这只有在您提前确切知道您希望 RecyclerView 有多大时才有效,但通常情况并非如此。

    更好的解决方案是使用约束来定义RecyclerView 的高度。第一步是给它一个高度0dp,可以认为是“匹配约束”。换句话说,您是在告诉系统使RecyclerView 尽可能高以满足其顶部和底部约束。

    接下来,您必须定义顶部和底部约束。最简单的方法是将RecyclerView 的顶部约束到父级的顶部,并将其底部约束到父级的底部。可能看起来像这样:

    android:layout_height="0dp"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    

    或者,您可以相对于其他一些视图定位RecyclerView。可能看起来像这样:

    android:layout_height="0dp"
    app:layout_constraintTop_toBottomOf="@+id/viewAboveMe"
    app:layout_constraintBottom_toTopOf="@+id/viewBelowMe"
    

    只要您将0dp 的高度与顶部和底部约束结合起来(并且只要您的RecyclerView 实际上小于其内容),这将允许您的RecyclerView 根据需要滚动。

    原创

    您的RecyclerView 使用wrap_content 作为其高度。如果你想让它滚动,你必须提供一个固定的高度,或者,在 ConstraintLayout 中,使用 0dp 作为它的高度,并给它一个 app:layout_constraintBottom_xxx 属性。

    【讨论】:

    • 它有效!但是我不明白为什么,0dp 如何帮助滚动,为什么它不能与 wrap_content 一起使用
    • 如果你使用wrap_content,回收器会根据需要让自己达到包装所有东西的高度......但父级会切断它的底部。如果你使用 0dp 加上一个约束底部,你会让 recyclerview smaller 比它的内容......现在 recyclerview 的滚动属性开始了。
    • 我不知道 app:layout_constraintBottom_xxx 你能解释一下吗
    • 我不知道为什么,但在某些情况下将 recyclerView 高度设置为 0dp 并且它的所有垂直约束都不起作用。我设法通过用 LinearLayout 替换 ConstraintLayout 并在父级和 recyclerview 本身上将高度设置为 match_parent 来使其水平滚动
    • 我帮我把width="0dp" 放到了回收站。放置了必要的顶部和底部约束,高度设置为 0dp,但没有帮助。从同事那里听说match_parent 不建议在ConstraintLayout 中使用,并且也更改了宽度并且效果很好:)
    【解决方案2】:

    这对我有用:

    <?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>
    

    【讨论】:

      【解决方案3】:

      有时会因为没有限制回收站视图的底部而发生。这会导致回收站视图具有超出可见屏幕的无限高度。

      【讨论】:

        【解决方案4】:

        在 RecyclerView 中,以下更改对我有用。

        <androidx.recyclerview.widget.RecyclerView.   
                android:layout_width="match_parent"
                android:layout_height="0dp"
                app:layout_constraintBottom_toTopOf="@id/idBelowView"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHeight_default="wrap"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/idAboveView" />
        

        【讨论】:

          【解决方案5】:

          我通过以下 XML 结构 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>
          

          【讨论】:

            猜你喜欢
            • 2023-04-05
            • 2020-04-29
            • 2020-12-23
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-01-17
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多